Beispiel #1
0
        public async Task <ResponseDto> Login(LoginDto user)
        {
            var result = new ResponseDto();

            if (user.LoginName == "admin" && user.LoginPwd == "123")
            {
                _user = new EmployeeDto()
                {
                    Name  = "admin",
                    Email = "*****@*****.**"
                };
            }
            else
            {
                _user = _mapper.Map <Employee, EmployeeDto>(await bll.Login(user));
            }
            if (_user != null)
            {
                var claim = new Claim[] { //声明
                    new Claim(ClaimTypes.UserData, JsonSerializer.Serialize(_user))
                };
                //对称秘钥
                var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey));
                //签名证书(秘钥,加密算法)
                var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);
                //生成token  [注意]需要nuget添加Microsoft.AspNetCore.Authentication.JwtBearer包,并引用System.IdentityModel.Tokens.Jwt命名空间
                var token = new JwtSecurityToken(_jwtSettings.Issuer, _jwtSettings.Audience, claim, DateTime.Now, DateTime.Now.AddMinutes(30), creds);
                result.data = new { token = new JwtSecurityTokenHandler().WriteToken(token) };
            }
            return(result);
        }
 public bool Login(int empno, string sal)
 {
     return(es.Login(empno, sal));
 }