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 <IActionResult> AddUser(User user) { user.Create(Request.RequestUser().UserId); user.Status = UserStatus.Normal; await _userBll.AddAsync(user); //日志 await _logBll.AddAsync( Request.RequestUser().UserId, Request.RequestUser().UserName + "添加了用户:" + user.UserName, Json.Serialize(user), user.PrimaryKey ); return(new JsonResult(new { success = true })); }
public async Task <IActionResult> AddProject(Project project) { project.Create(Request.RequestUser().UserId); await _projectBll.AddAsync(project); //日志 await _logBll.AddAsync( Request.RequestUser().UserId, Request.RequestUser().UserName + "创建了项目:" + project.Name, Json.Serialize(project), project.PrimaryKey ); return(new JsonResult(new { success = true })); }