Login Form Using MVC Razor

The following is the procedure.
 
Step 1: Create Table
  1. Create table userreg (id int identity(1,1),username varchar(50),password varchar(50))  
  2.   
  3. insert into userreg(username ,passwordvalues('knk','knk')
create table
 
Step 2: Create a project

Go to File, then New and click Project. Select ASP.NET MVC 4 Web Application and enter the project name, then click OK, select Empty, select View Engine Razor and press OK.

Step 3: Add model
  1. public class user {  
  2.     [Required(ErrorMessage = "Please Provide Username", AllowEmptyStrings = false)]  
  3.     public string Username {  
  4.         get;  
  5.         set;  
  6.     }  
  7.     [Required(ErrorMessage = "Please provide password", AllowEmptyStrings = false)]  
  8.     [DataType(System.ComponentModel.DataAnnotations.DataType.Password)]  
  9.     public string Password {  
  10.         get;  
  11.         set;  
  12.     }  

Step 4: Add Entity Data Model

To add an Entity Data Model, go to Solution Explorer, right-click on the Project name from Solution Explore, add a new item and select ADO.Net Entity Data Model under data. Now enter the model name and Add it. A dialog window will be shown (Entity Data Model Wizard), select Generate from database and click Next. Now choose your data connection, select your database and press next. Select tables and enter the Model namespace, then click Finish.

add new item

 
 
navigate properties

Step 5: Add a controller
 
Then add a controller as in the following:
  1. public ActionResult login() {  
  2.     return View();  
  3. }  
  4. [HttpPost]  
  5. [ValidateAntiForgeryToken]  
  6. public ActionResult login(userreg u) {  
  7.     if (ModelState.IsValid) {  
  8.         using(RBACEntities db = new RBACEntities()) {  
  9.             var v = db.userregs.Where(a = > a.username.Equals(u.username) && a.password.Equals(u.password)).FirstOrDefault();  
  10.             if (v != null) {  
  11.                 Session["log"] = v.username.ToString();  
  12.                 return RedirectToAction("AfterLogin");  
  13.             }  
  14.         }  
  15.     }  
  16.   
  17.     return View(u);  

Step 6: Add a view
 
Then add a view as in the following:

add view
  1. @model MvcApplication1.userreg  
  2. @{  
  3.    ViewBag.Title = "login";  
  4. }  
  5.   
  6.   
  7. <h2>login</h2>  
  8. @using (Html.BeginForm("Login""Login", FormMethod.Post))  
  9. {  
  10.    //this is for create form tag  
  11.    @Html.AntiForgeryToken() // this is for prevent CSRF attack  
  12.    @Html.ValidationSummary(true)  
  13.    if (@ViewBag.Message != null)  
  14.    {  
  15.   
  16.       <div style="border:1px solid red">  
  17.          @ViewBag.Message  
  18.       </div>  
  19.    }  
  20.   
  21. <table>  
  22.     <tr>  
  23.         <td>@Html.LabelFor(a => a.username)</td>  
  24.         <td>@Html.TextBoxFor(a => a.username)</td>  
  25.         <td>@Html.ValidationMessageFor(a => a.username)</td>  
  26.     </tr>  
  27.     <tr>  
  28.         <td>  
  29. @Html.LabelFor(a => a.password)  
  30. </td>  
  31.         <td>  
  32. @Html.PasswordFor(a => a.password)  
  33. </td>  
  34.         <td>  
  35. @Html.ValidationMessageFor(a => a.password)  
  36. </td>  
  37.     </tr>  
  38.     <tr>  
  39.         <td></td>  
  40.         <td>  
  41.             <input type="submit" value="Login" />  
  42.         </td>  
  43.         <td></td>  
  44.     </tr>  
  45. </table>  

Step 7: Add controller action after login
 
Add a controller action for after the login as in the following:
  1. public ActionResult AfterLogin() {  
  2.     if (Session["log"] != null) {  
  3.         return View();  
  4.     } else {  
  5.         return RedirectToAction("login");  
  6.     }  

Step 8: Add view after login

Add a view for after the login as in the following:
  1. @{  
  2. ViewBag.Title = "AfterLogin";  
  3. }  
  4.   
  5.   
  6. <h2>After Login</h2>  
  7. @if (Session["log"] != null)  
  8. {   
  9.    <text>  
  10.       Welcome @Session["log"].ToString()  
  11.    </text>  
  12. }