asp.net single sign-on in asp.net
For some reason, there is a situation in our application where a user can log in only one place, which is what we normally call a single sign-on. In the asp.net to achieve a single sign-on is very simple, the following is the main method and all the code analysis.
Realize the idea
Using the cache function, we put the user's login information in the cache, and set the expiration time for the session expiration time, so, once the session is invalid, our cache also expired, and cache for all users can access, therefore, It is easier to save user information with it than the database.
View Sample
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 ">
<meta http-equiv= "Author" content= "Mencius E Chapter" >
<meta http-equiv= "WebSite" content= "http://dotnet.aspx.cc/" >
<style>
H3 {font:17px Song body}
INPUT {font:12px Song body}
SPAN {font:12px Song body}
P {font:12px Song body}
H4 {font:12px Song body}
</style>
</HEAD>
<body ms_positioning= "GridLayout" >
<form id= "Form1" method= "POST" runat= "Server" >
<div align= "center" >
<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 description of the singlelogin.
Implement single sign-on
</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)
{
Actual examples are accessible:
Http://dotnet.aspx.cc/Exam/SingleLogin.aspx
}
Code generated #region the Web forms Designer
Override protected void OnInit (EventArgs e)
{
InitializeComponent ();
Base. OnInit (e);
}
<summary>
Designer supports required methods-do not use the Code editor to modify
The contents 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)
{
Keys, which are uniquely identified, should be unique, which allows you to set the rules yourself.
As a test, this is done with a combination of username and password and no other error checking.
Generate key
String SKey = Username.text + "_" + password.text;
Gets the value of the given key in the cache
String suser = Convert.ToString (Cache[skey]);
Check to see if there is
if (suser = null | | suser = = String.Empty)
{
There is no item in the cache for the key, the table name user is not logged in, or the login timeout has expired
Note the TimeSpan constructor overloaded version used below is the key to making a login decision.
TimeSpan sesstimeout = new TimeSpan (0,0,system.web.httpcontext.current.session.timeout,0,0);
HttpContext.Current.Cache.Insert (Skey,skey,null,datetime.maxvalue,sesstimeout,
System.web.caching.cacheitempriority.notremovable,null);
session["User"] = SKey;
The first time you sign in, you can do what you want to do.
msg.text= "
Msg.text + = "</A> I wish you a pleasant visit!" :) }
Else
{
The user's record is found in the Cache, the table name is already logged in, and the login is disabled
msg.text= "
Return
}
}
}
}