我在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,
});
}
}
}
答案 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
});