Detailed explanation of ASP. net2.0 authentication cookie

Source: Internet
Author: User

I am very familiar with ASP. NET Forms authentication. However, when controlling the user's (expiration time) expired time, have you ever encountered some strange phenomena? Although it is just a small cookie, you may not know a lot about it. Today, I will discuss in detail the notes on cookies.

In ASP. NET Forms authentication, we usually use the login control that comes with ASP. NET for verification. In the Web. config file, all forms settings are set as default. Now, the problem is coming.

1) Why did I log out in about half an hour after I clicked "remember me?

2) Why did I set timeout to e.g. 400000 for an indefinite period? Why did I log out again one or two days later?

 

This is a lot of issues encountered in Forms authentication. Next, I will give a detailed explanation of these two questions:

For question 1, I want to clarify the differences between ticket and cookie. A cookie is a container used to store things. It is stored on the client. Ticket is the specific data used to indicate the specific authentication information, which is placed in the cookie container. Therefore, the following happened during our verification. First, ticket is created, which contains the username and other information, and has an expiration time. Then, the cookie is created, and it also has an expiration time. Finally, save ticket in the cookie and send the cookie to the client's browser. After reading this, I think the problem is quite clear. The user's log out is due to the expiration time. But who has expired? In our ASP. net web. in config settings, timeout is the cookie expiration time (note that the default value is 30 minutes), while ticket has an unlimited expiration time because we chose "remember me ". This is why I clicked "Remember Me", but after about 30 minutes, I was still logged out because we didn't set the cookie timeout. Ticket and cookie, as long as one of them does not never expire, we cannot never expire.

When we solve problem 1 (if we manually set timeout = "4000000"), we encountered problem 2 again. What is the reason for this? This should start with ticket's encryption and decryption mechanism. ASP. NET uses a machinekey to encrypt cookies. This machinekey is randomly generated at application startup by default. ASP. NET then uses the same machinekey for Cookie decryption. This is because the key is randomly generated during application startup, which leads to problem 2. Imagine what if application recycle is restarted? ASP. NET will generate another key for decryption, and the previous cookie will no longer be valid, which is the cause of problem 2. With this in mind, the solution to the second problem is simple. manually set a specific key. For example:

Code
<MachinekeyValidationkey= "88cb6ca6cf403c5fbb41c2f62bb7fcfca05de7be"Decryptionkey= "B8a7cf3816425176"Validation= "Sha1" />

 

P.s. There are some sites on the internet that randomly generate keys, such as: http://www.aspnetresources.com/tools/keycreator.aspx.

Are you familiar with forms authentication cookies?

Have a nice day!

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.