Exemplo n.º 1
0
        public string Authenticated(LoginRequestDto request)
        {
            //rsa解密
            var code = SecurityRsa.Decrypt(request.Password);

            if (code is null)
            {
                return(null);
            }
            //获取用户
            var user = _context.User.Find(request.UserName);

            if (user is null || user.Password != SecurityAes.Encrypt(code))
            {
                return(null);
            }
            //获取用户角色
            var roles = user.Role.Split("::", StringSplitOptions.RemoveEmptyEntries).ToList();

            //创建claim
            var claims = new List <Claim>()
            {
                new Claim(ClaimTypes.Name, user.UserName)
            };

            claims.AddRange(roles.Select(role => new Claim(ClaimTypes.Role, role)));
            //生成token
            var key         = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_tokenManagement.Secret));
            var credentials = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
            var jwtToken    = new JwtSecurityToken(_tokenManagement.Issuer, _tokenManagement.Audience, claims,
                                                   expires: DateTime.Now.AddMinutes(_tokenManagement.AccessExpiration), signingCredentials: credentials);
            var token = new JwtSecurityTokenHandler().WriteToken(jwtToken);

            return(token);
        }
Exemplo n.º 2
0
        public UserModel AddUserData(RegistRequestDto request)
        {
            //rsa解密
            var code = SecurityRsa.Decrypt(request.Password);

            if (code is null)
            {
                return(null);
            }
            //查找用户
            if (_context.User.Find(request.UserName) != null)
            {
                return(null);
            }
            var newUser = new UserModel
            {
                UserName = request.UserName,
                Email    = request.Email,
                Password = SecurityAes.Encrypt(code),
                Role     = request.Role.ToLower()
            };

            _context.User.Add(newUser);
            _context.SaveChanges();
            return(newUser);
        }
Exemplo n.º 3
0
        public UserModel ModifyUserData(ModifyRequestDto requestDto, bool isAdmin)
        {
            //rsa解密
            var oldCode = SecurityRsa.Decrypt(requestDto.OldPassword);
            var code    = SecurityRsa.Decrypt(requestDto.Password);

            if (oldCode is null || code is null)
            {
                return(null);
            }
            //查找用户
            var user = _context.User.Find(requestDto.UserName);

            if (user is null || (user.Password != SecurityAes.Encrypt(oldCode) && !isAdmin))
            {
                return(null);
            }
            user.Email    = requestDto.Email;
            user.Password = SecurityAes.Encrypt(code);
            user.Role     = requestDto.Role.ToLower();
            _context.User.Update(user);
            _context.SaveChanges();
            return(user);
        }