1, configuration web.conf, using form authentication method
<system.web> <authentication mode= "None"/> <compilation debug= "true" targetframework= "4.6.1" /> <authentication mode= "Forms" > <form S loginurl= "~/login/index" timeout= "1440"/> </authentication>
2. Write user information after successful login verification
Landing
Public ActionResult Index (Loginviewmodel model)
{
if (db. User.count (x = X.account = = struser && X.password = = strpassword && x.enable = True) > 0)
{
User authentication Successful
Formsauthentication.setauthcookie (struser, model. Remember);
Return redirecttoaction ("Index", "Admin", new {pagesize = 10});
}
Else
{
return View ();
}
}
Exit Login
Public ActionResult Logout ()
{
FormsAuthentication.SignOut ();
Return redirecttoaction ("Index", "Login");
}
3, verify the login and permission check, you need to make all the landing page from Basecontroller derived
public class basecontroller: controller { /// <summary> /// overriding the base class before action is performed, unified login verification and page permission validation /// </summary > /// <param name= "FilterContext" ></param > protected override void onactionexecuting ( Actionexecutingcontext filtercontext) { base. OnActionExecuting (Filtercontext); //Login Verification Processing if (!filtercontext.httpcontext.user.identity.isauthenticated) { //not logged in response.redirect ("~/login/index"); } else { //login, action-level rights control processing string controllername = filtercontext.routedata.values["Controller"]. ToString (); String actionname = filtercontext.routedata.values["Action"]. ToString (); //According to Controllername and &NBSp ActionName permissions Check if () &NB Sp {} else &NBS P { //login, no privileges Response.Redirect ("~/login/index"); }& nbsp } } }
4, Password MD5 encryption
Public static string getmd5hash (string input) { Md5cryptoserviceprovider md5hasher = new md5cryptoserviceprovider (); byte[] data = md5hasher.computehash ( Encoding.Default.GetBytes (input)); Stringbuilder sbuilder = new stringbuilder (); for (Int i = 0; i < data. length; i++) { sbuilder.append (Data[i]. ToString ("X2")); &Nbsp; } return sbuilder.tostring (); }
MVC user Login verification and permission check (form authentication)