Huilang note: this is an article I just saw.
Article It feels good, so I can't help but translate it. You can also make it easier. Even if the English level is limited, I still hope to criticize and correct it. (if it is not limited, it is very bad, and the brothers are very cool ~~~).
Original article: Understanding single sign-on in ASP. NET 2.0
UnderstandingASP. Network 2.0Single Sign-on in
Published:16 Jan 2008
Summary
In this article,MasoudDiscussed applicationsASP. NETUnified Identity Authentication Model for cross-applicationProgramVerification issues, including:Membership providers, Web. configConfiguration,Configuration file encryption and decryption. At the end of the article, the author providesASP. NET login controlsTo verify the program.
ByMasoud tabatabaei
Directory
:
- Introduction
- What isSSO? How does it work?
- System Conditions
- Work
- Download
- Conclusion
- Introduction
GenerallyASP. NET webFor application authentication, you need to create a logon page for each application. Imagine if you have two or moreWebApplication, you may want to display only one logon page for all associated applications through some mechanism. In this way, once you log on once, you can browse all associated programs without any additional logon. Single Sign-on (SSOThe access control mechanism allows a user to access all software system resources after one verification.
Imagine that you have created two or moreWebSite. Like otherWebThe site is the same, you just useASP. NETPermission verification mechanism to verify your users. Therefore, you may need one or more logon pages for these sites. Now you are trying to prove how to achieve cross-program login by changing your configuration. In other words, we only want to configure a login page for our program, and once the user passes the verification, he can browse all other sites without another login. In the appendix of this article, you can also see how to encrypt your configuration file.
What is single sign-on? How does it work?
in many companies, some of them are represented by Web sites or Web applications. layer system. Naturally, because of security issues, they will need to use Asp. NET 2.0 , membership provider and role provider or customizes the permission verification and permission verification systems. Whatever the case, by default, all sites have a id and password that are valid in the database "login. aspx "Web forms . when you only have one site or these sites are running independently, this is no problem. But when you have two or more sites that are associated or linked together, you may ask: Why do you have to log on to each application once? Why can't you implement verification with only one "login. aspx" and truly unify all unrelated programs. Fortunately, in Asp. NET 2.0 , you can use the same configuration to achieve cross-application access, whether it is your new site or an existing site.
On Asp.. Net Configuration File (web. config) has a configuration section (in < span> , encrypts and decrypts forms (these forms can read form permission verification cookies ) cookie data and View-state data for permission authentication, besides processes ( out-of-Process ) session Status ID. Therefore, once the user passes the verification, A cookie is saved to the local computer, other applications with the same configurations can also recognize this cookie valid Permission ticket. Therefore, you do not need to log on to other applications that have the same configuration.
Because<Machinekey>The information is sensitive. You need to encrypt the information in the configuration file. To achieve this goal, I will useConfigurationmanagerClass and its method. There is also a classSectioninformationContains metadata of a single configuration section in the configuration. There is a method in this classProtectsection ()To decrypt the configuration section of your configuration file.
System Conditions
·A Web server running on Windows 2000 or later
·. NET Framework 2.0
·Visual Studio 2005
·Microsoft SQL Server 2005 express Edition
Now let's take a look at what happened in our project. I have a website(Aspalliance1)The site contains a logon page."Login. aspx ".You can use this page for permission verification. There is also a page called"Default. aspx ",It hasHeaderAnd some text.Aspalliance2The link of the site. You will see that once this user logs on, he can navigate to other sites without the need for a second login. There is also a page with two buttons for encryption and decryption."Encryption. aspx"Used to encrypt and decrypt the configuration file.
As I said before, you canWebThe configuration file supports cross-application access with a little bit of configuration. InWeb. configFile, there is<System. Web>Configuration section. We will<System. Web>For the same configuration, you only need to set the configuration section<Machinekey>And put its value in<System. Web>Configuration section.<Machinekey>There are some attributes that I will configure. First, specify the encryption type used for verification.ValidationkeyDefinesKey,DecryptionkeyDefines the data used for encryption and decryption.Key, OrKeyGeneration process.
List1:ConfigurationWeb. configInMachinekey
<Machinekey
Validationkey= "282487e295028e59b8f411acb689ccd6f39ddd21e6055a3ee480417315994760adf
21b580d8587db675fa02f79167413044e25309cccdb647174d5b3d0dd9141"
Decryptionkey= "8b6697227cbca902b1a0925d40faa00bda-f2df4359d2099"
Validation= "Sha1"/>
This exampleCodeIt is not encrypted and will not be published to the server. For security reasons<Machinekey>Is very important. You can2See the encrypted<Machinekey>.
List2: Web. configEncryptedMachinekey
<MachinekeyConfigprotectionprovider= "Rsaprotectedconfigurationprovider">
<EncrypteddataType= "Http://www.w3.org/2001/04/xmlenc#Element"
Xmlns= "Http://www.w3.org/2001/04/xmlenc#">
<EncryptionmethodAlgorithm= "Http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<KeyinfoXmlns= "Http://www.w3.org/2000/09/xmldsig#">
<EncryptedkeyXmlns= "Http://www.w3.org/2001/04/xmlenc#">
<EncryptionmethodAlgorithm= "Http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
<KeyinfoXmlns= "Http://www.w3.org/2000/09/xmldsig#">
<Keyname>RSA key</Keyname>
</Keyinfo>
<Cipherdata>
<Ciphervalue>
Lm3mfpx/94zm3hgdbsmkiixbrwm14t3/ugxs40bfoahbiactwq3gvqusftofvuonvny01kgbceh10rveid
Djnz/8lubnocbhm8oljgplhvrt + rjc/lrpesjk2ni/jy2swkxlgejgsq1w5ne53gztg3s9hu + nk4owxnt
6z3v7am =
</Ciphervalue>
</Cipherdata>
</Encryptedkey>
</Keyinfo>
<Cipherdata>
<Ciphervalue>
Bceguv/dh1imbcm5vn0kn8nrd + Ex + kemenr7x + vekwt1zo6y5 + jryf4rdwmjcfj1jhc36 + mafcdhuxn0rp
B6hu5yutx9va5q5n0ngrs9aipg + 0ihuus3hdzqe3p6nli30m1h0pml1yjbovy0i6fbca6 + + gt2mdwclerk
+ Pvwmoq7p1q97n5pnznqhvkcx45lhs5ysvs + mjjxvetrcatftpvaucjlsncl2kmerzf5w/su3ableuy04w
Pipeline +
0 cefe/hj2chpyw =
</Ciphervalue>
</Cipherdata>
</Encrypteddata>
</Machinekey>
You can useConfiguration,SectioninformationTwo classes are used to encrypt your configuration file. To encrypt and decrypt your<Machinekey>Let's write some code.SectioninformationClass has a methodProtectsection (), You can get a descriptionProtection providerFor example"Rsaproctedconfigurationprovider"And encrypt the configuration section. Here is anotherBooleanType attributeForcesaveWhen you need to configureSaveMethod To save the configuration file, you need to set itTrue. Here we have"Encryption. aspx"Page code. The page contains two buttons to encrypt and decrypt the configuration file.
List3: WebConfiguration file encryption code
Protected VoidBtnencrypt_click (ObjectSender, eventargs E)
{
Try
{
Configuration config=Webconfigurationmanager. openwebconfiguration (
"/Aspalliance1");
Configurationsection machinekeysection=Config. getsection (
"System. Web/machinekey");
Machinekeysection. sectioninformation. protectsection (
"Rsaprotectedconfigurationprovider");
Machinekeysection. sectioninformation. forcesave= True;
Config. Save ();
Response. Write ("<H2 style = 'color: red'> encryption succeed </H2>");
}
Catch(Exception ex)
{
Response. Write ("<H2 style = 'color: red'> error while encrypting </H2> <br/>");
Response. Write (ex. Message );
}
}
List4: WebConfiguration File decryption code
Protected VoidBtndecrypt_click (ObjectSender, eventargs E)
{
Try
{
Configuration config=Webconfigurationmanager. openwebconfiguration (
"/Aspalliance1");
Configurationsection machinekeysection=Config. getsection (
"System. Web/machinekey");
Machinekeysection. sectioninformation. unprotectsection ();
Machinekeysection. sectioninformation. forcesave= True;
Config. Save ();
Response. Write ("<H2 style = 'color: red'> decryption succeed </H2>");
}
Catch(Exception ex)
{
Response. Write ("<H2 style = 'color: red'> error while decrypting </H2> <br/>");
Response. Write (ex. Message );
}
}
Now you must set the same configuration in this site. First, you need to changeLoginurlThis form will be used to redirect anonymous users"Login. aspx"Page. But now it will redirect the userAspalliance1In the site"Login. aspx"Page.
List5:SetWeb. configVerification Section in
<Authentication Mode= "Forms">
<Forms Loginurl= "Http: // localhost/aspalliance1/login. aspx"Name= ". Aspxauth"/>
</Authentication>
If you want to achieve cross-program login to many of your sites, the most important thing is that you must configure your two or more sites to the same<Machinekey>. So I just need to copy and pasteAspalliance1In the site<Machinekey>Configuration sectionAspalliance2Site. Now you are ready to test your website.
List6:SetWeb. configInMachinekey
<Machinekey
Validationkey= "282487e295028e59b8f411acb689ccd6f39ddd21e6055a3ee480417315994760adf
21b580d8587db675fa02f79167413044e25309cccdb647174d5b3d0dd9141"
Decryptionkey= "8b6697227cbca902b1a0925d40faa00bda-f2df4359d2099"
Validation= "Sha1"/>
[Download]
To test this site, you can use the User Name:AdminPassword:123456 &To log on.
This download attachment containsVS 2005Project, which contains two sites:Aspalliance1 and aspalliance2.
To install this instance, you need to create twoIISThe virtual directory is named as follows:Aspalliance1AndAspalliance2And point the address to the corresponding folder. You can alsoVisual Studio 2005Open the site.
It is troublesome for a user to log on to multiple sites. Therefore, it would be great if you only log on once. To achieve this, you just need"Web. config"Add files with the same value<Machinekey>Configuration. For security considerations, I suggest you encrypt this configuration section. The encryption method isSectioninformationClassProtectsection ()Method is overwritten.
At the end of the article, I will attach a comment from the blog Hall Xia Baoyu: Supplement to the article Understanding single sign-on in ASP. NET 2.0.