zoukankan      html  css  js  c++  java
  • 跨子域单点登陆 demo

    有个项目药公用数据库,共享登陆,及在一个站点登陆后,所有的二级域名都共享登陆,不需要登陆了。下面简单的记录下。

    我用的共享Cookie。

    首先的首先在配置文件加入cookie设置:

    在 <system.web>节点下:
    <authentication mode="Forms">
    <forms name=".DottextCookie" loginUrl="login.aspx" protection="All" timeout="480" path="/" domain=".wangzihao.com"/>
    </authentication>

    我们在登陆的时候可以正常存储cookie,只需要加上一句如下:

        //存cookie
        string adminname = Utils.GetCookie("AdminName", "HGcms");
        string adminpwd = Utils.GetCookie("AdminPwd", "HGcms");
    
         
    
         /// <summary>
            /// 写cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <param name="strValue"></param>
            public static void WriteCookie(string strName, string key, string strValue)
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies[strName];
                if (cookie == null)
                {
                    cookie = new HttpCookie(strName);
                }
                cookie[key] = UrlEncode(strValue);
                cookie.Domain = FormsAuthentication.CookieDomain;
                HttpContext.Current.Response.AppendCookie(cookie);
            }        

    上面的在登录页面已经完成了。下面就是在二级域名下的读cookiek了,你可以像在登陆页面一样来读取cookie.

           //读cookie
            string adminname = Utils.GetCookie("AdminName", "HGcms");
                string adminpwd = Utils.GetCookie("AdminPwd", "HGcms");
    
         /// <summary>
            /// 读cookie值
            /// </summary>
            /// <param name="strName">名称</param>
            /// <returns>cookie值</returns>
            public static string GetCookie(string strName, string key)
            {
                if (HttpContext.Current.Request.Cookies != null && HttpContext.Current.Request.Cookies[strName] != null && HttpContext.Current.Request.Cookies[strName][key] != null)
                    return UrlDecode(HttpContext.Current.Request.Cookies[strName][key].ToString());
    
                return "";
            }
  • 相关阅读:
    Redis入门--- 五大数据类型 ---String ,List
    Redis入门 --- 安装
    Netty 应用实例-群聊系统,心跳检测机制案例 ,WebSocket 编程实现服务器和客户端长连接
    红锁的实现
    基于Redis实现分布式锁
    基于分布式锁 分布式全局唯一ID
    Netty Java BIO 编程 (一)
    Netty Java NIO 基本介绍Channel 和 buffer (二)
    Java AIO 基本介绍
    SpringBoot 系列教程自动配置选择生效
  • 原文地址:https://www.cnblogs.com/WZH75171992/p/8072194.html
Copyright © 2011-2022 走看看