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
BSA Air décrypté : tout ce que votre start-up doit savoir
How do I convert my money to another currency in PayPal?
Lengua With A Tilde Crossword
Truist Bank Near Here
Pixel Speedrun Unblocked 76
Places 5 Hours Away From Me
Stretchmark Camouflage Highland Park
Best Team In 2K23 Myteam
Mychart Mercy Lutherville
Federal Fusion 308 165 Grain Ballistics Chart
Mohawkind Docagent
My Vidant Chart
Shuiby aslam - ForeverMissed.com Online Memorials
Mens Standard 7 Inch Printed Chappy Swim Trunks, Sardines Peachy
Discover Westchester's Top Towns — And What Makes Them So Unique
Shreveport Active 911
Craigslist Free Stuff Greensboro Nc
Po Box 35691 Canton Oh
Sound Of Freedom Showtimes Near Cinelux Almaden Cafe & Lounge
Average Salary in Philippines in 2024 - Timeular
St Maries Idaho Craigslist
Honda cb750 cbx z1 Kawasaki kz900 h2 kz 900 Harley Davidson BMW Indian - wanted - by dealer - sale - craigslist
Missed Connections Dayton Ohio
Effingham Bookings Florence Sc
Recap: Noah Syndergaard earns his first L.A. win as Dodgers sweep Cardinals
Weve Got You Surrounded Meme
Tokyo Spa Memphis Reviews
Dexter Gomovies
Big Boobs Indian Photos
Kacey King Ranch
Dubois County Barter Page
Craigslist Free Puppy
Teenbeautyfitness
#scandalous stars | astrognossienne
Craigslist Com Humboldt
Craigslist Hamilton Al
Www Violationinfo Com Login New Orleans
The Best Carry-On Suitcases 2024, Tested and Reviewed by Travel Editors | SmarterTravel
Best Workers Compensation Lawyer Hill & Moin
Laff Tv Passport
Housing Intranet Unt
Cygenoth
Lake Andes Buy Sell Trade
Unveiling Gali_gool Leaks: Discoveries And Insights
Ssc South Carolina
Sea Guini Dress Code
Waco.craigslist
The Jazz Scene: Queen Clarinet: Interview with Doreen Ketchens – International Clarinet Association
FactoryEye | Enabling data-driven smart manufacturing
Image Mate Orange County
Lsreg Att
Mazda 3 Depreciation
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.