Owin 身份验证不发出 cookie

XPD*_*XPD 3 asp.net asp.net-mvc owin asp.net-identity owin-middleware

我在登录控制器中有以下操作。出于测试目的,我没有在 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();
    }
Run Code Online (Sandbox Code Playgroud)

而且我还在 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,

        });
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

Kah*_*azi 7

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

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

    });
Run Code Online (Sandbox Code Playgroud)