我当前正在尝试在应用程序agplipationsignInmanager上实现自己的自定义密码signInasync方法,但是到目前为止,即使该方法不会抛出任何错误,它也无法进行身份验证,这是我的代码,这是我的代码IdentityConfig.cs。我已经配置了ASP身份以将长期使用为主要密钥。
我不知道我是否缺少创建正确的用户标志的其他操作,任何人都可以指出我缺少的东西吗,我还没有找到任何代码。此自定义函数将附加参数传递给ApplicationUser的GenerateUserIdentityAsync方法。
public class ApplicationSignInManager : SignInManager<ApplicationUser, long>
{
public ApplicationSignInManager(ApplicationUserManager userManager, IAuthenticationManager authenticationManager)
: base(userManager, authenticationManager)
{
}
public async Task<SignInStatus> PasswordSystemSignInAsync(string userName, string password, bool IsPersistent, bool shouldLockout, bool IsAdministrative, string securityCode = null)
{
var user = await UserManager.FindByNameAsync(userName);
if(user != null)
{
bool passwordCheck = await UserManager.CheckPasswordAsync(user, password);
if (passwordCheck)
{
var signInUser = await user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager, IsAdministrative);
if (signInUser.IsAuthenticated)
{
return SignInStatus.Success;
}
return SignInStatus.Failure;
}
return SignInStatus.Failure;
}
return SignInStatus.Failure;
}
}
在您创建身份的方法中,您没有在生成的身份中签名。考虑一下:
public async Task<SignInStatus> PasswordSystemSignInAsync(string userName,string password, bool IsPersistent, bool shouldLockout, bool IsAdministrative, string securityCode = null)
{
var user = await UserManager.FindByNameAsync(userName);
if(user != null)
{
bool passwordCheck = await UserManager.CheckPasswordAsync(user, password);
if (passwordCheck)
{
var userIdentity = await user.GenerateUserIdentityAsync((ApplicationUserManager)UserManager, IsAdministrative);
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie, DefaultAuthenticationTypes.TwoFactorCookie);
AuthenticationManager.SignIn(new AuthenticationProperties { IsPersistent = IsPersistent}, userIdentity);
return SignInStatus.Success;
}
return SignInStatus.Failure;
}
return SignInStatus.Failure;
}