예제 #1
0
        public async Task <ActionResult> ExternalLoginCallback(string returnUrl)
        {
            var loginInfo = await AuthenticationManager.GetExternalLoginInfoAsync();

            if (loginInfo == null)
            {
                return(RedirectToAction("Login"));
            }

            // Sign in the user with this external login provider if the user already has a login
            var result = await AplicationSignInService.ExternalSignInAsync(loginInfo, isPersistent : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = false }));

            case SignInStatus.Failure:
            default:
                // If the user does not have an account, then prompt the user to create an account
                ViewBag.ReturnUrl     = returnUrl;
                ViewBag.LoginProvider = loginInfo.Login.LoginProvider;
                return(View("ExternalLoginConfirmation", new ExternalLoginConfirmationViewModel {
                    Email = loginInfo.Email
                }));
            }
        }
예제 #2
0
        public async Task <ActionResult> VerifyCode(VerifyCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // The following code protects for brute force attacks against the two factor codes.
            // If a user enters incorrect codes for a specified amount of time then the user account
            // will be locked out for a specified amount of time.
            // You can configure the account lockout settings in IdentityConfig
            var result = await AplicationSignInService.TwoFactorSignInAsync(model.Provider, model.Code, isPersistent : model.RememberMe, rememberBrowser : model.RememberBrowser);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(model.ReturnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid code.");
                return(View(model));
            }
        }
예제 #3
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            // This doesn't count login failures towards account lockout
            // To enable password failures to trigger account lockout, change to shouldLockout: true
            var result = await AplicationSignInService.PasswordSignInAsync(model.UserName, model.Password, model.RememberMe, shouldLockout : false);

            switch (result)
            {
            case SignInStatus.Success:
                return(RedirectToLocal(returnUrl));

            case SignInStatus.LockedOut:
                return(View("Lockout"));

            case SignInStatus.RequiresVerification:
                return(RedirectToAction("SendCode", new { ReturnUrl = returnUrl, RememberMe = model.RememberMe }));

            case SignInStatus.Failure:
            default:
                ModelState.AddModelError("", "Invalid login attempt.");
                return(View(model));
            }
        }
예제 #4
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email
                };
                var result = await AplicationUserService.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771
                    // Send an email with this link
                    // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id);
                    // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme);
                    // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>");
                    //Assign Role to user Here
                    await AplicationUserService.AddToRoleAsync(user.Id, model.UserRole);

                    //Ends Here
                    return(RedirectToAction("Index", "Home"));
                }
                //ViewBag.Name = new SelectList(context.Roles.Where(u => !u.Name.Contains("Admin"))
                //                          .ToList(), "Name", "Name");
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
예제 #5
0
 public async Task <ActionResult> VerifyCode(string provider, string returnUrl, bool rememberMe)
 {
     // Require that the user has already logged in via username/password or external login
     if (!await AplicationSignInService.HasBeenVerifiedAsync())
     {
         return(View("Error"));
     }
     return(View(new VerifyCodeViewModel {
         Provider = provider, ReturnUrl = returnUrl, RememberMe = rememberMe
     }));
 }
예제 #6
0
        public async Task <ActionResult> DisableTwoFactorAuthentication()
        {
            await AplicationUserService.SetTwoFactorEnabledAsync(User.Identity.GetUserId(), false);

            var user = await AplicationUserService.FindByIdAsync(User.Identity.GetUserId());

            if (user != null)
            {
                await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);
            }
            return(RedirectToAction("Index", "Manage"));
        }
예제 #7
0
        public async Task <ActionResult> SendCode(SendCodeViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View());
            }

            // Generate the token and send it
            if (!await AplicationSignInService.SendTwoFactorCodeAsync(model.SelectedProvider))
            {
                return(View("Error"));
            }
            return(RedirectToAction("VerifyCode", new { Provider = model.SelectedProvider, ReturnUrl = model.ReturnUrl, RememberMe = model.RememberMe }));
        }
예제 #8
0
        //
        // GET: /Manage/RemovePhoneNumber
        public async Task <ActionResult> RemovePhoneNumber()
        {
            var result = await AplicationUserService.SetPhoneNumberAsync(User.Identity.GetUserId(), null);

            if (!result.Succeeded)
            {
                return(RedirectToAction("Index", new { Message = ManageMessageId.Error }));
            }
            var user = await AplicationUserService.FindByIdAsync(User.Identity.GetUserId());

            if (user != null)
            {
                await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);
            }
            return(RedirectToAction("Index", new { Message = ManageMessageId.RemovePhoneSuccess }));
        }
