11

以下代码未创建 .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);
4

2 回答 2

3

您是否确保将以下内容添加到您的 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/

于 2014-03-19T04:36:22.533 回答
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
});
于 2014-06-27T13:39:38.987 回答