4

How to validate user in ASP.NET MVC 5.1.2 ?

In older versions of MVC 3,4 we had simple membership. So we do use membership.validate(user.name,password). But things have changed completly in asp.net mvc 5.

After programmatic login like

   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 validate a user we can do this :

1 Answer 1

4
        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 those does return any user then we have to assume that user is valid or else not.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.