Authentication is used for identity authentication and login with Forms,

Source: Internet
Author: User

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 );

 

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.