【发布时间】:2014-01-15 11:30:34
【问题描述】:
我正在尝试实现 cookie 身份验证。这是我的登录操作:
public async Task<IHttpActionResult> Login([FromBody]string email)
{
var user = await UserManager.FindByNameAsync(email);
Authentication.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
var identity =
await UserManager.CreateIdentityAsync(user,
DefaultAuthenticationTypes.ApplicationCookie);
// identity.IsAuthenticated is true, why?
Authentication.SignIn(identity); // identity is correct (name is user@user.com), i checked it
// User.Identity.IsAuthenticated is false here
return Ok();
}
身份验证是:
private IAuthenticationManager Authentication
{
get
{
return HttpContext.GetOwinContext().Authentication;
}
}
但 User.Identity.Name 仍然为空。我做错了什么?我怎样才能获得经过身份验证的用户?
这是我的 Startup.Auth:
public partial class Startup
{
static Startup()
{
var userManager = new UserManager<IdentityUser>(new UserStore<IdentityUser>(new MyDbContext()));
userManager.UserValidator = new UserValidator<IdentityUser>(userManager)
{
AllowOnlyAlphanumericUserNames = false,
};
UserManagerFactory = () => userManager;
}
public static Func<UserManager<IdentityUser>> UserManagerFactory { get; set; }
public void ConfigureAuth(IAppBuilder app)
{
app.UseCookieAuthentication(new CookieAuthenticationOptions());
}
}
【问题讨论】:
标签: c# asp.net-mvc-5 asp.net-web-api2