Asp.Net跨域登录验证,加强可以用于Passport
今天在工作中遇到的问题:
一个主站:www.xyz.com
一个二级域名站:cc.xyz.com
需求:
cc.xyz.com要读到www.xyz.com下的用户登陆信息
当前状态:
www.xyz.com使用.net提供的用户登陆验证方式:FormsAuthenticationTicket
解决方案:
第一步:两个站点下的Web.config文件中<authentication>节点的信息必须完全相同,尤其是<authentication>节点下的<forms name="必须相同">
第二步:www.xyz.com下的登录程序中加入Domain属性
string userData = "用户的登录信息";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,LoginFlag.ToString(),
DateTime.Now,
DateTime.Now.Add(TimeSpan.FromDays(3)),
isPersistent,userData);
//加密序列化验证票为字符串
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) ; //生成Cookie
Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
UserCookie.Expires = ticket.Expiration;
UserCookie.Domain = ".xyz.com";
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1,LoginFlag.ToString(),
DateTime.Now,
DateTime.Now.Add(TimeSpan.FromDays(3)),
isPersistent,userData);
//加密序列化验证票为字符串
string encTicket = FormsAuthentication.Encrypt(ticket);
// Create the cookie.
HttpCookie UserCookie = new HttpCookie(FormsAuthentication.FormsCookieName, encTicket) ; //生成Cookie
Context.Response.Cookies.Add (UserCookie) ; //输出Cookie
UserCookie.Expires = ticket.Expiration;
UserCookie.Domain = ".xyz.com";
在cc.xyz.com站下读取验证信息:
if (Page.User.Identity.IsAuthenticated)
{
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
Response.Write(identity.Ticket.UserData);
}
{
FormsIdentity identity = (FormsIdentity)HttpContext.Current.User.Identity;
Response.Write(identity.Ticket.UserData);
}
退出登陆程序:
HttpCookie UserCookie = Response.Cookies[FormsAuthentication.FormsCookieName];
UserCookie.Domain = ".hjbook.net";
UserCookie.Expires = DateTime.Now.AddDays(-1);
UserCookie.Domain = ".hjbook.net";
UserCookie.Expires = DateTime.Now.AddDays(-1);

浙公网安备 33010602011771号