Example #1
0
        public async Task <IActionResult> Cancel(string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ResetPassword
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                // TODO: clear token if account is there
                this.ModelState.AddModelError(
                    IdentityBaseConstants.ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            string returnUrl = result.UserAccount.VerificationStorage;

            await this._userAccountService
            .ClearVerificationAsync(result.UserAccount);

            return(this.Redirect(
                       this.Url.Action(
                           "Index",
                           "Login",
                           new { ReturnUrl = returnUrl }
                           )
                       ));
        }
        public async Task <IActionResult> Cancel([FromQuery] string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ResetPassword
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                if (result.UserAccount != null)
                {
                    await this._userAccountService
                    .ClearVerificationAsync(result.UserAccount);
                }

                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.TokenIsInvalid]);

                return(this.View("InvalidToken"));
            }

            string returnUrl = result.UserAccount.VerificationStorage;

            await this._userAccountService
            .ClearVerificationAsync(result.UserAccount);

            return(this.RedirectToLogin(returnUrl));
        }
        public async Task <IActionResult> Confirm([FromQuery] string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ResetPassword
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.TokenIsInvalid]);

                return(this.View("InvalidToken"));
            }

            ConfirmViewModel vm = new ConfirmViewModel
            {
                Email = result.UserAccount.Email
            };

            return(this.View("Confirm", vm));
        }
Example #4
0
        public async Task <IActionResult> Confirm(string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ResetPassword
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                this.ModelState.AddModelError(IdentityBaseConstants
                                              .ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            ConfirmViewModel vm = new ConfirmViewModel
            {
                Key   = key,
                Email = result.UserAccount.Email
            };

            return(this.View(vm));
        }
Example #5
0
        public async Task <IActionResult> Confirm(string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ConfirmAccount
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                this.ModelState.AddModelError(
                    IdentityBaseConstants.ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            // User account requires completion
            if (this._applicationOptions.EnableInvitationCreateEndpoint &&
                result.UserAccount.CreationKind == CreationKind.Invitation)
            {
                ConfirmViewModel vm = new ConfirmViewModel
                {
                    Key = key,
                    RequiresPassword = !result.UserAccount.HasPassword(),
                    Email            = result.UserAccount.Email
                };

                return(this.View(vm));
            }
            // User profile already fine and just needs to be activated
            else
            {
                string returnUrl = result.UserAccount.VerificationKey;

                await this._userAccountService
                .SetEmailVerifiedAsync(result.UserAccount);

                if (this._applicationOptions.LoginAfterAccountConfirmation)
                {
                    await this.HttpContext
                    .SignInAsync(result.UserAccount, null);

                    if (returnUrl != null &&
                        this._interaction.IsValidReturnUrl(returnUrl))
                    {
                        return(this.Redirect(returnUrl));
                    }
                }

                return(this.Redirect(
                           this.Url.Action(
                               "Login",
                               "Login",
                               new { ReturnUrl = returnUrl }
                               )
                           ));
            }
        }
Example #6
0
        public async Task <IActionResult> Confirm(string key)
        {
            // Check token integrity
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ChangeEmail
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                this.ModelState.AddModelError(IdentityBaseConstants
                                              .ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            // TODO: Move to verification storage reader or something
            string[] storage = JsonConvert.DeserializeObject <string[]>(
                result.UserAccount.VerificationStorage);

            string email     = storage[0];
            string returnUrl = storage[1];

            // Check if new email address is already taken
            if (await this._userAccountService
                .LoadByEmailAsync(email) != null)
            {
                this.ModelState.AddModelError(IdentityBaseConstants
                                              .ErrorMessages.EmailAddressAlreadyTaken);

                ConfirmViewModel vm = new ConfirmViewModel
                {
                    Key       = key,
                    ReturnUrl = returnUrl,
                    Email     = result.UserAccount.Email
                };

                return(this.View(vm));
            }

            // Update user account if email still available
            await this._userAccountService.SetNewEmailAsync(
                result.UserAccount,
                email
                );

            if (this._interaction.IsValidReturnUrl(returnUrl))
            {
                return(this.Redirect(returnUrl));
            }

            throw new ApplicationException("Invalid return URL");
        }
        public async Task <IActionResult> Confirm(
            [FromQuery] string key,
            ConfirmInputModel model)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ResetPassword
                );

            if (result.UserAccount == null ||
                result.TokenExpired ||
                !result.PurposeValid)
            {
                if (result.UserAccount != null)
                {
                    await this._userAccountService
                    .ClearVerificationAsync(result.UserAccount);
                }

                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.TokenIsInvalid]);

                return(this.View("InvalidToken"));
            }

            if (!ModelState.IsValid)
            {
                return(View(new ConfirmViewModel
                {
                    Email = result.UserAccount.Email
                }));
            }

            string returnUrl = result.UserAccount.VerificationStorage;

            await this._userAccountService.SetNewPasswordAsync(
                result.UserAccount,
                model.Password
                );

            if (this._applicationOptions.CancelAfterAccountRecovery)
            {
                return(this.View("Complete"));
            }
            else if (this._applicationOptions.LoginAfterAccountRecovery)
            {
                await this._authenticationService
                .SignInAsync(result.UserAccount, returnUrl);

                return(this.RedirectToReturnUrl(returnUrl, this._interaction));
            }

            return(this.RedirectToLogin(returnUrl));
        }
Example #8
0
        public async Task <IActionResult> Confirm(ConfirmInputModel model)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                model.Key,
                VerificationKeyPurpose.ResetPassword
                );

            if (result.UserAccount == null ||
                result.TokenExpired ||
                !result.PurposeValid)
            {
                // TODO: clear token if account is there

                this.ModelState.AddModelError(
                    IdentityBaseConstants.ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            if (!ModelState.IsValid)
            {
                return(View(new ConfirmViewModel
                {
                    Key = model.Key,
                    Email = result.UserAccount.Email
                }));
            }

            string returnUrl = result.UserAccount.VerificationStorage;

            await this._userAccountService.SetNewPasswordAsync(
                result.UserAccount,
                model.Password
                );

            if (this._applicationOptions.LoginAfterAccountRecovery)
            {
                await this.HttpContext.SignInAsync(result.UserAccount, null);

                if (this._interaction.IsValidReturnUrl(returnUrl))
                {
                    return(this.Redirect(returnUrl));
                }
            }

            return(this.Redirect(
                       this.Url.Action(
                           "Index",
                           "Login",
                           new { ReturnUrl = returnUrl }
                           )
                       ));
        }
Example #9
0
        public async Task <IActionResult> Cancel([FromQuery] string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ChangeEmail
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                if (result.UserAccount != null)
                {
                    await this._userAccountService
                    .ClearVerificationAsync(result.UserAccount);
                }

                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.TokenIsInvalid]);

                return(this.View("InvalidToken"));
            }

            await this._userAccountService
            .ClearVerificationAsync(result.UserAccount);

            string[] storage = JsonConvert.DeserializeObject <string[]>(
                result.UserAccount.VerificationStorage);

            string email     = storage[0];
            string returnUrl = storage[1];

            if (this._interaction.IsValidReturnUrl(returnUrl))
            {
                return(this.Redirect(returnUrl));
            }

            throw new ApplicationException("Invalid return URL");
        }
