Пример #1
0
        public async Task <IActionResult> Register(RegisterModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;

            if (!ModelState.IsValid)
            {
                return(View("Error"));
            }

            ConfaqueUser user = new ConfaqueUser()
            {
                UserName  = model.Email,
                Email     = model.Email,
                BirthDate = model.BirthDate
            };

            // User manager creates the user.
            IdentityResult result = await this._userManager.CreateAsync(user, model.Password);

            // Role manager creates the organizer role if it is not created already.
            // This is needed because the role has to exist before a user can be added to the role.
            if (!await this._roleManager.RoleExistsAsync("Organizer").ConfigureAwait(false))
            {
                await this._roleManager.CreateAsync(new IdentityRole("Organizer")).ConfigureAwait(false);
            }

            // Same is done for the speaker what is done for the organizer above.
            if (!await this._roleManager.RoleExistsAsync("Speaker").ConfigureAwait(false))
            {
                await this._roleManager.CreateAsync(new IdentityRole("Speaker")).ConfigureAwait(false);
            }

            // Adds user to the role and the claim.
            await this._userManager.AddToRoleAsync(user, model.Role).ConfigureAwait(false);

            await this._userManager.AddClaimAsync(user, new Claim("technology", model.Technology)).ConfigureAwait(false);

            if (result.Succeeded)
            {
                string code = await this._userManager.GenerateEmailConfirmationTokenAsync(user).ConfigureAwait(false);

                string callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, Request.Scheme);
                await this._emailService.SendEmailAsync(user.Email, "Confirm Account", $"Please confirm your account by clicking this link {callbackUrl}").ConfigureAwait(false);

                await this._signInManager.SignInAsync(user, isPersistent : false).ConfigureAwait(false);

                return(View("RegistrationConfirmation"));
            }

            foreach (IdentityError error in result.Errors)
            {
                ModelState.TryAddModelError("error", error.Description);
            }

            return(View(model));
        }
Пример #2
0
        public async Task <IActionResult> ToggleTwoFactorAuthentication()
        {
            ConfaqueUser user = await this._userManager.GetUserAsync(this.User).ConfigureAwait(false);

            if (user.TwoFactorEnabled)
            {
                await this._userManager.SetTwoFactorEnabledAsync(user, false).ConfigureAwait(false);

                return(RedirectToAction("Index"));
            }

            return(RedirectToAction("EnableAuthenticator"));
        }
Пример #3
0
        public async Task <IActionResult> Index()
        {
            ConfaqueUser user = await this._userManager.GetUserAsync(this.User).ConfigureAwait(false);

            ManageAccountModel model = new ManageAccountModel()
            {
                Email    = user.Email,
                Username = user.UserName,
                IsTwoFactorAuthEnabled = user.TwoFactorEnabled,
                IsEmailConfirmed       = user.EmailConfirmed,
            };

            return(View(model));
        }
Пример #4
0
        public async Task <IActionResult> TwoFactorLogin(bool rememberMe, string returnUrl = null)
        {
            ConfaqueUser user = await this._signInManager.GetTwoFactorAuthenticationUserAsync().ConfigureAwait(false);

            if (user == null)
            {
                throw new ApplicationException("User is null");
            }

            TwoFactorLoginModel model = new TwoFactorLoginModel()
            {
                RememberMe = rememberMe
            };

            ViewData["ReturnUrl"] = returnUrl;
            return(View(model));
        }
Пример #5
0
        public async Task <IActionResult> ConfirmEmail(string userId, string code)
        {
            if (userId == null || code == null)
            {
                return(NotFound());
            }

            ConfaqueUser user = await this._userManager.FindByIdAsync(userId).ConfigureAwait(false);

            if (user == null)
            {
                return(NotFound());
            }

            IdentityResult result = await this._userManager.ConfirmEmailAsync(user, code).ConfigureAwait(false);

            return(View(result != null && result.Succeeded ? "ConfirmEmail" : "Error"));
        }
Пример #6
0
        public async Task <IActionResult> EnableAuthenticator(AuthenicatorModel model)
        {
            if (model == null)
            {
                throw new Exception("Model is null");
            }

            if (!this.ModelState.IsValid)
            {
                return(View(model));
            }

            if (string.IsNullOrEmpty(model.Code))
            {
                this.ModelState.AddModelError("model.Code", "Verification code not provided");
            }

            ConfaqueUser user = await this._userManager.GetUserAsync(this.User).ConfigureAwait(false);

            if (user == null)
            {
                throw new ApplicationException("User not found");
            }

            bool is2faTokenValid = await this._userManager.VerifyTwoFactorTokenAsync(
                user,
                this._userManager.Options.Tokens.AuthenticatorTokenProvider,
                model.Code).ConfigureAwait(false);

            if (!is2faTokenValid)
            {
                this.ModelState.AddModelError("model.Code", "Code is invalid");
            }

            IdentityResult result = await this._userManager.SetTwoFactorEnabledAsync(user, true).ConfigureAwait(false);

            if (!result.Succeeded)
            {
                this.ModelState.AddModelError("is2faEnabled", "Cannot Enable 2FA");
            }

            return(RedirectToAction("Index"));
        }
Пример #7
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                ConfaqueUser user = new ConfaqueUser()
                {
                    UserName = model.Email, Email = model.Email
                };
                IdentityResult result = await this._userManager.CreateAsync(user).ConfigureAwait(false);

                if (result.Succeeded)
                {
                    ExternalLoginInfo info = await this._signInManager.GetExternalLoginInfoAsync().ConfigureAwait(false);

                    result = await this._userManager.AddLoginAsync(user, info).ConfigureAwait(false);

                    return(await this.RedirectToLocal(returnUrl).ConfigureAwait(false));
                }
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }
Пример #8
0
        public async Task <IActionResult> EnableAuthenticator()
        {
            ConfaqueUser user = await this._userManager.GetUserAsync(this.User).ConfigureAwait(false);

            string key = await this._userManager.GetAuthenticatorKeyAsync(user).ConfigureAwait(false);

            if (string.IsNullOrEmpty(key))
            {
                IdentityResult result = await this._userManager.ResetAuthenticatorKeyAsync(user).ConfigureAwait(false);

                if (result.Succeeded)
                {
                    key = await this._userManager.GetAuthenticatorKeyAsync(user).ConfigureAwait(false);
                }
            }

            AuthenicatorModel model = new AuthenicatorModel()
            {
                SharedKey        = this.FormatKey(key),
                AuthenticatorUri = this.GenerateQRCodeUri(user.Email, key)
            };

            return(View(model));
        }