- Mudita Rathore
- Jan 05, 2021
- 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.
- From the "MVC4 Project" window select "Web API".
- 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#".
- Select "Class" and click on the "Add" button.
Now add the following code:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- namespaceMvcApplication6.Models
- {
- publicclassLoginModel
- {
- publicstringUserName{get;set;}
- publicstringUserPassword{get;set;}
- }
- }
Step 3
Now in the controller we add the following code:
- In the "Solution Explorer".
- Expand "Controller" folder.
- Select the "HomeController".
Add the following code:
- usingSystem;
- usingSystem.Collections.Generic;
- usingSystem.Linq;
- usingSystem.Web;
- usingSystem.Web.Mvc;
- usingMvcApplication6.Models;
- namespaceMvcApplication6.Controllers
- {
- publicclassHomeController:Controller
- {
- publicActionResultIndex()
- {
- LoginModelobj=newLoginModel();
- returnView(obj);
- }
- [HttpPost]
- publicActionResultIndex(LoginModelobjuserlogin)
- {
- vardisplay=Userloginvalues().Where(m=>m.UserName==objuserlogin.UserName&&m.UserPassword==objuserlogin.UserPassword).FirstOrDefault();
- if(display!=null)
- {
- ViewBag.Status="CORRECTUserNAmeandPassword";
- }
- else
- {
- ViewBag.Status="INCORRECTUserNameorPassword";
- }
- returnView(objuserlogin);
- }
- publicList<LoginModel>Userloginvalues()
- {
- List<LoginModel>objModel=newList<LoginModel>();
- objModel.Add(newLoginModel{UserName="user1",UserPassword="password1"});
- objModel.Add(newLoginModel{UserName="user2",UserPassword="password2"});
- objModel.Add(newLoginModel{UserName="user3",UserPassword="password3"});
- objModel.Add(newLoginModel{UserName="user4",UserPassword="password4"});
- objModel.Add(newLoginModel{UserName="user5",UserPassword="password5"});
- returnobjModel;
- }
- }
- }
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".
Add the following code:
- @modelMvcApplication6.Models.LoginModel
- @{
- ViewBag.Title="Index";
- }
- <scriptlanguage="javascript">
- functiondisplay(){
- if($("#txtusername").attr("value")=="")
- {
- alert("PleaseEnteryourUserName.");
- returnfalse;
- }elseif($("#txtuserpassword").attr("value")==""){
- alert("PleaseenterUserPassword.");
- returnfalse;
- }
- alert("LoginSuccessfully");
- returntrue;
- }
- </script>
- <h3>SimpleWeAPILoginForm</h3>
- @using(Html.BeginForm("Index","Home"))
- {
- <tablewidth="40%"cellpadding="1"cellspacing="5">
- <tr>
- <tdcolspan="2"style="color:#f00;font-size:larger">@ViewBag.Status</td>
- </tr>
- <tr>
- <tdalign="right">UserName:</td>
- <td>@Html.TextBoxFor(m=>m.UserName,new{@style="width:200px",@id="txtusername"})</td>
- </tr>
- <tr>
- <tdalign="right">UserPassword:</td>
- <td>@Html.PasswordFor(m=>m.UserPassword,new{@style="width:200px",@id="txtuserpassword"})</td>
- </tr>
- <tr>
- <tdcolspan="2">
- <inputtype="submit"value="Login"title="login"onclick="display();"/>
- </td>
- </tr>
- </table>
- }
Step 5
Execute the application. The output will be:
Enter the UserName and UserPassword declared in the controller. The output will be:
Now leavt the UserName TextBox empty; then its output looks as: