zoukankan      html  css  js  c++  java
  • 利用cookie的跨域单点登录的简单实现

    Configuration:
    1. Web.Config
    在两个站点的配置配置文件machine节点上相同的validationKey, decryptionKey and validation的值,如
    <machineKey validationKey="282487E295028E59B8F411ACB689CCD6F39DDD21E6055A3EE480424315994760A
    DF21B580D8587DB675FA02F79167413044E25309CCCDB647174D5B3D0DD9141"
    decryptionKey="8B6697227CBCA902B1A0925D40FAA00B353F2DF4359D2099"
    validation="SHA1" />
    2. IIS
    在IIS->Directory security 上添加 "ASPNET Machine Account" 的所有权限
    Coding:
    代码示例
    站点1的->Login.aspx.cs
    if (login_Successful)
    {
        //创建一个cookie
        HttpCookie cookie = new HttpCookie("strCookieName");
        //设置cookie的值  可以保存登录名称信息
        cookie.Value ="set_cookie_value";
        //设置 cookie 的生存周期5 分钟
        DateTime dtNow = DateTime.Now;
        TimeSpan tsMinute = new TimeSpan(0, 0, 5, 0);
        cookie.Expires = dtNow + tsMinute;
        //添加cookie
        Response.Cookies.Add(cookie);
        Response.Write("Cookie written. ");
    }
    检查cookie是否存在
    站点2->Default.aspx.cs
    protected void Page_Load(object sender, EventArgs e)
    {
        //获取cookie
        HttpCookie cookie = Request.Cookies["strCookieName"];
        //检查cookie 是否存在
        if (cookie != null)
        {
            ReadCookie();
        }
        else
        {
            lblCookie.Text = "Cookie not found. ";
        }
    }
    protected void ReadCookie()
    {
        //Get the cookie name the user entered
        //Grab the cookie
        HttpCookie cookie = Request.Cookies["strCookieName"];
        //Check to make sure the cookie exists
        if (cookie == null)
        {
            lblCookie.Text = "Cookie not found. ";
        }
        else
        {
            //Write the cookie value
            String strCookieValue = cookie.Value.ToString();
            lblCookie.Text = "The cookie contains: " + strCookieValue + "";
        }
    }
  • 相关阅读:
    [读书笔记]子查询
    [读书笔记]SQL约束
    [转载]NoSQL数据库的基础知识
    利用C#实现对excel的写操作
    [转载]SQL Server内核架构剖析
    利用花生壳和IIS发布网页过程
    [读书笔记]ASP.NET的URL路由引擎
    [翻译]比较ADO.NET中的不同数据访问技术(Performance Comparison:Data Access Techniques)
    [正则表达式]基础知识总结
    CF623E Transforming Sequence
  • 原文地址:https://www.cnblogs.com/linzheng/p/1906026.html
Copyright © 2011-2022 走看看