Simple Login Form in Web API (2024)

Simple Login Form in Web API (1)

  • 346k
  • 0
  • 6

Introduction

This article shows how to create a simple login form in the ASP.NET Web API.

The following is the procedure for creating a login form in the Web API.

Step 1

First create a Web API application as in the following:

  • Start Visual Studio 2012.
  • From the start window select "New Project".
  • From the new project window select "Installed" -> "Visual C#" -> "Web".
  • Select "ASP.NET MVC4 Web Application" and click the "Ok" button.

    Simple Login Form in Web API (3)

  • From the "MVC4 Project" window select "Web API".

    Simple Login Form in Web API (4)

  • Click the "OK" button.

Step 2

Add a Model class.

  • In the "Solution Explorer".
  • Right-click on the "Model" folder.
  • Select "Add" -> "class".
  • From the Add new item window select "Installed" -> "Visual C#".

    Simple Login Form in Web API (5)

  • Select "Class" and click on the "Add" button.

Now add the following code:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. namespaceMvcApplication6.Models
  6. {
  7. publicclassLoginModel
  8. {
  9. publicstringUserName{get;set;}
  10. publicstringUserPassword{get;set;}
  11. }
  12. }

Step 3

Now in the controller we add the following code:

  • In the "Solution Explorer".
  • Expand "Controller" folder.
  • Select the "HomeController".

Simple Login Form in Web API (6)

Add the following code:

  1. usingSystem;
  2. usingSystem.Collections.Generic;
  3. usingSystem.Linq;
  4. usingSystem.Web;
  5. usingSystem.Web.Mvc;
  6. usingMvcApplication6.Models;
  7. namespaceMvcApplication6.Controllers
  8. {
  9. publicclassHomeController:Controller
  10. {
  11. publicActionResultIndex()
  12. {
  13. LoginModelobj=newLoginModel();
  14. returnView(obj);
  15. }
  16. [HttpPost]
  17. publicActionResultIndex(LoginModelobjuserlogin)
  18. {
  19. vardisplay=Userloginvalues().Where(m=>m.UserName==objuserlogin.UserName&&m.UserPassword==objuserlogin.UserPassword).FirstOrDefault();
  20. if(display!=null)
  21. {
  22. ViewBag.Status="CORRECTUserNAmeandPassword";
  23. }
  24. else
  25. {
  26. ViewBag.Status="INCORRECTUserNameorPassword";
  27. }
  28. returnView(objuserlogin);
  29. }
  30. publicList<LoginModel>Userloginvalues()
  31. {
  32. List<LoginModel>objModel=newList<LoginModel>();
  33. objModel.Add(newLoginModel{UserName="user1",UserPassword="password1"});
  34. objModel.Add(newLoginModel{UserName="user2",UserPassword="password2"});
  35. objModel.Add(newLoginModel{UserName="user3",UserPassword="password3"});
  36. objModel.Add(newLoginModel{UserName="user4",UserPassword="password4"});
  37. objModel.Add(newLoginModel{UserName="user5",UserPassword="password5"});
  38. returnobjModel;
  39. }
  40. }
  41. }

In the code above is the list of UserNames and Passwords. These are validated for the login.

Step 4

In the View write the following code:

  • In the "Solution Explorer".
  • Expand the "Views" folder.
  • Select "Home" -> "Index.cshtml".

Simple Login Form in Web API (7)

Add the following code:

  1. @modelMvcApplication6.Models.LoginModel
  2. @{
  3. ViewBag.Title="Index";
  4. }
  5. <scriptlanguage="javascript">
  6. functiondisplay(){
  7. if($("#txtusername").attr("value")=="")
  8. {
  9. alert("PleaseEnteryourUserName.");
  10. returnfalse;
  11. }elseif($("#txtuserpassword").attr("value")==""){
  12. alert("PleaseenterUserPassword.");
  13. returnfalse;
  14. }
  15. alert("LoginSuccessfully");
  16. returntrue;
  17. }
  18. </script>
  19. <h3>SimpleWeAPILoginForm</h3>
  20. @using(Html.BeginForm("Index","Home"))
  21. {
  22. <tablewidth="40%"cellpadding="1"cellspacing="5">
  23. <tr>
  24. <tdcolspan="2"style="color:#f00;font-size:larger">@ViewBag.Status</td>
  25. </tr>
  26. <tr>
  27. <tdalign="right">UserName:</td>
  28. <td>@Html.TextBoxFor(m=>m.UserName,new{@style="width:200px",@id="txtusername"})</td>
  29. </tr>
  30. <tr>
  31. <tdalign="right">UserPassword:</td>
  32. <td>@Html.PasswordFor(m=>m.UserPassword,new{@style="width:200px",@id="txtuserpassword"})</td>
  33. </tr>
  34. <tr>
  35. <tdcolspan="2">
  36. <inputtype="submit"value="Login"title="login"onclick="display();"/>
  37. </td>
  38. </tr>
  39. </table>
  40. }

Step 5

Execute the application. The output will be:

Simple Login Form in Web API (8)

Enter the UserName and UserPassword declared in the controller. The output will be:

Simple Login Form in Web API (9)

Now leavt the UserName TextBox empty; then its output looks as:

Simple Login Form in Web API (10)

Simple Login Form in Web API (2024)
Top Articles
28 Of The Biggest Open World Games Based On The Size Of Their Maps
What is UPnP and is it Safe?
Safety Jackpot Login
Skylar Vox Bra Size
Promotional Code For Spades Royale
Yogabella Babysitter
Marist Dining Hall Menu
Gw2 Legendary Amulet
Self-guided tour (for students) – Teaching & Learning Support
Apnetv.con
Whiskeytown Camera
Rls Elizabeth Nj
Declan Mining Co Coupon
Daniela Antury Telegram
Shooting Games Multiplayer Unblocked
ocala cars & trucks - by owner - craigslist
I Wanna Dance with Somebody : séances à Paris et en Île-de-France - L'Officiel des spectacles
سریال رویای شیرین جوانی قسمت 338
Northern Whooping Crane Festival highlights conservation and collaboration in Fort Smith, N.W.T. | CBC News
N2O4 Lewis Structure & Characteristics (13 Complete Facts)
Violent Night Showtimes Near Amc Fashion Valley 18
Lonesome Valley Barber
Zack Fairhurst Snapchat
The Blind Showtimes Near Amc Merchants Crossing 16
Understanding Genetics
Iu Spring Break 2024
27 Fantastic Things to do in Lynchburg, Virginia - Happy To Be Virginia
Jailfunds Send Message
Guinness World Record For Longest Imessage
LG UN90 65" 4K Smart UHD TV - 65UN9000AUJ | LG CA
Moonrise Time Tonight Near Me
Daily Journal Obituary Kankakee
How to Play the G Chord on Guitar: A Comprehensive Guide - Breakthrough Guitar | Online Guitar Lessons
Carespot Ocoee Photos
About Us | SEIL
#1 | Rottweiler Puppies For Sale In New York | Uptown
Why Holly Gibney Is One of TV's Best Protagonists
Ticket To Paradise Showtimes Near Regal Citrus Park
Registrar Lls
Wilson Tattoo Shops
Andrew Lee Torres
Karen Wilson Facebook
Gas Buddy Il
Gli italiani buttano sempre più cibo, quasi 7 etti a settimana (a testa)
Chr Pop Pulse
2294141287
Here’s What Goes on at a Gentlemen’s Club – Crafternoon Cabaret Club
Assignation en paiement ou injonction de payer ?
Where and How to Watch Sound of Freedom | Angel Studios
sin city jili
Peugeot-dealer Hedin Automotive: alles onder één dak | Hedin
Black Adam Showtimes Near Cinemark Texarkana 14
Latest Posts
Article information

Author: Prof. An Powlowski

Last Updated:

Views: 6316

Rating: 4.3 / 5 (64 voted)

Reviews: 87% of readers found this page helpful

Author information

Name: Prof. An Powlowski

Birthday: 1992-09-29

Address: Apt. 994 8891 Orval Hill, Brittnyburgh, AZ 41023-0398

Phone: +26417467956738

Job: District Marketing Strategist

Hobby: Embroidery, Bodybuilding, Motor sports, Amateur radio, Wood carving, Whittling, Air sports

Introduction: My name is Prof. An Powlowski, I am a charming, helpful, attractive, good, graceful, thoughtful, vast person who loves writing and wants to share my knowledge and understanding with you.