How to verify a user or authenticate with ASP.NET MVC 5.1.2 OWIN

How to check user in ASP.NET MVC 5.1.2?

In older versions of MVC 3.4, we had a simple membership. Therefore, we use member.validate (username, password). But everything has changed completely in asp.net mvc 5.

After logging in, for example

   await SignInAsync(user, model.RememberMe); // after login HttpContext.Current.User.Identity.IsAuthenticated is false . Odd.
   private async Task SignInAsync(ApplicationUser user, bool isPersistent) {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
        AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, await user.GenerateUserIdentityAsync(Manager));
    }

There is no previous authentication system. To verify the user, we can do this:

+4
source share
1 answer
        public static ApplicationUserManager Manager {
        get {
            return _userManager ?? HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>();
        }
        private set {
            _userManager = value;
        }
    }

    public static async Task<ApplicationUser> GetUserAsync(string userName, string password) {
        return await Manager.FindAsync(userName: userName, password: password);
    }
    public static ApplicationUser GetUserByEmail(string email, string password) {
        var user = Manager.FindByEmail(email);
        return Manager.Find(user.UserName, password);
    }

if they return any user, then we must assume that the user is valid or not.

+4
source

All Articles