Exemple #1
0
        public override async Task GrantResourceOwnerCredentials(OAuthGrantResourceOwnerCredentialsContext context)
        {
            using (var identityManager = _identityManagerFactory.Create())
            {
                var user = await identityManager.FindAsync(context.UserName, context.Password);

                if (user == null)
                {
                    context.SetError("invalid_grant", "The user name or password is incorrect.");
                    return;
                }

                string userId = user.Id; // await identityManager.Logins.GetUserIdForLocalLoginAsync(context.UserName);

                //   IEnumerable<Claim> claims = await identityManager.GetClaimsAsync(userId);
                ClaimsIdentity oAuthIdentity = await identityManager.CreateIdentityAsync(user, context.Options.AuthenticationType);

                ClaimsIdentity cookiesIdentity = await identityManager.CreateIdentityAsync(user, _cookieOptions.AuthenticationType);

                AuthenticationProperties properties = CreatePropertiesAsync(user);
                AuthenticationTicket     ticket     = new AuthenticationTicket(oAuthIdentity, properties);
                context.Validated(ticket);
                context.Request.Context.Authentication.SignIn(cookiesIdentity);
            }
        }
        public async Task <ActionResult> LoginUser(LoginViewModel user)
        {
            UserManager <AppUser> userManager = _userManagerFactory.Create(HttpContext);

            if (userManager == null)
            {
                return(View(user));
            }

            SignInManager <AppUser, string> signInManager = _signInManagerFactory.Create(HttpContext, userManager);

            if (signInManager == null)
            {
                return(View(user));
            }

            if (ModelState.IsValid)
            {
                var appUser = await userManager.FindAsync(user.Email, user.Password);

                if (appUser != null)
                {
                    await signInManager.SignInAsync(appUser, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Index", "Home"));
                }
            }
            ModelState.AddModelError("", @"Invalid username or password");

            return(View(user));
        }
Exemple #3
0
        public async Task <ActionResult> SignUpUser(SignUpViewModel user)
        {
            UserManager <AppUser>           userManager   = _userManagerFactory.Create(HttpContext);
            SignInManager <AppUser, string> signInManager = _signInManagerFactory.Create(HttpContext, userManager);

            //if (user.Password != user.ConfirmedPassword)
            //    ModelState.AddModelError("", Properties.Resources.PasswordDoesNotMatch);

            if (ModelState.IsValid)
            {
                var appUser = new AppUser
                {
                    UserName = user.Email,
                    Email    = user.Email,
                };

                IdentityResult result = await userManager.CreateAsync(appUser, user.Password);

                if (result.Succeeded)
                {
                    // **Uncomment that code when you want to generate an new role to a user. Then, run the Application and SignUp.**
                    //var roleStore = new RoleStore<AppRole>(new ViFlixContext());
                    //RoleManager<AppRole> roleManager = new RoleManager<AppRole>(roleStore);
                    //await roleManager.CreateAsync(new AppRole(RoleName.Admin));
                    //await userManager.AddToRoleAsync(appUser.Id, RoleName.Admin);

                    await signInManager.SignInAsync(appUser, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("IndexWhenAuthenticated", "Home"));
                }

                result.Errors.ForEach(error => ModelState.AddModelError("", error));
            }

            return(View(user));
        }
Exemple #4
0
 public AuthController() : this(UserManagerFactory.Create())
 {
 }