首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >即使在手动设置CurrentPrincipal.Identity.IsAuthenticated cookie域时,FormsAuth也是正确的。

问即使在手动设置CurrentPrincipal.Identity.IsAuthenticated cookie域时,FormsAuth也是正确的。
EN

Stack Overflow用户
提问于 2015-06-19 07:09:31
回答 2查看 1.2K关注 0票数 1

参考sharing cookie in subdomains,我实现了jro的答案,它成功地登录了。(在不同的子域中共享cookie )

然而,随着这一变化的影响,签署过程。请参考我在下面共享的SignOut和SignIn代码。

问题是,在登录过程中,它执行一个FormsAuthentication.SignOut,然后重定向到控制器中的标志,但是"System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated"被设置为true,即使在注销过程中调用了FormsAuthentication.SignOut。

设置窗体身份验证Cookie的代码

代码语言:javascript
复制
 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;
        }

注销

代码语言:javascript
复制
 public ActionResult SignOut()
        {                        

                if (HttpContext != null && HttpContext.Session != null)
                {                      
                    HttpContext.Session.Abandon();
                }    

                FormsAuthentication.SignOut();

            } 
            return RedirectToAction("SignIn", "User");    
        }

SignIn

代码语言:javascript
复制
 public ActionResult SignIn(string CompanyCode)
        {           
           //Check if logged in
            if (System.Threading.Thread.CurrentPrincipal.Identity.IsAuthenticated)
            {
                //return to a specific page
            }
        }

感谢你在这方面的任何帮助。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-06-19 08:51:58

解决了这个问题。如果手动设置域名,则必须从webconfig窗体身份验证设置中设置域名。否则,它将尝试从默认域(在我的例子中是subapp1.parent.com)中清除cookie,因为我已经手动重写了cookie域,因此没有这样的cookie。

我的窗体身份验证设置如下

代码语言:javascript
复制
<forms cookieless="UseCookies" defaultUrl="~/Applications" loginUrl="~/user/signin"  name="FormAuthentication" path="/"/>

然后我添加了domain=".parent.com"作为域,它开始工作了。

​

我是怎么诊断出这个问题的,

我试着按照下面的代码在登录时手动删除cookie,

代码语言:javascript
复制
 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“。然后,我检查了表单认证设置,并指出域名没有设置在那里。

希望这将有助于为某人节省几个小时!

票数 1
EN

Stack Overflow用户

发布于 2015-06-19 07:14:02

必须在CurrentPrincipal方法中将SignOut和用户设置为null。

代码语言:javascript
复制
public class LogOffController : Controller
{
    public ActionResult Index()
    {
        FormsAuthentication.SignOut();

        HttpContext.User = null;
        Thread.CurrentPrincipal = null;

        return View();
    }
}

希望能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30932064

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档