OWIN Cookie身份验证无法在IIS 8.5上运行

时间:2016-09-23 13:08:00

标签: c# asp.net iis cookies owin

我开发了一个带有OWIN身份验证的ASP.NET webapp,它可以在我的开发机器(Windows 10 / IIS 10)上正常工作,但是当使用IIS 8.5将webapp发布到我的Windows 2012服务器时,cookie身份验证不会似乎工作。

当我登录(IsPersistent设置为true)并关闭浏览器时,我仍然在我再次启动浏览器时登录,这样就可以了。但是当我重新启动IIS并启动浏览器时,我必须再次登录。

我创建了一个非常简单的应用程序来测试它,使用以下代码:

Startup.cs

public void ConfigureAuthentication(IAppBuilder app)
    {
        app.UseCookieAuthentication(new CookieAuthenticationOptions
        {
            AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
            LoginPath = new PathString("/Login"),
            CookieName = "ThisIsTheAuthCookie"
        });
    }

AuthenticationController.cs

public ActionResult Login(string userName, string password)
    {
        //For testing purposes every user/pwd is fine
        var identity = new ClaimsIdentity(new [] { new Claim(ClaimTypes.Name, userName), },
            DefaultAuthenticationTypes.ApplicationCookie,
            ClaimTypes.Name, ClaimTypes.Role);

        HttpContext.GetOwinContext().Authentication.SignIn(new AuthenticationProperties { IsPersistent = true }, identity);

        return RedirectToAction("index", "home");
    }

即使Chrome显示Cookie,但看起来OWIN在IIS 8.5上没有使用它: enter image description here

有人知道问题是什么吗?

THX, 丹尼

3 个答案:

答案 0 :(得分:0)

你能尝试几件事并分享结果: - 1.重新启动IIS,保留User-Agent。看看你现在是否登录。 2.启用Katana日志记录并在日志中检查此警告/错误。

答案 1 :(得分:0)

关于此的任何结果?

对我来说,看起来你有可用会话ID的cookie但是IIS服务器在这个会话上不再知道。您确定在IIS服务器上保留会话吗? (而不是'在处理中#39;)

您可以在IIS配置中的“会话状态”下找到该选项。见TechNet Article IIS

答案 2 :(得分:0)

问题解决了。我不得不在web.config中添加MachineKey元素!