单点登录在ASP.NET上的简单实现

添加人:iyond七级(4344分)   添加时间:2007-05-20    阅读次数:10188  收藏此教程
  代码分析

  从上面的流程中我们可以看出,系统中Shop和Office的代码是完全类似的。只要Shop可以实现,Office也可以同样的克隆。所以我们的重点分析的对象是Shop和Service的代码。

  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();
        }
    }
}
3页 第2上一页123下一页
相关的教程: ASP.NET 单点登录
收藏此教程

当前平均分: 0.1(7 次打分)

-5-4-3-2-1012345
评论主题
您的大名
您的评论
验证码 点击换一个验证码
知识库搜索: