public async Task <IActionResult> Register(RegisterViewModel model, string returnUrl = null)
        {
            ViewData["ReturnUrl"] = returnUrl;
            if (ModelState.IsValid)
            {
                var user = new TestSiteUser {
                    UserName = model.Username, Email = model.Email
                };
                var result = await _userManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    _logger.LogInformation("User created a new account with password.");

                    var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);

                    var callbackUrl = Url.EmailConfirmationLink(user.Id.ToString(), code, Request.Scheme);
                    await _emailSender.SendEmailAsync(model.Email, callbackUrl, "");

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

                    _logger.LogInformation("User created a new account with password.");
                    return(RedirectToLocal(returnUrl));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 2
0
        public async Task <IActionResult> ExternalLoginConfirmation(ExternalLoginViewModel model, string returnUrl = null)
        {
            if (ModelState.IsValid)
            {
                // Get the information about the user from the external login provider
                var info = await SignInManager.GetExternalLoginInfoAsync();

                if (info == null)
                {
                    throw new ApplicationException("Error loading external login information during confirmation.");
                }
                var user = new TestSiteUser {
                    UserName = model.Email, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user);

                if (result.Succeeded)
                {
                    result = await UserManager.AddLoginAsync(user, info);

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

                        Logger.LogInformation("User created an account using {Name} provider.", info.LoginProvider);
                        return(RedirectToLocal(returnUrl));
                    }
                }
                AddErrors(result);
            }

            ViewData["ReturnUrl"] = returnUrl;
            return(View(nameof(ExternalLogin), model));
        }
        public async Task <ActionResult> Edit(TestSiteUser user)
        {
            var filter = Builders <TestSiteUser> .Filter.Eq(x => x.Id, user.Id);

            await UserCollection.ReplaceOneAsync(filter, user);

            return(Redirect("/user"));
        }
        private async Task LoadSharedKeyAndQrCodeUriAsync(TestSiteUser user, EnableAuthenticatorViewModel model)
        {
            var unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);

            if (string.IsNullOrEmpty(unformattedKey))
            {
                await _userManager.ResetAuthenticatorKeyAsync(user);

                unformattedKey = await _userManager.GetAuthenticatorKeyAsync(user);
            }

            model.SharedKey        = FormatKey(unformattedKey);
            model.AuthenticatorUri = GenerateQrCodeUri(user.Email, unformattedKey);
        }
Exemplo n.º 5
0
        private async Task TestUserRoles()
        {
            if (await RoleManager.RoleExistsAsync(TestData.RoleName))
            {
                await RoleManager.DeleteAsync(await RoleManager.FindByNameAsync(TestData.RoleName));
            }

            IdentityResult roleResult = await RoleManager.CreateAsync(new MongoRole(TestData.RoleName));

            if (!roleResult.Succeeded || !await RoleManager.RoleExistsAsync(TestData.RoleName))
            {
                throw new Exception("Add role fails");
            }


            MongoRole role = await RoleManager.FindByNameAsync(TestData.RoleName);

            TestSiteUser user = await UserManager.FindByEmailAsync(TestData.Email);

            if (user == null)
            {
                user = new TestSiteUser {
                    UserName = TestData.Username, Email = TestData.Email
                };
                IdentityResult result = await UserManager.CreateAsync(user, TestData.Password);
            }

            IdentityResult addRoleResult = await UserManager.AddToRoleAsync(user, TestData.RoleName);

            if (!addRoleResult.Succeeded || !await UserManager.IsInRoleAsync(user, TestData.RoleName))
            {
                throw new Exception("Add role to user fails");
            }


            IdentityResult removeRoleResult = await UserManager.RemoveFromRoleAsync(user, TestData.RoleName);

            if (!removeRoleResult.Succeeded || await UserManager.IsInRoleAsync(user, TestData.RoleName))
            {
                throw new Exception("Remove user from role fails");
            }

            TestSiteUser userWithoutRole = await UserManager.FindByEmailAsync(TestData.Email);

            if (userWithoutRole.Roles.Any(r => r == role.Id.ToString()))
            {
                throw new Exception("Remove user from role fails");
            }
        }
Exemplo n.º 6
0
        public async Task Register(RegisterViewModel model, string returnUrl = null)
        {
            var user = new TestSiteUser {
                UserName = model.Username, Email = model.Email
            };
            var result = await UserManager.CreateAsync(user, model.Password);

            if (!result.Succeeded)
            {
                throw new ValidationException(result.Errors);
            }

            var code = await UserManager.GenerateEmailConfirmationTokenAsync(user);

            await EmailSender.SendMailConfirmationLink(user.Id, code);

            await SignInManager.SignInAsync(user, isPersistent : false);
        }
Exemplo n.º 7
0
        private async Task TestAuthenticationTokens()
        {
            TestSiteUser user = await UserManager.FindByEmailAsync(TestData.Email);

            await UserManager.SetAuthenticationTokenAsync(user, TestData.LoginProvider, TestData.TokenName, TestData.TokenValue);

            var token = await UserManager.GetAuthenticationTokenAsync(user, TestData.LoginProvider, TestData.TokenName);

            if (token != TestData.TokenValue)
            {
                throw new AutheticationTokenException("Authentication token fails");
            }

            var res = await UserManager.RemoveAuthenticationTokenAsync(user, TestData.LoginProvider, TestData.TokenName);

            if (!res.Succeeded || await UserManager.GetAuthenticationTokenAsync(user, TestData.LoginProvider, TestData.TokenName) != null)
            {
                throw new AutheticationTokenException("Authentication token fails");
            }
        }
Exemplo n.º 8
0
        private async Task TestClaims()
        {
            TestSiteUser user = await UserManager.FindByEmailAsync(TestData.Email);

            var claim = new Claim(TestData.ClaimType, TestData.ClaimValue, TestData.ClaimIssuer);

            if (!(await UserManager.AddClaimAsync(user, claim)).Succeeded)
            {
                throw new ClaimFailsException("Failed add claim");
            }

            if ((await UserManager.GetClaimsAsync(user)).All(x => x.Value != TestData.ClaimValue))
            {
                throw new ClaimFailsException("Failed retrieve claim");
            }

            await UserManager.RemoveClaimAsync(user, claim);

            if ((await UserManager.GetClaimsAsync(user)).Any(x => x.Value == TestData.ClaimValue))
            {
                throw new ClaimFailsException("Failed removed claim");
            }
        }
        public async Task <ActionResult> Edit(TestSiteUser user)
        {
            await _userUserCollection.ReplaceOneAsync(x => x.Id == user.Id, user);

            return(Redirect("/user"));
        }