Implementation of single point of login (SSO) for ASP. NET sites across subdomains

Source: Internet
Author: User

In MSDN's document "configure Forms authentication (http://msdn2.microsoft.com/zh-CN/library/eb0zx8fc.aspx) across applications", a method of implementing shared identity login information between Web Farm and multiple applications is proposed. This method implements identity sharing in the field environment. For single-point login across sub-domain names, such as the implementation of Web and CSDN passes, many friends have provided solutions. For details, refer: http://www.cnblogs.com/dudu/archive/2005/07/04/186279.html

Form authentication is based on identity cookies. After logging on to the client, a cookie containing user identity information (including a ticket) is generated. The cookie name is the name set by form in the Authentication section of web. config, as shown in

<Authentication mode = "Forms">
<Forms loginUrl = "login. aspx" name = ". ASPXAUTH" path = "/" protection = "All"> </forms>
</Authentication>

Here,. ASPNETAUTH is the Cookie name. This cookie is included in the Request. Cookies set to transmit user identity information. Therefore, the idea of sharing authentication information is simple: as long as this authentication cookie can be shared in the Self-domain name, the Form authentication information can naturally be shared!

There are many articles sharing cookies online. The basic practice is to set the domain attribute of the Cookie. The domain of the cookie specifies the domain associated with the cookie. Domain is String. Empty by default, indicating that the associated domain is the domain corresponding to the current Request. If a subdomain name is set for domain, such as cookie. Domain = "brookes.com", this cookie is associated with all subordinate domains under brookes.com. Therefore, it can be shared by www.brookes.com/web2.brookes.com.

At this point, the method for sharing Form verification information across subdomains is simple:

If (Membership. ValidateUser (userName, password ))
...{
FormsAuthentication. SetAuthCookie (userName. Text, false );
HttpCookie cookie = Response. Cookies [FormsAuthentication. FormsCookieName];
Cookie. Domain = ".brookes.com ";
Response. Cookies. Add (cookie );
FormsAuthentication. RedirectFromLoginPage (userName, false );
}

This Code illustrates the implementation principle. Here, the process of self-Writing Form Verification is implemented. If you are using the Login control and the framework performs verification on its own, how do you set the domain of this cookie?

There are three methods:

1. Process in the Login. OnLoggedIn event. This event is triggered after the user passes authentication. If the cookie already exists, you can modify the domain attribute. For more information about the code, see the preceding section;
2. Write the process of verifying the user and setting the AuthCookie into an HttpMoudle. This method is slightly responsible for google Code
3. There is one of the simplest methods. In. net 2.0, the Authticainon forms element adds a new attribute: domain. This attribute corresponds to the domain attribute of form AuthCookie. Therefore, you only need to make the following settings in the web. config of each subdomain:

<Authentication mode = "Forms">
<Forms loginUrl = "login. aspx" name = ". ASPXAUTH" path = "/" protection = "All" domain = "brookes.com"> </forms>
</Authentication>

OK. Now you don't need to make any other settings. All subdomains under your brookes.com can share form authentication information!

Several points should also be noted:
1. This domain attribute will overwrite the domain attribute setting in the httpCookie configuration section, but it will only affect AuthCookie, and other cookies will not be affected;
2. After reading the above article, I will certainly think that I can set the httpCookies configuration section, for example:
<HttpCookies domain = "brookes.com"/>
The results are the same. The difference is that httpCookies specify the domain attribute of all cookies on the site. This will allow all cookies on the site to be shared across subdomains! Whether or not to avoid such sharing is necessary, and you can determine based on your needs.
3. This domain attribute is highlighted in some documents by adding a point in front, such as domain = ".brookes.com", which is not found in MS documents. Based on my test results, there is no difference between the two statements, and the results are the same.

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.