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());
}
}