コード例 #1
0
        public async Task <IdentityResult> CreateUserAsync(UserDto dto, string password = null)
        {
            var emailExisted = await EmailExistedAsync(dto.Email);

            var nameExisted = await UsernameExistedAsync(dto.UserName);

            if (emailExisted)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Code = "EmailExisted", Description = $"邮箱{dto.Email}已经存在!"
                }));
            }
            if (nameExisted)
            {
                return(IdentityResult.Failed(new IdentityError {
                    Code = "UsernameExisted", Description = $"用户名{dto.UserName}已经存在!"
                }));
            }

            var user = new MoMoBotUser
            {
                Avatar   = await _appSettings.GetDefaultUserAvatarAsync(),
                Nickname = dto.Nickname,
                Email    = dto.Email,
                UserName = dto.UserName
            };

            if (string.IsNullOrEmpty(password))
            {
                password = await _appSettings.GetDefaultUserPasswordAsync();
            }
            return(await _userManager.CreateAsync(user, password));
        }
コード例 #2
0
        private async Task <IList <Claim> > GetUserClaimsAsync(MoMoBotUser user)
        {
            var claims = new List <Claim>
            {
                new Claim(JwtClaimTypes.Audience, "momobot.api"),
                new Claim(JwtClaimTypes.Issuer, "yunstorm"),
                new Claim(JwtClaimTypes.Id, user.Id.ToString()),
                new Claim(JwtClaimTypes.Name, user.UserName),
                new Claim(JwtClaimTypes.Email, user.Email),
                new Claim(JwtClaimTypes.NickName, user.Nickname),
                new Claim("avatar", user.Avatar)
            };

            var roles = await _userManager.GetRolesAsync(user);

            if (roles != null)
            {
                claims.Add(new Claim(JwtClaimTypes.Role, string.Join(',', roles)));
            }

            return(claims);
        }