In ASP. NET, how does one use SESSION to determine whether a user is logged on? (
Source: Internet
Author: User
The code is very simple. I have posted what I have written in ordinary times for you to see:
If (bResult = true) // The logon username and password are correct.
{
// Save the login user name
Session ["LoginUser"] = FormatString. Replace (txtLoginUser. Text); // The value is assigned to the session. I have performed some security operations on the logged-on user.
// Go to the management homepage
Response. Redirect ("Admin_Index.Aspx ");
}
Else
{
LblErr. Text = Message. Show ("incorrect user name or password! ");
ShowValidate (); // re-display the verification code
}
Then how can I determine whether to log on to the page? The code is simpler, as follows:
If (Session ["LoginUser"] = null) // The above Session Name uses LoginUser. The name here must be the same as the above one. You can change it as needed to keep it consistent, note: There is no ToString () after Session ["LoginUser"]. If you add it, an error will occur.
{
Response. Redirect ("Login. Aspx"); // if the Session is empty, go to the Login page.
}
The Session may be lost. For the solution, see another article I wrote:
Solution to Session loss after login
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