源码网商城,靠谱的源码在线交易网站 我的订单 购物车 帮助

源码网商城

.NET单点登陆的实现方法及思路

  • 时间:2021-10-12 08:50 编辑: 来源: 阅读:
  • 扫一扫,手机访问
摘要:.NET单点登陆的实现方法及思路
系统的基本架构    我们假设一个系统System包含Service客户服务中心、Shop网上购物中心和Office网上办公中心三个独立的网站。 Service管理客户的资料,登录和注销过程。不论客户访问System的任何一个页面,系统都会转到登录界面,在用户登录后,系统会自动转会到客户上 次请求的页面。并且用户此后可以在System中无缝切换。不需要再次进行登录。即在System中实现单点登录SSO(Single Sign-On)。   我们知道,用户的即时状态通常是使用Application、Session、Cookie和存储的。而这些都是不能在程序中跨站点访问的。我们必需通过站点间相互通讯来确认用户的即时状态。  简单的实现    第一步,假设用户访问了Shop或Office的任何一个页面Any。该页面所在的网站将会检查用户的即时状态。如果用户已经登录了,则将 Any页面的信息返回给用户。如果用户还没有登录,则自动转到Service的Validate页面,验证用户在Service状态。即Shop或 Office向Service发出请求,要求Service返回用户的即时状态。   第二步,Validate验证用户的即时状态,如果 用户已经登录了,则Service将用户的即时状态返回给Shop或Office的同步页面 Synchronous,通知Shop或Office同步用户状态。如果用户没有登录,则自动转向Customer页面,提示用户登录。   第三步,用户完成登录过程,当用户成功登录后,自动转回Validate页面,通知Shop或Office的Synchronous进行用户状态的同步。   第四步,在用户状态同步完成后,在本地站点,用户状态成为在线状态,即可访问Any页面。   在上面的流程中。我们知道,不管用户访问哪个站点,用户只需要一次登录,就保证用户在Service的即时状态都是在线的,不会再需要进行第二次登录的过程。   现在我们的思路已经清楚,具体的实现我们将在代码分析中完成。 代码分析   从上面的流程中我们可以看出,系统中Shop和Office的代码是完全类似的。只要Shop可以实现,Office也可以同样的克隆。所以我们的重点分析的对象是Shop和Service的代码。   [b]1、Shop的Web.config和Project.cs[/b]   在Shop的Web.config里,我们配置了Service站点和Shop站点,以方便我们在部署时方便修改。
[url=]   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();   }  } }
[b]5、Service的Global.cs[/b]    现在我们页面转到了Service的Validate页面,我们转过来看 Service的代码。在Global中我们同样定义了四个Session变量,都和Shop的Session用处类似。WebSite是保存请求用户即 时状态的站点信息。以便能在登录后返回正确的请求站点。   [b]6、Service的Validate.cs [/b]  首先,将Shop传递过来的参数保存到Session中。如果用户没有登录,则转到Customer页面进行登录。如果用户已经登录了。则将用户即时状态传回给Shop站点。如上所述,这里将Security重新Hash了一次传回给Shop,以保证数据不被纂改。
[u]复制代码[/u] 代码如下:
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");  } }
  [b]7、Service的Customer.cs和Login.cs [/b]   Customer主要的是一个用于登录的表单,这里就不 贴出代码了。这里分析一下Login的一段代码,这段代码是当登录是直接在Service完成的(WebSite为空值),则页面不会转到Shop或 Office站点。所以应该暂停在Service站点。系统如果比较完美,这里应该显示一组字系统的转向链接。下面我们看到,当Pass为真时,页面转回 到Validate页面,通过上面的分析,我们知道,页面会转向Shop的Synchronous页面,进行用户状态的同步。
[u]复制代码[/u] 代码如下:
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"); }
  [b]8、Shop的Synchronous.cs [/b]   好了,我们在Service中完成了登录,并把用户状态传递回Shop站 点。我们接着看用户状态是怎么同步的。首先,如果Session里的Security是空字符串,则表示Shop站点没有向Service发送过请求,而 Service向Shop发回了请求,这显然是错误的。这次访问是由客户端伪造进行的访问,于是访问被拒绝了。同样Security和 InSecurity不相同,则表示请求和应答是不匹配的。可能应答被纂改过了,所以应答同样被拒绝了。当检验Security通过后,我们保证 Serive完成了应答,并且返回了确切的参数,下面就是读出参数同步Shop站点和Service站点的用户即时状态。
[u]复制代码[/u] 代码如下:
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(""); }
 [b]9、Shop的Page.cs [/b]   我们知道,页面在一段时间不刷新后,Session会超时失效,在我们一直访问Shop的 时候怎么才能保证Service的Session不会失效呢?很简单,我们返回来看Shop的Page.cs。通过在所有Shop的页面内都用 <iframe>嵌套Service的某个页面,就能保证Service能和Shop的页面同时刷新。需要注意的一点是Service的Session必 须保证不小于所有Shop和Office的Session超时时间。这个在Web.config里可以进行配置。
[u]复制代码[/u] 代码如下:
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
  • 全部评论(0)
联系客服
客服电话:
400-000-3129
微信版

扫一扫进微信版
返回顶部