Example #10
0
        public async Task <IActionResult> Cancel(string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ChangeEmail
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                // TODO: clear token if account is there

                this.ModelState.AddModelError(
                    IdentityBaseConstants.ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            await this._userAccountService
            .ClearVerificationAsync(result.UserAccount);

            // TODO: Move to verification storage reader or something
            string[] storage = JsonConvert.DeserializeObject <string[]>(
                result.UserAccount.VerificationStorage);

            string email     = storage[0];
            string returnUrl = storage[1];

            if (this._interaction.IsValidReturnUrl(returnUrl))
            {
                return(this.Redirect(returnUrl));
            }

            throw new ApplicationException("Invalid return URL");
        }
Example #11
0
        public async Task <IActionResult> Confirm([FromQuery] string key)
        {
            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ConfirmAccount
                );

            if (result.UserAccount == null ||
                !result.PurposeValid ||
                result.TokenExpired)
            {
                if (result.UserAccount != null)
                {
                    await this._userAccountService
                    .ClearVerificationAsync(result.UserAccount);
                }

                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.TokenIsInvalid]);

                return(this.View("InvalidToken"));
            }

            // User account requires completion.
            if (this._applicationOptions.EnableAccountInvitation &&
                result.UserAccount.CreationKind == CreationKind.Invitation)
            {
                // TODO: move invitation confirmation to own contoller
                //       listening on /invitation/confirm

                ConfirmViewModel vm = new ConfirmViewModel
                {
                    RequiresPassword = !result.UserAccount.HasPassword(),
                    Email            = result.UserAccount.Email
                };

                return(this.View("Confirm", vm));
            }
            // User account already fine and can be authenticated.
            else
            {
                // TODO: Refactor so the db will hit only once in case
                //       LoginAfterAccountConfirmation is true

                string returnUrl = result.UserAccount.VerificationStorage;

                await this._userAccountService
                .SetEmailVerifiedAsync(result.UserAccount);

                if (this._applicationOptions.LoginAfterAccountConfirmation)
                {
                    await this._authenticationService
                    .SignInAsync(result.UserAccount, returnUrl);

                    return(this.RedirectToReturnUrl(
                               returnUrl, this._interaction));
                }
                else if (this._applicationOptions.CancelAfterAccountConfirmation)
                {
                    return(this.View("Complete"));
                }

                return(this.RedirectToLogin(returnUrl));
            }
        }
        public async Task <IActionResult> Confirm(
            [FromQuery] string key,
            ConfirmInputModel model)
        {
            if (!this._applicationOptions.EnableAccountInvitation)
            {
                return(this.NotFound());
            }

            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                key,
                VerificationKeyPurpose.ConfirmAccount
                );

            if (result.UserAccount == null ||
                result.TokenExpired ||
                !result.PurposeValid)
            {
                if (result.UserAccount != null)
                {
                    await this._userAccountService
                    .ClearVerificationAsync(result.UserAccount);
                }

                this.ModelState.AddModelError(
                    this._localizer[ErrorMessages.TokenIsInvalid]);

                return(this.View("InvalidToken"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View("Confirm", new ConfirmViewModel
                {
                    Email = result.UserAccount.Email
                }));
            }

            string returnUrl = result.UserAccount.VerificationStorage;

            this._userAccountService.SetEmailVerified(result.UserAccount);

            this._userAccountService
            .AddLocalCredentials(result.UserAccount, model.Password);

            await this._userAccountService
            .UpdateUserAccountAsync(result.UserAccount);

            if (this._applicationOptions.CancelAfterAccountConfirmation)
            {
                // return this.View("Complete");
                return(this.RedirectToAction(
                           "complete",
                           new { ReturnUrl = returnUrl }
                           ));
            }
            else if (result.UserAccount.CreationKind == CreationKind.Invitation)
            {
                return(this.RedirectToReturnUrl(
                           returnUrl, this._interaction));
            }
            else
            {
                if (this._applicationOptions.LoginAfterAccountRecovery)
                {
                    await this._authenticationService
                    .SignInAsync(result.UserAccount, returnUrl);

                    return(this.RedirectToReturnUrl(
                               returnUrl, this._interaction));
                }

                return(this.RedirectToLogin(returnUrl));
            }
        }
Example #13
0
        public async Task <IActionResult> Confirm(ConfirmInputModel model)
        {
            if (!this._applicationOptions.EnableInvitationCreateEndpoint)
            {
                return(this.NotFound());
            }

            TokenVerificationResult result = await this._userAccountService
                                             .HandleVerificationKeyAsync(
                model.Key,
                VerificationKeyPurpose.ConfirmAccount
                );

            if (result.UserAccount == null ||
                result.TokenExpired ||
                !result.PurposeValid)
            {
                // TODO: clear token if account is there

                this.ModelState.AddModelError(
                    IdentityBaseConstants.ErrorMessages.TokenIsInvalid);

                return(this.View("InvalidToken"));
            }

            if (!this.ModelState.IsValid)
            {
                return(this.View(new ConfirmViewModel
                {
                    Key = model.Key,
                    Email = result.UserAccount.Email
                }));
            }

            string returnUrl = result.UserAccount.VerificationStorage;

            this._userAccountService.SetEmailVerified(result.UserAccount);

            this._userAccountService
            .AddLocalCredentials(result.UserAccount, model.Password);

            await this._userAccountService
            .UpdateUserAccountAsync(result.UserAccount);

            if (result.UserAccount.CreationKind == CreationKind.Invitation)
            {
                // TODO: validate
                return(this.Redirect(returnUrl));
            }
            else
            {
                if (this._applicationOptions.LoginAfterAccountRecovery)
                {
                    await this.HttpContext
                    .SignInAsync(result.UserAccount, null);

                    if (this._interaction.IsValidReturnUrl(returnUrl))
                    {
                        return(this.Redirect(returnUrl));
                    }
                }

                return(this.Redirect(
                           this.Url.Action(
                               "Index",
                               "Login",
                               new { ReturnUrl = returnUrl }
                               )
                           ));
            }
        }