public async Task <IActionResult> Login(LoginReq req) { var log = new OperatingLog(); log.Create(""); log.Summary = req.UserName + "尝试登录"; var user = await _userBll.LoginAsync(req.UserName, req.Password); if (user == null) { await _logBll.AddAsync(log); return(new JsonResult(new { success = false, message = "用户不存在" })); } else { log.Create(user.PrimaryKey); log.Model = Json.Serialize(user); log.LinkId = user.PrimaryKey; await _logBll.AddAsync(log); } var claims = new[] { new Claim("name", user.UserName), new Claim("id", user.PrimaryKey), new Claim("role", "admin"), }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_config.Value.SecretKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken(_config.Value.Issuer, _config.Value.Audience, claims, DateTime.Now, DateTime.Now.AddMinutes(_config.Value.Expired), creds); var result = new JwtSecurityTokenHandler().WriteToken(token); return(new JsonResult(new { success = true, token = result })); }
public async Task AddAsync(string createId, string summary, string model, string linkId) { var log = new OperatingLog(); log.Create(createId); log.LinkId = linkId; log.Summary = summary; log.Model = model; await AddAsync(log); }