Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to validate username and password of user before log in in ASP.net Identity?

In my ASP.Net application, want to validate username and password of users before logging them into the application. I am using ASP.net Identity framework for membership.

Want to make sure whether the entered username and password exist or not. something like this one Membership.ValidateUser(username,password) which was used in default membership but can not be used with ASP.net Identity. Tried this one but no luck?

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

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

var user = manager.Find(userName,Password);
like image 733
Mohammed Faisal Khan Avatar asked Jan 13 '17 21:01

Mohammed Faisal Khan


People also ask

Which validation is used for password in asp net?

Net. In this article we will see how to enforce users to create strong passwords. To validate for strong passwords we will use RegularExpressionValidator with REGEX.

What is UserManager in ASP.NET Core?

The ASP.NET Identity UserManager class is used to manage users e.g. registering new users, validating credentials and loading user information. It is not concerned with how user information is stored. For this it relies on a UserStore (which in our case uses Entity Framework).


2 Answers

I use something like this:

public static bool ValidateUser(string Email, string Password)
    {
        var user = UserManager.FindByName(Email);
        return SignInManager.UserManager.CheckPassword(user, Password); 
    }
like image 75
puddleglum Avatar answered Sep 22 '22 10:09

puddleglum


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);
like image 36
aelagawy Avatar answered Sep 25 '22 10:09

aelagawy