参考sharing cookie in subdomains,我实现了jro的答案,它成功地登录了。(在不同的子域中共享cookie )
然而,随着这一变化的影响,签署过程。请参考我在下面共享的SignOut和SignIn代码。
问题是,在登录过程中,它执行一个FormsAuthentication.SignOut,然后重定向到控制器中的标志,但是"System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated"被设置为true,即使在注销过程中调用了FormsAuthentication.SignOut。
设置窗体身份验证Cookie的代码
public static HttpCookie GetAuthenticationCookie(CookieData cookieData)
{
string userData = PrepareCookieContentFromCookieData(cookieData); //Get a string with User data
AuthenticationSection section = WebConfigurationManager.GetWebApplicationSection("system.web/authentication") as AuthenticationSection;
TimeSpan ts = section.Forms.Timeout;
int timeout = (ts.Minutes != 0) ? timeout = ts.Minutes : 1;
bool isPersistent = Convert.ToBoolean(HttpContext.Current.Request.Form["isPersistent"] ?? "False");
if (isPersistent) timeout = 30 * 24 * 60;
//ticket object is formed based on the above details set. Evry page afer login will use this ticket to get base user data
FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(1, cookieData.userName, DateTime.Now,
DateTime.Now.AddMinutes(timeout), isPersistent, userData, FormsAuthentication.FormsCookiePath);
// to encrypt the ticket
string encryptedCookieString = FormsAuthentication.Encrypt(ticket);
// setting the ticket to the cookie.
var cookie = new HttpCookie(FormsAuthentication.FormsCookieName, encryptedCookieString);
cookie.HttpOnly = true;
cookie.Domain = "parent.com";
if (isPersistent)
cookie.Expires = DateTime.Now.AddYears(1);
return cookie;
}注销
public ActionResult SignOut()
{
if (HttpContext != null && HttpContext.Session != null)
{
HttpContext.Session.Abandon();
}
FormsAuthentication.SignOut();
}
return RedirectToAction("SignIn", "User");
}SignIn
public ActionResult SignIn(string CompanyCode)
{
//Check if logged in
if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
{
//return to a specific page
}
}感谢你在这方面的任何帮助。
发布于 2015-06-19 08:51:58
解决了这个问题。如果手动设置域名,则必须从webconfig窗体身份验证设置中设置域名。否则,它将尝试从默认域(在我的例子中是subapp1.parent.com)中清除cookie,因为我已经手动重写了cookie域,因此没有这样的cookie。
我的窗体身份验证设置如下
<forms cookieless="UseCookies" defaultUrl="~/Applications" loginUrl="~/user/signin" name="FormAuthentication" path="/"/>然后我添加了domain=".parent.com"作为域,它开始工作了。
我是怎么诊断出这个问题的,
我试着按照下面的代码在登录时手动删除cookie,
var cookie = HttpContext.Request.Cookies[FormsAuthentication.FormsCookieName];
if (cookie != null)
{
Logger.Log.InfoFormat("Cookies found. Domain:{0} Name:{1}", cookie.Domain, cookie.Name);
cookie.Expires = DateTime.Now.AddYears(-1);
Response.Cookies.Add(cookie);
}问题仍然存在。但是,当发生这种情况时,我会记录(log4net) cookie.Domain以获取详细信息。令人惊讶的是,域是空的,在这里我期待"parent.com“。然后,我检查了表单认证设置,并指出域名没有设置在那里。
希望这将有助于为某人节省几个小时!
发布于 2015-06-19 07:14:02
必须在CurrentPrincipal方法中将SignOut和用户设置为null。
public class LogOffController : Controller
{
public ActionResult Index()
{
FormsAuthentication.SignOut();
HttpContext.User = null;
Thread.CurrentPrincipal = null;
return View();
}
}希望能帮上忙。
https://stackoverflow.com/questions/30932064
复制相似问题