2017-01-13 49 views
1

在我的ASP.Net應用程序中,想要在將用戶登錄到應用程序之前驗證用戶的用戶名和密碼。我正在使用ASP.net身份框架成員資格。如何在使用ASP.net身份登錄之前驗證用戶的用戶名和密碼?

想要確認輸入的用戶名和密碼是否存在。 像這樣的一個Membership.ValidateUser(username,password)它用於默認的成員資格,但不能與ASP.net身份一起使用。 試過這一個,但沒有運氣?

var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 

var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); 

var user = manager.Find(userName,Password); 
+1

請註明aelagawy的答案被接受,因爲它似乎回答你的問題。 – Megrez7

回答

2
var manager = Context.GetOwinContext().GetUserManager<ApplicationUserManager>(); 
var signinManager = Context.GetOwinContext().GetUserManager<ApplicationSignInManager>(); 
var user = manager.FindByName(Email.Text); 

//check for credentials before sign in ..  
var validCredentials = signinManager.UserManager.CheckPassword(user, Password.Text); 
if (validCredentials) 
{ 
    //sample code to run if user's credentials is valid and before login 
    if(!manager.IsInRole(user.Id, "Administrators")) 
    { 
     FailureText.Text = "you need a higher permission level in order to login"; 
     return; 
    } 
} 

//then sign in 
var result = signinManager.PasswordSignIn(Email.Text, Password.Text, RememberMe.Checked, shouldLockout: false); 
0

我用的是這樣的:

public static bool ValidateUser(string Email, string Password) 
    { 
     var user = UserManager.FindByName(Email); 
     return SignInManager.UserManager.CheckPassword(user, Password); 
    } 
相關問題