• 博客园logo
  • 会员
  • 众包
  • 新闻
  • 博问
  • 闪存
  • 赞助商
  • HarmonyOS
  • Chat2DB
    • 搜索
      所有博客
    • 搜索
      当前博客
  • 写随笔 我的博客 短消息 简洁模式
    用户头像
    我的博客 我的园子 账号设置 会员中心 简洁模式 ... 退出登录
    注册 登录

清宇诺诺

  • 博客园
  • 联系
  • 订阅
  • 管理

公告

View Post

跨子域单点登陆 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 "";
        }

 

posted on 2017-12-20 09:37  清宇诺诺  阅读(224)  评论(0)    收藏  举报

刷新页面返回顶部
 
博客园  ©  2004-2025
浙公网安备 33010602011771号 浙ICP备2021040463号-3