Owin身份验证不发出cookie。

4

我在登录控制器中有以下操作。为了测试目的,我没有在Index操作中使用登录表单。相反,我创建了claims identity并进行了登录。这个操作是GET而不是POST。它创建了一个claims identity并将其用于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,

        });
        }
    }
}

是的,我已经添加并调试了 Configuration 方法。它在那里被触发了。这与传递到 UseCookieAuthentication 方法中的 Configuration 有关吗? - XPD
是的!兄弟,你搞定了。我设置后,它神奇地开始工作了。把它放在答案里,我会接受的。 - XPD
为什么需要明确指定?理想情况下,由于它从AuthenticationOptions继承,AuthenticationType应默认正确地设置为内部。 - XPD
2个回答

8

您应该将ClaimsIdentityAuthenticationType设置为与CookieOptionAuthenticationType相同。

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

    });

1
我花了一天多的时间,直到我弄清楚要搜索什么,并找到了你的答案。谢谢! - Thomas Eyde

1

如果有人好奇为什么我们需要按照已接受的答案所示进行操作,我在这里放上我的发现。

如果您在CookieAuthenticationOptions中未指定AuthenticationType,则默认值将使用CookieAuthenticationDefaults.AuthenticationType,其值为"Cookies"。

而Microsoft.AspNet.Identity包中的DefaultAuthenticationTypes.ApplicationCookie具有字符串值"ApplicationCookie"。

在CookieAuthenticationHandler的ApplyResponseGrantAsync()方法中(该方法被调用以将身份验证cooker附加到响应标头),会调用以下代码。 如果身份验证类型与claimsidentity的不匹配,则返回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;
        }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接