. Net MVC uses forms authentication and. netmvcforms Authentication

Source: Internet
Author: User

. Net MVC uses forms authentication and. netmvcforms Authentication

The folder branch is like this.

First set in Web. config

Authentication and authorization nodes
<System. web> <authentication mode = "Forms"> <forms loginUrl = "~ /Login/Index "timeout =" 2880 "defaultUrl = "~ /Home/Index "/> </authentication> <anonymousIdentification enabled =" true "/> <authorization> <deny users = "? "/> <! -- Reject anonymous access --> </authorization> <compilation debug = "true" targetFramework = "4.5"/> 

If you do not need anonymous access in the Login folder, or you can use anonymous access in addition to Login in LoginController,

Then we need to add this node

<Location path = "Login"> <! -- The method in LoginController can be accessed anonymously --> <system. web> <authorization> <allow users = "*"/> <! -- Allow anonymous access --> </authorization> </system. web> </location>

Post a part of the code in the login Method for your reference only

Public bool ValidateUser (LoginVO model) {string encodePassword = MD5 (model. passWord); // encrypted string SQL = "select * from User_Users where (UserName = @ UserName or JobNumber = @ JobNumber) and PassWord = @ PassWord"; var user = Context. data. query <UsersPO> (SQL, new {UserName = model. loginName, JobNumber = model. loginName, PassWord = encodePassword }). singleOrDefault (); if (user = null) return false; DateTime e Xpiration = model. IsRememberLogin // do you want to remember the password? DateTime. now. addDays (14): DateTime. now. add (FormsAuthentication. timeout); var ticket = new FormsAuthenticationTicket (1, // specify the version number: user can be specified at will. userName, // logon UserName: corresponding to Web. <allow users = "Admin "... /> Users attribute DateTime. now, // release time expiration, // The expiration time is true, // whether it is a persistent Cookie user. userId. toString (), // user data: available (System. web. security. formsIdentity) (HttpContext. current. user. identity )). ticket. userData obtains FormsAuthentication. formsCookiePath // specify the Cookie as Web. <forms path = "/"... /> Path attribute. The default value is "/" if not specified.); var encryptedTicket = FormsAuthentication. Encrypt (ticket); if (HttpContext. Current. Request. Cookies [FormsAuthentication. FormsCookieName]! = Null) {HttpContext. current. request. cookies. remove (FormsAuthentication. formsCookieName);} var loginIdentify = new HttpCookie (FormsAuthentication. formsCookieName); if (model. isRememberLogin) {loginIdentify. expires = DateTime. now. addDays (7);} loginIdentify. value = encryptedTicket; HttpContext. current. response. appendCookie (loginIdentify); // Add Cookie return true ;} /// <summary> /// encryption /// </summary> /// <param name = "str"> </param> /// <param name =" encoding "> </param> // <param name =" toUpper "> </param> // <param name =" isReverse "> </param> /// <param name = "count"> </param> // <returns> </returns> private string MD5 (string str, encoding encoding = null, int count = 1) {if (encoding = null) {encoding = Encoding. default;} var bytes = new MD5CryptoServiceProvider (). computeHash (encoding. getBytes (str); var md5 = string. empty; for (int I = 0; I <bytes. length; I ++) {md5 + = bytes [I]. toString ("x "). padLeft (2, '0');} if (count <= 1) {return md5;} return MD5 (md5, encoding, -- count );}


Related Article

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.