Specific introduction to how to use forms validation in. NET MVC

Source: Internet
Author: User
Tags ticket
This article is mainly for you to introduce in detail the. NET MVC uses forms to verify the relevant data, with a certain reference value, interested in small partners can refer to

Forms validation is used in. NET MVC for your reference, as follows

The branch of the folder is like this

First set up 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= "?" /> <!--deny anonymous access-  </authorization>  <compilation debug= "true" targetframework= "4.5"/>  

If there is no need for anonymous access in the login folder, or if there is a way to access the Logincontroller in addition to logging in,

Then we need to add this one node

<location path= "Login" > <!--here means that the method under Logincontroller can be accessed anonymously--  <system.web>   < authorization>    <allow users= "*"/> <!--allow Anonymous access-   </authorization>  </ System.web> </location>

Login method to post a part of the code, for reference only

 public bool ValidateUser (Loginvo model) {string Encodepassword = MD5 (model. PassWord);//encrypt String sql = "SELECT * from User_users where (username= @UserName or jobnumber= @JobNumber) and P      Assword= @PassWord "; var user = context.data.query<userspo> (sql, new {UserName = model. LoginName, Jobnumber = model. LoginName, PassWord = Encodepassword}).      Singleordefault ();      if (user = = null) return false; DateTime expiration = model. Isrememberlogin//Do you remember the password?      DateTime.Now.AddDays (+): DateTime.Now.Add (formsauthentication.timeout); var ticket=new formsauthenticationticket (1,//specified version number: User can be specified arbitrarily.) username,//Login user name: corresponding to Web. config <allow users= "Admin" .../> users property DateTime.Now,//Publish time expiration, Expiration Time true,//is the persistent Cookie user. Userid.tostring (),//User data: Available ((System.Web.Security.FormsIdentity) (HttpContext.Current.User.Identity)). Ticket.userdata get Formsauthentication.formscookiepath//refer to<forms path= "/" .../> path property, default to "/" if not specified);      var encryptedticket = Formsauthentication.encrypt (ticket); if (httpcontext.current.request.cookies[formsauthentication.formscookiename]! = null) {HttpContext.Current.Re Quest.      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 a cookie to return true; }//<summary>//encryption///</summary>//<param name= "str" ></param>//<param Name= "Encoding" ></param>//<param name= "ToUpper" ></param>//<param name= "Isreverse" >&   lt;/param>//<param name= "Count" ></param>///<returns></returns> private string MD5 (String str, Encoding encoding=null, int count = 1) {if (Encoding = = null) {Encod      ing = 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.