Пример #1
0
        protected internal virtual async Task <UserEntity> EnsureLoginUserAsync(UserLoginModel userLogin)
        {
            if (userLogin == null)
            {
                throw new ArgumentNullException(nameof(userLogin));
            }

            UserEntity loginUser = null;

            if (userLogin.Id != null)
            {
                loginUser = await this.DatabaseContext.Users.FindAsync(userLogin.Id);
            }

            if (loginUser == null)
            {
                loginUser = new UserEntity {
                    Id = userLogin.Id ?? Guid.NewGuid().ToString(), UserName = Guid.NewGuid().ToString()
                };
                loginUser.NormalizedUserName = loginUser.UserName.ToUpperInvariant();
                await this.DatabaseContext.Users.AddAsync(loginUser);
            }
            else
            {
                if (loginUser.PasswordHash != null)
                {
                    throw new InvalidOperationException($"The user with id {loginUser.Id.ToStringRepresentation()} already exists with a password and can not be associated with a login.");
                }

                loginUser.SecurityStamp = Guid.NewGuid().ToString();
            }

            return(loginUser);
        }
Пример #2
0
        public virtual async Task <IdentityResult> DeleteUserAsync(UserEntity user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            return(await this.UserManager.DeleteAsync(user));
        }
Пример #3
0
        protected internal virtual async Task <UserEntity> UserModelToUserEntityAsync(UserModel user)
        {
            UserEntity userEntity = null;

            if (user != null)
            {
                userEntity = new UserEntity {
                    Email = user.Email, EmailConfirmed = !string.IsNullOrWhiteSpace(user.Email), UserName = user.UserName
                };

                if (user.Id != null)
                {
                    userEntity.Id = user.Id;
                }
            }

            return(await Task.FromResult(userEntity));
        }
Пример #4
0
        protected internal virtual async Task SaveClaimsAsync(IClaimBuilderCollection claims, UserEntity user)
        {
            var logPrefix = $"{this.GetType().FullName}.{nameof(this.SaveClaimsAsync)}:";

            this.Logger.LogDebugIfEnabled($"{logPrefix} user-id = {user?.Id.ToStringRepresentation()}, starting...");

            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }

            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }

            try
            {
                var comparer     = StringComparer.Ordinal;
                var sortedClaims = new ClaimBuilderCollection();

                foreach (var claim in claims.OrderBy(claim => claim.Type, comparer).ThenBy(claim => claim.Value, comparer))
                {
                    sortedClaims.Add(new ClaimBuilder {
                        Type = claim.Type, Value = claim.Value
                    });
                }

                var i = 0;
                var userClaimsToRemove = new List <IdentityUserClaim <string> >();

                foreach (var userClaim in this.DatabaseContext.UserClaims.Where(claim => claim.UserId == user.Id).OrderBy(claim => claim.Id))
                {
                    if (sortedClaims.Count < i + 1)
                    {
                        userClaimsToRemove.Add(userClaim);
                    }
                    else
                    {
                        var claim = sortedClaims[i];
                        const StringComparison comparison = StringComparison.OrdinalIgnoreCase;

                        if (!string.Equals(claim.Type, userClaim.ClaimType, comparison) || !string.Equals(claim.Value, userClaim.ClaimValue, comparison))
                        {
                            this.Logger.LogDebugIfEnabled($"{logPrefix} changing claim with id {userClaim.Id.ToStringRepresentation()} from type {userClaim.ClaimType.ToStringRepresentation()} to type {claim.Type.ToStringRepresentation()} and from value {userClaim.ClaimValue.ToStringRepresentation()} to value {claim.Value.ToStringRepresentation()}.");

                            userClaim.ClaimType  = claim.Type;
                            userClaim.ClaimValue = claim.Value;
                        }
                    }

                    i++;
                }

                if (userClaimsToRemove.Any())
                {
                    this.Logger.LogDebugIfEnabled($"{logPrefix} removing {userClaimsToRemove.Count} claims with id's: {string.Join(", ", userClaimsToRemove.Select(userClaim => userClaim.Id))}");
                    this.DatabaseContext.UserClaims.RemoveRange(userClaimsToRemove);
                }
                else if (sortedClaims.Count > i)
                {
                    foreach (var claim in sortedClaims.Skip(i))
                    {
                        var claimToAdd = new IdentityUserClaim <string>
                        {
                            ClaimType  = claim.Type,
                            ClaimValue = claim.Value,
                            UserId     = user.Id
                        };

                        this.Logger.LogDebugIfEnabled($"{logPrefix} adding claim with type {claim.Type.ToStringRepresentation()} and value {claim.Value.ToStringRepresentation()}.");
                        await this.DatabaseContext.UserClaims.AddAsync(claimToAdd);
                    }
                }

                var savedChanges = await this.DatabaseContext.SaveChangesAsync();

                this.Logger.LogDebugIfEnabled($"{logPrefix} saved changes = {savedChanges}");
            }
            catch (Exception exception)
            {
                throw new InvalidOperationException("Could not save claims for user.", exception);
            }
        }
Пример #5
0
        public virtual async Task <UserEntity> ResolveUserAsync(IClaimBuilderCollection claims, string provider, string userIdentifier)
        {
            if (claims == null)
            {
                throw new ArgumentNullException(nameof(claims));
            }

            if (provider == null)
            {
                throw new ArgumentNullException(nameof(provider));
            }

            if (userIdentifier == null)
            {
                throw new ArgumentNullException(nameof(userIdentifier));
            }

            var user = await this.GetUserAsync(provider, userIdentifier);

            var userClaims = (user != null ? await this.UserManager.GetClaimsAsync(user) : null) ?? new List <Claim>();

            if (user != null && await this.ClaimsAreEqualAsync(claims, userClaims))
            {
                return(user);
            }

            var autoSaveChanges = this.UserManager.Store.AutoSaveChanges;

            this.UserManager.Store.AutoSaveChanges = true;
            var userExists = user != null;

            try
            {
                if (!userExists)
                {
                    user = new UserEntity
                    {
                        UserName = Guid.NewGuid().ToString()
                    };

                    var identityResult = await this.UserManager.CreateAsync(user);

                    if (!identityResult.Succeeded)
                    {
                        throw new InvalidOperationException($"Could not create user: {await this.CreateErrorMessageAsync(identityResult)}");
                    }

                    identityResult = await this.UserManager.AddLoginAsync(user, new UserLoginInfo(provider, userIdentifier, provider));

                    if (!identityResult.Succeeded)
                    {
                        throw new InvalidOperationException($"Could not add login for user: {await this.CreateErrorMessageAsync(identityResult)}");
                    }
                }

                await this.SaveClaimsAsync(claims, user);

                return(user);
            }
            finally
            {
                this.UserManager.Store.AutoSaveChanges = autoSaveChanges;
            }
        }