コード例 #1
0
        public async Task Create_should_add_user()
        {
            var identity = CreateIdentity(found: false);

            var values = new UserValues
            {
                Email = identity.Email
            };

            SetupCreation(identity, 1);

            await sut.CreateAsync(values.Email, values);

            A.CallTo(() => userEvents.OnUserRegistered(A <IUser> .That.Matches(x => x.Identity == identity)))
            .MustHaveHappened();

            A.CallTo(() => userEvents.OnConsentGiven(A <IUser> .That.Matches(x => x.Identity == identity)))
            .MustNotHaveHappened();

            A.CallTo(() => userManager.AddToRoleAsync(identity, A <string> ._))
            .MustNotHaveHappened();

            A.CallTo(() => userManager.AddPasswordAsync(identity, A <string> ._))
            .MustNotHaveHappened();

            A.CallTo(() => userManager.SetLockoutEndDateAsync(identity, A <DateTimeOffset> ._))
            .MustNotHaveHappened();
        }
コード例 #2
0
        public async Task <IActionResult> PostUser([FromBody] CreateUserDto request)
        {
            var user = await userManager.CreateAsync(userFactory, request.ToValues());

            userEvents.OnUserRegistered(user);

            var response = UserDto.FromUser(user, Resources);

            return(Ok(response));
        }
コード例 #3
0
        public async Task <IActionResult> ExternalCallback(string returnUrl = null)
        {
            var externalLogin = await signInManager.GetExternalLoginInfoWithDisplayNameAsync();

            if (externalLogin == null)
            {
                return(RedirectToAction(nameof(Login)));
            }

            var result = await signInManager.ExternalLoginSignInAsync(externalLogin.LoginProvider, externalLogin.ProviderKey, true);

            if (!result.Succeeded && result.IsLockedOut)
            {
                return(View(nameof(LockedOut)));
            }

            var isLoggedIn = result.Succeeded;

            IUser user = null;

            if (!isLoggedIn)
            {
                var email = externalLogin.Principal.FindFirst(ClaimTypes.Email).Value;

                user = await userManager.FindByEmailAsync(email);

                if (user != null)
                {
                    isLoggedIn =
                        await AddLoginAsync(user, externalLogin) &&
                        await LoginAsync(externalLogin);
                }
                else
                {
                    user = CreateUser(externalLogin, email);

                    var isFirst = userManager.Users.LongCount() == 0;

                    isLoggedIn =
                        await AddUserAsync(user) &&
                        await AddLoginAsync(user, externalLogin) &&
                        await MakeAdminAsync(user, isFirst) &&
                        await LockAsync(user, isFirst) &&
                        await LoginAsync(externalLogin);

                    userEvents.OnUserRegistered(user);

                    if (user.IsLocked)
                    {
                        return(View(nameof(LockedOut)));
                    }
                }
            }

            if (!isLoggedIn)
            {
                return(RedirectToAction(nameof(Login)));
            }
            else if (user != null && !user.HasConsent())
            {
                return(RedirectToAction(nameof(Consent), new { returnUrl }));
            }
            else
            {
                return(RedirectToReturnUrl(returnUrl));
            }
        }
コード例 #4
0
        public async Task <IActionResult> ExternalCallback(string?returnUrl = null)
        {
            var externalLogin = await signInManager.GetExternalLoginInfoWithDisplayNameAsync();

            if (externalLogin == null)
            {
                return(RedirectToAction(nameof(Login)));
            }

            var result = await signInManager.ExternalLoginSignInAsync(externalLogin.LoginProvider, externalLogin.ProviderKey, true);

            if (!result.Succeeded && result.IsLockedOut)
            {
                return(View(nameof(LockedOut)));
            }

            var isLoggedIn = result.Succeeded;

            UserWithClaims?user;

            if (isLoggedIn)
            {
                user = await userManager.FindByLoginWithClaimsAsync(externalLogin.LoginProvider, externalLogin.ProviderKey);
            }
            else
            {
                var email = externalLogin.Principal.FindFirst(ClaimTypes.Email)?.Value !;

                user = await userManager.FindByEmailWithClaimsAsync(email);

                if (user != null)
                {
                    isLoggedIn =
                        await AddLoginAsync(user, externalLogin) &&
                        await AddClaimsAsync(user, externalLogin, email) &&
                        await LoginAsync(externalLogin);
                }
                else
                {
                    user = new UserWithClaims(userFactory.Create(email), new List <Claim>());

                    var isFirst = userManager.Users.LongCount() == 0;

                    isLoggedIn =
                        await AddUserAsync(user) &&
                        await AddLoginAsync(user, externalLogin) &&
                        await AddClaimsAsync(user, externalLogin, email, isFirst) &&
                        await LockAsync(user, isFirst) &&
                        await LoginAsync(externalLogin);

                    userEvents.OnUserRegistered(user);

                    if (await userManager.IsLockedOutAsync(user.Identity))
                    {
                        return(View(nameof(LockedOut)));
                    }
                }
            }

            if (!isLoggedIn)
            {
                return(RedirectToAction(nameof(Login)));
            }
            else if (user != null && !user.HasConsent() && !identityOptions.NoConsent)
            {
                return(RedirectToAction(nameof(Consent), new { returnUrl }));
            }
            else
            {
                return(RedirectToReturnUrl(returnUrl));
            }
        }