2017-02-15 67 views
0

我目前正在嘗試在ApplicationSignInManager上實現自己的自定義PasswordSignInAsync方法,但到目前爲止它無法驗證即使該方法不會引發任何錯誤,這裏是我在IdentityConfig中的代碼的.cs。我已經將ASP Identity配置爲只用作主鍵。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; 
     } 
} 

回答

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

AuthenticationManager.signout是做什麼的?在SignIn上,我必須設置所有屬性嗎?該方法是否從Startup.Auth檢索所有其他屬性? –

+0

@Forcefield爲了確保用戶尚未登錄,只需調用'signOut'方法註銷用戶即可。該方法是安全的,如果用戶尚未登錄,則不執行任何操作。不,您不需要設置所有屬性,只需填寫一次即可。 –

+0

這是要走的路!只是從參數中傳遞IsPersistent是我對你的答案做出的唯一改變。身份登錄管理器是否在某處引用了此引用?我找不到任何定製的PasswordSignInMethod的例子,這些例子沒有被稱爲基本的 –

相關問題