SSO-C#跨域单点登录(二)

开始新建一个站点SiteA,同时引用前面建的web服务:

创建BasePage.cs类:

 /// <summary>
    /// 页面基类
    /// </summary>
    public class BasePage : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies[Common.Something.CookieName] != null)
                {
                    HttpCookie cookie = Request.Cookies[Common.Something.CookieName];
                    cookie.Domain = "localhost";
                    cookie.Expires = DateTime.Now.AddMinutes(Common.Something.TimeOut);
                    Response.Cookies.Add(cookie);

                    var webservice = new UserWS.WebService1SoapClient();
                    string json = webservice.TokenGetCredence(Request.Cookies[Common.Something.CookieName].Value);

                    //Page.ClientScript.RegisterClientScriptBlock(typeof(string), "error", json);
                    if (!string.IsNullOrEmpty(json))
                    {
                        Common.User user = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.User>(json); 
                        Page.ClientScript.RegisterStartupScript(GetType(), "message", string.Format("欢迎 {0} 登录站点A!", user.UserName));
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "error", "<script>alert('cache不存在或失效');</script>");
                        Response.Redirect("http://localhost:7313/default.aspx?backurl=http://localhost:2532/default.aspx");
                    }
                }
                else
                {
                    Response.Redirect("http://localhost:7313/default.aspx?backurl=http://localhost:2532/default.aspx");
                }
            }

            base.OnLoad(e);
        }
    }
创建一个default.aspx页面:

  public partial class <span style="font-size:18px;">Default</span>: BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies[Common.Something.CookieName];
            cookie.Domain = "localhost";
            cookie.Expires = DateTime.Now.AddDays(-30);
            Response.Cookies.Add(cookie);
<span style="font-size:24px;"></span><pre name="code" class="csharp">                       //跳转到Login站点(端口7313,可以在项目生成选项里写死端口号)的default页面
                      Response.Redirect("http://localhost:7313/default.aspx");
        }
    }

 

开始新建一个站点SiteB,同时引用前面建的web服务:

创建BasePage.cs类:

 /// <summary>
    /// 页面基类
    /// </summary>
    public class BasePage : Page
    {
        protected override void OnLoad(EventArgs e)
        {
            if (!IsPostBack)
            {
                if (Request.Cookies[Common.Something.CookieName] != null)
                {
                    HttpCookie cookie = Request.Cookies[Common.Something.CookieName];
                    cookie.Domain = "localhost";
                    cookie.Expires = DateTime.Now.AddMinutes(Common.Something.TimeOut);
                    Response.Cookies.Add(cookie);

                    var webservice = new UserWS.WebService1SoapClient();
                    string json = webservice.TokenGetCredence(Request.Cookies[Common.Something.CookieName].Value);

                    //Page.ClientScript.RegisterClientScriptBlock(typeof(string), "error", json);
                    if (!string.IsNullOrEmpty(json))
                    {
                        var user = Newtonsoft.Json.JsonConvert.DeserializeObject<Common.User>(json);
                        Page.ClientScript.RegisterStartupScript(GetType(), "message", string.Format("欢迎 {0} 登录站点B!", user.UserName));
                    }
                    else
                    {
                        Page.ClientScript.RegisterStartupScript(GetType(), "error", "<script>alert('cache不存在或失效');</script>");
                        Response.Redirect("http://localhost:7313/default.aspx?backurl=http://localhost:3216/default.aspx");
                    }
                }
                else
                    Response.Redirect("http://localhost:7313/default.aspx?backurl=http://localhost:3216/default.aspx");
            }
            
            base.OnLoad(e);
        }
    }
创建一个default.aspx页面:

   public partial class WebForm1 : BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            HttpCookie cookie = Request.Cookies[Common.Something.CookieName];
            if (cookie != null)
            {
                cookie.Domain = "localhost";
                cookie.Expires = DateTime.Now.AddDays(-30);
                Response.Cookies.Add(cookie);
            }
            Response.Redirect("http://localhost:7313/default.aspx");
        }
    }



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值