top
Loading...
單點登錄在ASP.NET上的簡單實現




1、Shop的Web.config和Project.cs

在Shop的Web.config里,我們配置了Service站點和Shop站點,以方便我們在部署時方便修改。

<appsettings>
<add key="Service" value="http://localhost:8001" />
<add key="WebSite" value="http://localhost:8002" />
</appsettings>

在Project類里進行引用。

using System;
using System.Configuration;

namespace Amethysture.SSO.Shop
{
public class Project
{
public static string Service = ConfigurationSettings.AppSettings["Service"];
public static string WebSite = ConfigurationSettings.AppSettings["WebSite"];
}
}

2、Shop的Global.cs

Shop的Global.cs定義了四個Session變量,UserID用來標識用戶身份。Pass標識用戶即時狀態,Security用于保存往來Service和Shop的通訊不是被仿冒的。Url保存了上次請求的頁面,以保證在用戶登錄后能轉到用戶請求的頁面。

protected void Session_Start(Object sender, EventArgs e)
{
this.Session.Add("UserID", 0);
this.Session.Add("Pass", false);
this.Session.Add("Security", "");
this.Session.Add("Url", "");
}

3、Shop的Any.cs

Shop的Any.cs并沒有包含代碼,因為Any類從Page繼承而來,為了代碼分析方便,我們將代碼集中到Page.cs中。

using System;
using System.Web;

namespace Amethysture.SSO.Shop
{
public class Any : Amethysture.SSO.Shop.Page
{
}
}

4、Shop的Page.cs

Page類有兩個方法,CustomerValidate和Initialize。CustomerValidate用戶檢查用戶的即時狀態,而Initialize是頁面登錄后發送給用戶的信息。我們的重點是CustomerValidate。

CustomerValidate是一個非常簡單的流程,用條件語句檢查Pass的狀態,如果Pass為否,則表示用戶沒有登錄,頁面跳轉到Service的Validate頁面中。我們要分析的是其中保存的Url和遞交的WebSite和Security幾個參數。Url的作用在前面已經講清楚了,只是為了保證用戶登錄后能回到原來的頁面。而WebSite是為了保證該站點是被Service所接受的,并且保證Service知道是哪個站點請求了用戶即時狀態。因為這個例子是個簡單的例子,在后面的Validate里沒有驗證WebSite是否是接受的請求站點,但是在實際應用中應該驗證這一點,因為Shop和Service等同于服務器和客戶端,服務器出于安全考慮必須要檢查客戶端是否是被允許的。Security是非常重要的一點。Shop對Service發送的是請求,不需要保證該請求沒有被篡改,但是在Service應答Shop請求時就必須要保證應答的數據沒有被篡改了。Security正是為了保證數據安全而設計的。

在代碼中,Security是通過Hash一個隨機產生的數字生成的。具有不確定性。和保密性。我們可以看到,Security同時保存在Session中和發送給Service。我們把這個Security當作明文。在后面我們可以看到,Security在Service經過再一次Hash后作為密文發送回Shop。如果我們將Session保存的Security經過同樣的Hash方法處理后等到的字符串如果和Service返回的密文相同,我們就能夠在一定程度上保證Service應答的數據是沒有經過修改的。

using System;
using System.Web;
using System.Security.Cryptography;
using System.Text;

namespace Amethysture.SSO.Shop
{
public class Page : System.Web.UI.Page
{
private void CustomerValidate()
{
bool Pass = (bool) this.Session["Pass"];
if (!Pass)
{
string Security = "";
Random Seed = new Random();
Security = Seed.Next(1, int.MaxValue).ToString();
byte[] Value;
UnicodeEncoding Code = new UnicodeEncoding();
byte[] Message = Code.GetBytes(Security);
SHA512Managed Arithmetic = new SHA512Managed();
Value = Arithmetic.ComputeHash(Message);
Security = "";
foreach(byte o in Value)
{
Security += (int) o + "O";
}
this.Session["Security"] = Security;
this.Session["Url"] = this.Request.RawUrl;
this.Response.Redirect(Project.Service + "/Validate.aspx?WebSite=" + Project.WebSite + "&Security=" + Security);
}
}

protected virtual void Initialize()
{
this.Response.Write("<html>");
this.Response.Write("<head>");
this.Response.Write("<title>Amethysture SSO Project</title>");
this.Response.Write("<link rel=stylesheet type="text/css" href="" + project.website + "/Default.css">");
this.Response.Write("</head>");
this.Response.Write("<body>");
this.Response.Write("<iframe width="0" height="0" src="" + project.service + "/Customer.aspx"></iframe>");
this.Response.Write("<div align="center">");
this.Response.Write("Amethysture SSO Shop Any Page");
this.Response.Write("</div>");
this.Response.Write("</body>");
this.Response.Write("</html>");
}

protected override void OnInit(EventArgs e)
{
base.OnInit(e);
this.CustomerValidate();
this.Initialize();
this.Response.End();
}
}
}

5、Service的Global.cs

現在我們頁面轉到了Service的Validate頁面,我們轉過來看Service的代碼。在Global中我們同樣定義了四個Session變量,都和Shop的Session用處類似。WebSite是保存請求用戶即時狀態的站點信息。以便能在登錄后返回正確的請求站點。

protected void Session_Start(Object sender, EventArgs e)
{
this.Session.Add("UserID", 0);
this.Session.Add("Pass", false);
this.Session.Add("WebSite", "");
this.Session.Add("Security", "");
}

6、Service的Validate.cs

首先,將Shop傳遞過來的參數保存到Session中。如果用戶沒有登錄,則轉到Customer頁面進行登錄。如果用戶已經登錄了。則將用戶即時狀態傳回給Shop站點。如上所述,這里將Security重新Hash了一次傳回給Shop,以保證數據不被纂改。

private void CustomerValidate()
{
bool Pass = (bool) this.Session["Pass"];
if ((this.Request.QueryString["WebSite"] != null) && (this.Request.QueryString["WebSite"] != ""))
{
this.Session["WebSite"] = this.Request.QueryString["WebSite"];
}
if ((this.Request.QueryString["Security"] != null) && (this.Request.QueryString["Security"] != ""))
{
this.Session["Security"] = this.Request.QueryString["Security"];
}
if (Pass)
{
string UserID = this.Session["UserID"].ToString();
string WebSite = this.Session["WebSite"].ToString();
string Security = this.Session["Security"].ToString();
byte[] Value;
UnicodeEncoding Code = new UnicodeEncoding();
byte[] Message = Code.GetBytes(Security);
SHA512Managed Arithmetic = new SHA512Managed();
Value = Arithmetic.ComputeHash(Message);
Security = "";
foreach(byte o in Value)
{
Security += (int) o + "O";
}
this.Response.Redirect(WebSite + "/Synchronous.aspx?UserID=" + UserID + "&Pass=True&Security=" + Security);
}
else
{
this.Response.Redirect("Customer.aspx");
}
}

7、Service的Customer.cs和Login.cs

Customer主要的是一個用于登錄的表單,這里就不貼出代碼了。這里分析一下Login的一段代碼,這段代碼是當登錄是直接在Service完成的(WebSite為空值),則頁面不會轉到Shop或Office站點。所以應該暫停在Service站點。系統如果比較完美,這里應該顯示一組字系統的轉向鏈接。下面我們看到,當Pass為真時,頁面轉回到Validate頁面,通過上面的分析,我們知道,頁面會轉向Shop的Synchronous頁面,進行用戶狀態的同步。

if (Pass)
{
if ((this.Session["WebSite"].ToString() != "") && (this.Session["Security"].ToString() != ""))
{
this.Response.Redirect("Validate.aspx");
}
else
{
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("Pass");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
}
}
else
{
this.Response.Redirect("Customer.aspx");
}

8、Shop的Synchronous.cs

好了,我們在Service中完成了登錄,并把用戶狀態傳遞回Shop站點。我們接著看用戶狀態是怎么同步的。首先,如果Session里的Security是空字符串,則表示Shop站點沒有向Service發送過請求,而Service向Shop發回了請求,這顯然是錯誤的。這次訪問是由客戶端偽造進行的訪問,于是訪問被拒絕了。同樣Security和InSecurity不相同,則表示請求和應答是不匹配的。可能應答被纂改過了,所以應答同樣被拒絕了。當檢驗Security通過后,我們保證Serive完成了應答,并且返回了確切的參數,下面就是讀出參數同步Shop站點和Service站點的用戶即時狀態。

string InUserID = this.Request.QueryString["UserID"];
string InPass = this.Request.QueryString["Pass"];
string InSecurity = this.Request.QueryString["Security"];

string Security = this.Session["Security"].ToString();
if (Security != "")
{
byte[] Value;
UnicodeEncoding Code = new UnicodeEncoding();
byte[] Message = Code.GetBytes(Security);
SHA512Managed Arithmetic = new SHA512Managed();
Value = Arithmetic.ComputeHash(Message);
Security = "";
foreach(byte o in Value)
{
Security += (int) o + "O";
}

if (Security == InSecurity)
{
if (InPass == "True")
{
this.Session["UserID"] = int.Parse(InUserID);
this.Session["Pass"] = true;
this.Response.Redirect(this.Session["Url"].ToString());
}
}
else
{
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("數據錯誤");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
}
}
else
{
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("訪問錯誤");
this.Response.Write("");
this.Response.Write("");
this.Response.Write("");
}

9、Shop的Page.cs

我們知道,頁面在一段時間不刷新后,Session會超時失效,在我們一直訪問Shop的時候怎么才能保證Service的Session不會失效呢?很簡單,我們返回來看Shop的Page.cs。通過在所有Shop的頁面內都用<iframe>嵌套Service的某個頁面,就能保證Service能和Shop的頁面同時刷新。需要注意的一點是Service的Session必須保證不小于所有Shop和Office的Session超時時間。這個在Web.config里可以進行配置。

this.Response.Write("<iframe width="0" height="0" src="" + project.service + "/Customer.aspx"></iframe>");

總結

一次完整的登錄完成了。我們接著假設一下現在要跳到Office的Any頁面,系統會進行怎樣的操作呢?Any(用戶沒有登錄)->Validate(用戶已經登錄)->Synchronous(同步)->Any。也就是說這次,用戶沒有進行登錄的過程。我們通過一次登錄,使得Service的用戶狀態為登錄,并且不管有多少個網站應用,只要這些網站都保證符合Shop的特性,這些網站就都能保持Service的用戶狀態,同時能通過Service獲得用戶的狀態。也就是說我們實現了SSO。


作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