为什么SetAuthCookie不起作用,但Response.Cookies.Add呢?

时间:2018-01-11 08:27:56

标签: c# asp.net-mvc forms-authentication

我有这个代码但它无法正常工作:

FormsAuthentication.SetAuthCookie(userId.ToString(), false);

该网站基本上表现得像我根本没有运行代码行并继续重定向回登录页面(如果returnUrl是授权页面)。

谢天谢地,我偶然发现了这篇文章:http://melick-rajee.blogspot.com/2015/03/formsauthenticationsetauthcookie-not.html用户显然遇到了同样的问题,但是能够得到以下信息:

FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(userId.ToString(), false, 60);
string encTicket = FormsAuthentication.Encrypt(ticket);
Response.Cookies.Add(new HttpCookie(FormsAuthentication.FormsCookieName, encTicket)); 

我能想到的唯一可能不是我网站标准的是它是另一个网站的子应用程序。不完全确定这会对事情产生什么影响,但我真的很想理解为什么第一种方式不能像其他应用程序一样对我起作用。

0 个答案:

没有答案