For some reason, a single user can only log on to one place in our application, which is also called Single Sign-On. Implementing Single Sign-On in ASP. NET is actually very simple. The main method and all the code are analyzed below.
Implementation
By using the Cache function, we store user login information in the Cache and set the expiration time to the Session expiration time. Therefore, once the Session fails, our Cache also expires; the Cache can be accessed by all users. Therefore, it is easier to use it to save user information than to use the database.
SingleLogin. aspx code
<% @ Page language = "c #" Codebehind = "SingleLogin. aspx. cs" AutoEventWireup = "false"
Inherits = "eMeng. Exam. SingleLogin" %>
<! Doctype html public "-// W3C // dtd html 4.0 Transitional // EN">
<HTML>
<HEAD>
<Title> Single Sign-On test </title>
<Meta http-equiv = "Content-Type" content = "text/html; charset = gb2312">
<Style>
H3 {FONT: 17px}
INPUT {FONT: 12px}
SPAN {FONT: 12px}
P {FONT: 12px}
H4 {FONT: 12px}
</Style>
</HEAD>
<Body MS_POSITIONING = "GridLayout">
<Form id = "Form1" method = "post" runat = "server">
<Div align = "center">
<H3> Single Sign-On test <P> User name: <asp: TextBox id = "UserName" runat = "server"> </asp: TextBox> </p>
<P> User PassWord: <asp: TextBox id = "Password" runat = "server" TextMode = "PassWord"> </asp: TextBox> </p>
<P> <asp: Button id = "Login" runat = "server" Text = "Login"> </asp: Button> </p>
<P> <asp: Label id = "Msg" runat = "server"> </asp: Label> </p>
</Div>
</Form>
</Body>
</HTML>
SingleLogin. aspx. cs code
Using System;
Using System. Collections;
Using System. ComponentModel;
Using System. Data;
Using System. Drawing;
Using System. Web;
Using System. Web. SessionState;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. HtmlControls;
Namespace eMeng. Exam
{
/// <Summary>
/// Summary of SingleLogin.
/// Implement Single-point Logon
/// </Summary>
Public class SingleLogin: System. Web. UI. Page
{
Protected System. Web. UI. WebControls. TextBox UserName;
Protected System. Web. UI. WebControls. TextBox PassWord;
Protected System. Web. UI. WebControls. Label Msg;
Protected System. Web. UI. WebControls. Button Login;
Private void Page_Load (object sender, System. EventArgs e)
{
}
# Code generated by region Web Form Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e );
}
/// <Summary>
/// The designer supports the required methods-do not use the code editor to modify
/// Content of this method.
/// </Summary>
Private void InitializeComponent ()
{
This. Login. Click + = new System. EventHandler (this. Login_Click );
This. Load + = new System. EventHandler (this. Page_Load );
}
# Endregion
Private void Login_Click (object sender, System. EventArgs e)
{
// The Key as the unique identifier should be unique, which can be set as needed.
// As a test. Here, the user name and password are combined for identification. No other error checks are performed.
// Generate the Key
String sKey = UserName. Text + "_" + PassWord. Text;
// Obtain the value of the given Key in the Cache
String sUser = Convert. ToString (Cache [sKey]);
// Check for existence
If (sUser = null | sUser = String. Empty)
{
// The project where the Key is not found in the Cache. The table name user is not logged on or has timed out.
// Note that the following method of using the TimeSpan constructor to overload the version is the key to determining whether to log on.
TimeSpan SessTimeOut = new TimeSpan (0, 0, System. Web. HttpContext. Current. Session. Timeout, 0 );
HttpContext. Current. Cache. Insert (sKey, sKey, null, DateTime. MaxValue, SessTimeOut,
System. Web. Caching. CacheItemPriority. NotRemovable, null );
Session ["User"] = sKey;
// Log on for the first time. You can do what you want.
Msg. Text = "
Msg. Text + = "</a>. Have a good time! :) </H4> ";
}
Else
{
// This user's record is found in the Cache. The table name has been logged on and cannot be logged on again.
Msg. Text = "
Return;
}
}
}
}