Owin身份验证不会发出cookie

时间:2018-03-04 15:33:36

标签: asp.net asp.net-mvc asp.net-identity owin owin-middleware

我在Login控制器中有以下操作。出于测试目的,我没有在Index操作中使用登录表单。相反,我创建声明身份并登录。此操作是GET而不是POST。它会创建声明标识并将其用于AuthenticationManager.SignIn。但是当我检查浏览器cookie时,我找不到身份验证cookie。我想弄清楚出了什么问题。

    [AllowAnonymous]
    public ActionResult Index()
    {
        var identity = new ClaimsIdentity(DefaultAuthenticationTypes.ApplicationCookie);
        identity.AddClaim(new Claim(ClaimTypes.NameIdentifier, "30"));
        identity.AddClaim(new Claim(ClaimTypes.Name, "JFid"));
        identity.AddClaim(new Claim(ClaimTypes.Email, "test"));

        AuthenticationManager.SignIn(new AuthenticationProperties()
        {
            IsPersistent = true,
            ExpiresUtc = DateTime.UtcNow.AddDays(7)

        }, identity);

        return View();
    }

此外,我还在OWIN中启用了Cookie身份验证。

[assembly: OwinStartup(typeof(D.Support.WebStartup))]
namespace D.Support
{
    public class WebStartup
    {
        public void Configuration(IAppBuilder app)
        {

        app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
        {
            LoginPath = new PathString("/MyLoginPath"),
            CookieName = "MyCookieName",
            CookieHttpOnly = true,

        });
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您应该将ClaimsIdentity AuthenticationType设置为与CookieOption相同AuthenticationType

 app.UseCookieAuthentication(new Microsoft.Owin.Security.Cookies.CookieAuthenticationOptions()
    {
        LoginPath = new PathString("/MyLoginPath"),
        CookieName = "MyCookieName",
        CookieHttpOnly = true,
        AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie

    });