Owin 身份验证不会发出 cookie



我在登录控制器中有以下操作。出于测试目的,我不在索引操作中使用登录表单。相反,我创建声明标识并登录。此操作是获取而不是开机自检。它创建一个声明标识并将其用于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,
        });
        }
    }
}

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

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

如果有人好奇为什么我们需要按照公认的答案去做,只是为了把我的发现放在这里。

如果您没有在 CookieAuthenticationOptions 中指定 AuthenticationType,则最终使用的默认值是 CookieAuthenticationDefaults.AuthenticationType,其值为"Cookies">

来自 Microsoft.AspNet.Identity 包的 DefaultAuthenticationTypes.ApplicationCookie 的字符串值为"ApplicationCookie"。

在 CookieAuthenticationHandler 的 ApplyResponseGrantAsync(( 方法中,调用该方法以将身份验证锅附加到响应标头,调用以下代码。如果身份验证类型与声明标识不匹配,它将返回 null。

/// <summary>
        /// Find response sign-in details for a specific authentication middleware
        /// </summary>
        /// <param name="authenticationType">The authentication type to look for</param>
        /// <returns>The information instructing the middleware how it should behave</returns>
        public AuthenticationResponseGrant LookupSignIn(string authenticationType)
        {
            if (authenticationType == null)
            {
                throw new ArgumentNullException("authenticationType");
            }
            AuthenticationResponseGrant grant = _context.Authentication.AuthenticationResponseGrant;
            if (grant == null)
            {
                return null;
            }
            foreach (var claimsIdentity in grant.Principal.Identities)
            {
                if (string.Equals(authenticationType, claimsIdentity.AuthenticationType, StringComparison.Ordinal))
                {
                    return new AuthenticationResponseGrant(claimsIdentity, grant.Properties ?? new AuthenticationProperties());
                }
            }
            return null;
        }

相关内容

  • 没有找到相关文章

最新更新