示例#1
0
        /// <summary>
        ///     CreateAsync a ClaimsIdentity from a user
        /// </summary>
        /// <param name="manager"></param>
        /// <param name="user"></param>
        /// <param name="authenticationType"></param>
        /// <param name="cancellationToken"></param>
        /// <returns></returns>
        public virtual async Task <ClaimsIdentity> CreateAsync(TUser user, ClaimsIdentityOptions options,
                                                               CancellationToken cancellationToken = default(CancellationToken))
        {
            if (user == null)
            {
                throw new ArgumentNullException("user");
            }
            if (options == null)
            {
                throw new ArgumentNullException("options");
            }
            var userId = await UserManager.GetUserIdAsync(user, cancellationToken);

            var userName = await UserManager.GetUserNameAsync(user, cancellationToken);

            var id = new ClaimsIdentity(options.AuthenticationType, options.UserNameClaimType,
                                        options.RoleClaimType);

            id.AddClaim(new Claim(options.UserIdClaimType, userId));
            id.AddClaim(new Claim(options.UserNameClaimType, userName, ClaimValueTypes.String));
            if (UserManager.SupportsUserSecurityStamp)
            {
                id.AddClaim(new Claim(options.SecurityStampClaimType,
                                      await UserManager.GetSecurityStampAsync(user, cancellationToken)));
            }
            if (UserManager.SupportsUserRole)
            {
                var roles = await UserManager.GetRolesAsync(user, cancellationToken);

                foreach (var roleName in roles)
                {
                    id.AddClaim(new Claim(options.RoleClaimType, roleName, ClaimValueTypes.String));
                    if (RoleManager.SupportsRoleClaims)
                    {
                        var role = await RoleManager.FindByNameAsync(roleName);

                        if (role != null)
                        {
                            id.AddClaims(await RoleManager.GetClaimsAsync(role, cancellationToken));
                        }
                    }
                }
            }
            if (UserManager.SupportsUserClaim)
            {
                id.AddClaims(await UserManager.GetClaimsAsync(user, cancellationToken));
            }
            return(id);
        }
示例#2
0
        /// <summary>
        /// Creates a <see cref="ClaimsPrincipal"/> from an user asynchronously.
        /// </summary>
        /// <param name="user">The user to create a <see cref="ClaimsPrincipal"/> from.</param>
        /// <param name="authenticationType">The name of the authentication method the <paramref name="user"/> was sourced from.</param>
        /// <returns>The <see cref="Task"/> that represents the asynchronous creation operation, containing the created <see cref="ClaimsPrincipal"/>.</returns>
        public virtual async Task <ClaimsPrincipal> CreateAsync(TUser user)
        {
            if (user == null)
            {
                throw new ArgumentNullException(nameof(user));
            }
            var userId = await UserManager.GetUserIdAsync(user);

            var userName = await UserManager.GetUserNameAsync(user);

            var id = new ClaimsIdentity(Options.Cookies.ApplicationCookieAuthenticationScheme,
                                        Options.ClaimsIdentity.UserNameClaimType,
                                        Options.ClaimsIdentity.RoleClaimType);

            id.AddClaim(new Claim(Options.ClaimsIdentity.UserIdClaimType, userId));
            id.AddClaim(new Claim(Options.ClaimsIdentity.UserNameClaimType, userName));
            if (UserManager.SupportsUserSecurityStamp)
            {
                id.AddClaim(new Claim(Options.ClaimsIdentity.SecurityStampClaimType,
                                      await UserManager.GetSecurityStampAsync(user)));
            }
            if (UserManager.SupportsUserRole)
            {
                var roles = await UserManager.GetRolesAsync(user);

                foreach (var roleName in roles)
                {
                    id.AddClaim(new Claim(Options.ClaimsIdentity.RoleClaimType, roleName));
                    if (RoleManager.SupportsRoleClaims)
                    {
                        var role = await RoleManager.FindByNameAsync(roleName);

                        if (role != null)
                        {
                            id.AddClaims(await RoleManager.GetClaimsAsync(role));
                        }
                    }
                }
            }
            if (UserManager.SupportsUserClaim)
            {
                id.AddClaims(await UserManager.GetClaimsAsync(user));
            }
            return(new ClaimsPrincipal(id));
        }
示例#3
0
 private async Task ValidateUserName(UserManager<TUser> manager, TUser user, ICollection<IdentityError> errors)
 {
     var userName = await manager.GetUserNameAsync(user);
     if (string.IsNullOrWhiteSpace(userName))
     {
         errors.Add(Describer.InvalidUserName(userName));
     }
     else if (manager.Options.User.UserNameValidationRegex != null && 
         !Regex.IsMatch(userName, manager.Options.User.UserNameValidationRegex, RegexOptions.CultureInvariant, manager.Options.User.UserNameValidationRegexTimeout))
     {
         errors.Add(Describer.InvalidUserName(userName));
     }
     else
     {
         var owner = await manager.FindByNameAsync(userName);
         if (owner != null && 
             !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user)))
         {
             errors.Add(Describer.DuplicateUserName(userName));
         }
     }
 }
示例#4
0
        private async Task ValidateUserName(UserManager <TUser> manager, TUser user, ICollection <string> errors)
        {
            var userName = await manager.GetUserNameAsync(user);

            if (string.IsNullOrWhiteSpace(userName))
            {
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "UserName"));
            }
            else if (manager.Options.User.UserNameValidationRegex != null && !Regex.IsMatch(userName, manager.Options.User.UserNameValidationRegex))
            {
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidUserName, userName));
            }
            else
            {
                var owner = await manager.FindByNameAsync(userName);

                if (owner != null &&
                    !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user)))
                {
                    errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateName, userName));
                }
            }
        }
示例#5
0
        private async Task ValidateUserName(UserManager <TUser> manager, TUser user, ICollection <string> errors)
        {
            var userName = await manager.GetUserNameAsync(user);

            if (string.IsNullOrWhiteSpace(userName))
            {
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.PropertyTooShort, "UserName"));
            }
            else if (manager.Options.User.AllowOnlyAlphanumericNames && !userName.All(IsAlphaNumeric))
            {
                // If any characters are not letters or digits, its an illegal user name
                errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.InvalidUserName, userName));
            }
            else
            {
                var owner = await manager.FindByNameAsync(userName);

                if (owner != null &&
                    !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user)))
                {
                    errors.Add(String.Format(CultureInfo.CurrentCulture, Resources.DuplicateName, userName));
                }
            }
        }
示例#6
0
        private async Task ValidateUserName(UserManager <TUser> manager, TUser user, ICollection <IdentityError> errors)
        {
            var userName = await manager.GetUserNameAsync(user);

            if (string.IsNullOrWhiteSpace(userName))
            {
                errors.Add(Describer.InvalidUserName(userName));
            }
            else if (!string.IsNullOrEmpty(manager.Options.User.AllowedUserNameCharacters) &&
                     userName.Any(c => !manager.Options.User.AllowedUserNameCharacters.Contains(c)))
            {
                errors.Add(Describer.InvalidUserName(userName));
            }
            else
            {
                var owner = await manager.FindByNameAsync(userName);

                if (owner != null &&
                    !string.Equals(await manager.GetUserIdAsync(owner), await manager.GetUserIdAsync(user)))
                {
                    errors.Add(Describer.DuplicateUserName(userName));
                }
            }
        }