MVC User Login verification and permission check (form authentication)

Source: Internet
Author: User
Tags md5 encryption

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.