Login Form in MVC

Login in mvc

In model

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel.DataAnnotations;  
  4. using System.Globalization;  
  5. using System.Web.Mvc;  
  6. using System.Web.Security;  
  7. using System.Data;  
  8. using System.Data.SqlClient;  
  9. using System.Linq;  
  10. using contestmangement.Models;  
  11. namespace contestmangement.Areas.Admin.Models  
  12. {  
  13.     public class LogOnModel  
  14.     {  
  15.         [Required]  
  16.         [Display(Name = "User name")]  
  17.         public string UserName {get;set;}  
  18.         [Required]  
  19.         [DataType(DataType.Password)]  
  20.         [Display(Name = "Password")]  
  21.         public string Password {get;set;}  
  22.         [Display(Name = "Remember me?")]  
  23.         public bool RememberMe {get;set;}  
  24.         private dbs_contestmanagementEntities db = new dbs_contestmanagementEntities();  
  25.         public bool IsValid(string _username, string _pwd)  
  26.         {  
  27.             IEnumerable < con_admin > enumerable = db.con_admin.Where(m = > m.adm_username == _username && m.adm_password == _pwd);  
  28.             List < con_admin > user = enumerable.ToList < con_admin > ();  
  29.             if (user.Count > 0)  
  30.             {  
  31.                 return true;  
  32.             }  
  33.             return false;  
  34.         }  
  35.     }  
  36. }  

 

Then in login controller

  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Web;  
  5. using System.Web.Mvc;  
  6. using System.Web.Routing;  
  7. using System.Web.Security;  
  8. using contestmangement.Areas.Admin.Models;  
  9. namespace contestmangement.Areas.Admin.Controllers  
  10. {  
  11.     public class AdminLoginController: Controller  
  12.     {  
  13.         //  
  14.         // GET: /Admin/AdminLogin/  
  15.         public ActionResult LogOn()  
  16.         {  
  17.             return View();  
  18.         }  
  19.         //  
  20.         // POST: /Account/LogOn  
  21.         [HttpPost]  
  22.         public ActionResult LogOn(LogOnModel model, string returnUrl)  
  23.         {  
  24.             if (ModelState.IsValid)  
  25.             {  
  26.                 if (model.IsValid(model.UserName, model.Password))  
  27.                 {  
  28.                     FormsAuthentication.SetAuthCookie(model.UserName, model.RememberMe);  
  29.                     if (Url.IsLocalUrl(returnUrl) && returnUrl.Length > 1 && returnUrl.StartsWith("/")  
  30.                     && !returnUrl.StartsWith("//") && !returnUrl.StartsWith("/\\"))  
  31.                     {  
  32.                         return Redirect(returnUrl);  
  33.                     }   
  34.                     else  
  35.                     {  
  36.                         return RedirectToAction("Index""admin");  
  37.                     }  
  38.                 }  
  39.                 else  
  40.                 {  
  41.                     ModelState.AddModelError("""The user name or password provided is incorrect.");  
  42.                 }  
  43.             }  
  44.             // If we got this far, something failed, redisplay form  
  45.             return View(model);  
  46.         }  
  47.         //  
  48.         // GET: /Account/LogOff  
  49.         public ActionResult LogOff()  
  50.         {  
  51.             FormsAuthentication.SignOut();  
  52.             return RedirectToAction("Index""Home");  
  53.         }  
  54.         #region Status Codes  
  55.         private static string ErrorCodeToString(MembershipCreateStatus createStatus)  
  56.         {  
  57.             // See http://go.microsoft.com/fwlink/?LinkID=177550 for  
  58.             // a full list of status codes.  
  59.             switch (createStatus)  
  60.             {  
  61.                 case MembershipCreateStatus.DuplicateUserName:  
  62.                     return "User name already exists. Please enter a different user name.";  
  63.                 case MembershipCreateStatus.DuplicateEmail:  
  64.                     return "A user name for that e-mail address already exists. Please enter a different e-mail address.";  
  65.                 case MembershipCreateStatus.InvalidPassword:  
  66.                     return "The password provided is invalid. Please enter a valid password value.";  
  67.                 case MembershipCreateStatus.InvalidEmail:  
  68.                     return "The e-mail address provided is invalid. Please check the value and try again.";  
  69.                 case MembershipCreateStatus.InvalidAnswer:  
  70.                     return "The password retrieval answer provided is invalid. Please check the value and try again.";  
  71.                 case MembershipCreateStatus.InvalidQuestion:  
  72.                     return "The password retrieval question provided is invalid. Please check the value and try again.";  
  73.                 case MembershipCreateStatus.InvalidUserName:  
  74.                     return "The user name provided is invalid. Please check the value and try again.";  
  75.                 case MembershipCreateStatus.ProviderError:  
  76.                     return "The authentication provider returned an error. Please verify your entry and try again. If the problem persists, please contact your system administrator.";  
  77.                 case MembershipCreateStatus.UserRejected:  
  78.                     return "The user creation request has been canceled. Please verify your entry and try again. If the problem persists, please contact your system administrator.";  
  79.                 default:  
  80.                     return "An unknown error occurred. Please verify your entry and try again. If the problem persists, please contact your system administrator.";  
  81.             }  
  82.         }  
  83.         #endregion  
  84.     }  
  85. }  

 

Simple LINQ Query with session

  1. string User;  
  2. User = Session["username"].ToString();  
  3. var teamDetails = from m in db.con_teamdetails where m.tea_teamname == User orderby m.tea_rank select m;  
  4. return View(teamDetails.ToList());