ASP身份自定义密码登录不起作用

时间:2017-02-15 17:38:01

标签: asp.net-mvc asp.net-identity

我目前正在尝试在ApplicationSignInManager上实现我自己的自定义PasswordSignInAsync方法,但到目前为止它无法验证,即使该方法不会抛出任何错误,这里是我在IdentityConfig.cs中的代码。我已经将ASP Identity配置为使用long作为主键。

我不知道我是否错过了创建正确用户登录的额外操作,任何人都可以指出我缺少的内容,我还没有找到任何代码。此自定义函数将其他参数传递给ApplicationUser的GenerateUserIdentityAsync方法。

copy

1 个答案:

答案 0 :(得分:1)

在方法中,您可以创建标识,但不会登录生成的标识。考虑一下:

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