0
私はシンプルなログインルートを作成しようとしていますを承認し、このコードは、ログインしてブラウザにクッキーを送信するために正常に動作しません:ASP.NETアイデンティティ適切
ここ[Route("Login")]
[AllowAnonymous]
public async Task<IHttpActionResult> Login(UserBindingModel model)
{
if (ModelState.IsValid)
{
var user = await UserManager.FindUserAsync(model.username, model.password);
if (user != null)
{
await SignInAsync(user, true);
return Ok();
}
}
return BadRequest();
}
が呼び出されていますSignInAsync方法であります:ここでは
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
Authentication.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
Authentication.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
は私IdentityConfigです:
public class ApplicationUserManager : UserManager<ApplicationUser>
{
public ApplicationUserManager(IUserStore<ApplicationUser> store)
: base(store)
{
}
public static ApplicationUserManager Create(IdentityFactoryOptions<ApplicationUserManager> options, IOwinContext context)
{
var manager = new ApplicationUserManager(new TestUserStore());
// Configure validation logic for usernames
manager.UserValidator = new UserValidator<ApplicationUser>(manager)
{
AllowOnlyAlphanumericUserNames = false,
RequireUniqueEmail = true
};
// Configure validation logic for passwords
manager.PasswordValidator = new PasswordValidator
{
RequiredLength = 6,
RequireNonLetterOrDigit = false,
RequireDigit = true,
RequireLowercase = true,
RequireUppercase = false,
};
var dataProtectionProvider = options.DataProtectionProvider;
if (dataProtectionProvider != null)
{
manager.UserTokenProvider = new DataProtectorTokenProvider<ApplicationUser>(dataProtectionProvider.Create("ASP.NET Identity"));
}
return manager;
}
public async Task<ApplicationUser> FindUserAsync(string username, string password)
{
var userStore = new TestUserStore();
ApplicationUser user = await userStore.FindByNameAsync(username, password);
return await Task.FromResult(user);
}
}
これはcookiを送信するにもかかわらず、私は別のAPIコントローラを呼び出すたびに、私はリクエストが不正であることを知っています。私はアイデンティティ・フレームワークに非常に精通していないので、何が起こっているのか分かりません。
スタートアップ認証コードを含めることはできますか? – john
「apiメソッド」とは、ApiControllerを意味しますか? –
@john identityconfigコードは私のstartup.authコードです –