Request.GetOwinContext()。Authentication.SignIn不创建cookie

时间:2014-02-02 21:26:20

标签: cookies asp.net-identity owin asp.net-web-api2

以下代码没有创建.ASPNET Cookie,我在WebAPI自定义登录方法中使用此代码

  //TODO Validate Credential
  var claims = new List<Claim>();
            claims.Add(new Claim(ClaimTypes.Name, "ABCDE"));
            claims.Add(new Claim(ClaimTypes.Email, "ABCDE@EFGH.com"));

            var id = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);

            var authmgr = Request.GetOwinContext().Authentication;
            authmgr.SignIn(id);

2 个答案:

答案 0 :(得分:2)

您确定已将以下内容添加到App_Start / Startup.Auth.cs中吗?

public void ConfigureAuth(IAppBuilder app)
{
   app.UseCookieAuthentication(new CookieAuthenticationOptions
   {
      AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
      LoginPath = new PathString("/Account/Login")
   });
}

查看Brock Allen的引物 -

http://brockallen.com/2013/10/24/a-primer-on-owin-cookie-authentication-middleware-for-the-asp-net-developer/

答案 1 :(得分:1)

另一件可能导致这种情况的事情是将CookieSecure设置为Always,至少在调试时。当我拿出它时,我得到了一个饼干,当它在那里我没有饼干。在Startup.Configuration(以前的ConfigureAuth)方法中:

app.UseCookieAuthentication(new CookieAuthenticationOptions
{
    AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
    //CookieSecure = CookieSecureOption.Always,
    ExpireTimeSpan = TimeSpan.FromMinutes(30),
    LoginPath = new PathString("/Login/Index"),
    SlidingExpiration = true
});