首页 文章

Owin身份验证不会发出cookie

提问于
浏览
0

我在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 回答

  • 2

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

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

相关问题