예제 #9
0
        public async Task <ActionResult> SendCode(string returnUrl, bool rememberMe)
        {
            var userId = await AplicationSignInService.GetVerifiedUserIdAsync();

            if (userId == null)
            {
                return(View("Error"));
            }
            var userFactors = await AplicationUserService.GetValidTwoFactorProvidersAsync(userId);

            var factorOptions = userFactors.Select(purpose => new SelectListItem {
                Text = purpose, Value = purpose
            }).ToList();

            return(View(new SendCodeViewModel {
                Providers = factorOptions, ReturnUrl = returnUrl, RememberMe = rememberMe
            }));
        }
예제 #10
0
        protected override void Dispose(bool disposing)
        {
            if (disposing)
            {
                if (AplicationUserService != null)
                {
                    AplicationUserService.Dispose();
                    AplicationUserService = null;
                }

                if (AplicationSignInService != null)
                {
                    AplicationSignInService.Dispose();
                    AplicationSignInService = null;
                }
            }

            base.Dispose(disposing);
        }
예제 #11
0
        public async Task <ActionResult> ChangePassword(ChangePasswordViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result = await AplicationUserService.ChangePasswordAsync(User.Identity.GetUserId(), model.OldPassword, model.NewPassword);

            if (result.Succeeded)
            {
                var user = await AplicationUserService.FindByIdAsync(User.Identity.GetUserId());

                if (user != null)
                {
                    await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                }
                return(RedirectToAction("Index", new { Message = ManageMessageId.ChangePasswordSuccess }));
            }
            AddErrors(result);
            return(View(model));
        }
예제 #12
0
        public async Task <ActionResult> RemoveLogin(string loginProvider, string providerKey)
        {
            ManageMessageId?message;
            var             result = await AplicationUserService.RemoveLoginAsync(User.Identity.GetUserId(), new UserLoginInfo(loginProvider, providerKey));

            if (result.Succeeded)
            {
                var user = await AplicationUserService.FindByIdAsync(User.Identity.GetUserId());

                if (user != null)
                {
                    await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                }
                message = ManageMessageId.RemoveLoginSuccess;
            }
            else
            {
                message = ManageMessageId.Error;
            }
            return(RedirectToAction("ManageLogins", new { Message = message }));
        }
예제 #13
0
        public async Task <ActionResult> SetPassword(SetPasswordViewModel model)
        {
            if (ModelState.IsValid)
            {
                var result = await AplicationUserService.AddPasswordAsync(User.Identity.GetUserId(), model.NewPassword);

                if (result.Succeeded)
                {
                    var user = await AplicationUserService.FindByIdAsync(User.Identity.GetUserId());

                    if (user != null)
                    {
                        await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                    }
                    return(RedirectToAction("Index", new { Message = ManageMessageId.SetPasswordSuccess }));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
예제 #14
0
        public async Task <ActionResult> VerifyPhoneNumber(VerifyPhoneNumberViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }
            var result = await AplicationUserService.ChangePhoneNumberAsync(User.Identity.GetUserId(), model.PhoneNumber, model.Code);

            if (result.Succeeded)
            {
                var user = await AplicationUserService.FindByIdAsync(User.Identity.GetUserId());

                if (user != null)
                {
                    await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                }
                return(RedirectToAction("Index", new { Message = ManageMessageId.AddPhoneSuccess }));
            }
            // If we got this far, something failed, redisplay form
            ModelState.AddModelError("", "Failed to verify phone");
            return(View(model));
        }
예제 #15
0
        public async Task <ActionResult> ExternalLoginConfirmation(ExternalLoginConfirmationViewModel model, string returnUrl)
        {
            if (User.Identity.IsAuthenticated)
            {
                return(RedirectToAction("Index", "Manage"));
            }

            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await AuthenticationManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    return(View("ExternalLoginFailure"));
                }
                var user = new ApplicationUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await AplicationUserService.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await AplicationUserService.AddLoginAsync(user.Id, info.Login);

                    if (result.Succeeded)
                    {
                        await AplicationSignInService.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewBag.ReturnUrl = returnUrl;
            return(View(model));
        }