Authentication is used for identity authentication and login with Forms,
When logging on, you do not need to use your own logic to determine whether a user is logged on or not, so you can search the Internet and find that this method is still available. This method is very simple and practical. It is used for the first time, there are still many things you don't understand, so you can easily check the changes later.
To use this method, you need to understand the attributes and usage in it. There are a lot of features on the Internet. You can search for them by yourself, and I will mainly post the implementation code.
1. Add the <system. web> node in web. config
/* If the loginurl logon interface is not displayed, go to the next page. * name: cookie name. You can modify * defaurl URL: Default Page. After logon, go to the default page * timeOut: cookie retention time */<authentication mode = "Forms"> <forms loginUrl = "Login. aspx "name = ". ASPXFORMSAUTH "defaultUrl =" Index. aspx "timeout =" 600 "> </forms> </authentication>
2. Use FormsAuthentication. RedirectFromLoginPage to check whether the logon is successful.
String txtMail = Request ["txt_mail"]; string Com_Pwd = Request ["txt_pwd"]; DataTable msg = ComLogin (txtMail, Com_Pwd, true); if (msg. rows. count> 0) {Session ["name"] = msg. rows [0] [4]; Session ["gs"] = msg. rows [0] [0]; // Select Automatic Logon if (chkbox2.Checked ){
// If Automatic Logon is selected, the second parameter is true. The cookie is retained, and the cookie duration and web. the timeout time in config is the same as FormsAuthentication. redirectFromLoginPage (txtMail, true);} else {FormsAuthentication. redirectFromLoginPage (txtMail, false) ;}} else {Response. write ("<script> alert ('check input information') </script> ");}
There are two other methods to achieve the same effect.
FormsAuthentication.SetAuthCookie(txtMail,false);
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket (1, username, DateTime. now, DateTime. now. addMinutes (720), isPersistent, userData, FormsAuthentication. formsCookiePath); // encrypted ticket string encTicket = FormsAuthentication. encrypt (ticket); // create cookieHttpCookie cookie = new HttpCookie (FormsAuthentication. formsCookieName, encTicket); HttpContext. current. response. cookies. add (cookie );