public IActionResult ChangePwd(int id, string pwd, string newpwd, bool isAdmin = false) { if (isAdmin) { UserTable _ut = _repository.Get(p => p.Id == id); _ut.LoginPwd = Pwd.Ecoding(newpwd); if (_repository.Update(_ut)) { return(Json(new { state = "0", msg = "密码修改成功!" })); } else { return(Json(new { state = "-1", msg = "密码修改失败!" })); } } else { UserTable _ut = _repository.Get(p => p.Id == id && p.LoginPwd == Pwd.Ecoding(pwd)); if (_ut is null) { return(Json(new { state = "-1", msg = "原始密码错误!" })); } else { _ut.LoginPwd = Pwd.Ecoding(newpwd); if (_repository.Update(_ut)) { return(Json(new { state = "0", msg = "密码修改成功!" })); } else { return(Json(new { state = "-1", msg = "密码修改失败!" })); } } } }
public IActionResult Add([FromBody] UserTable _user) { try { if (_user == null) { return(Json(new { state = "-1", msg = "请输入用户!" })); } if (_repository.IsExist(p => p.LoginName == _user.LoginName)) { return(Json(new { state = "-1", msg = "帐号已存在!" })); } if (_repository.IsExist(p => p.ViewName == _user.ViewName)) { return(Json(new { state = "-1", msg = "昵称已存在!" })); } _user.RegTime = DateTime.Now; _user.LoginPwd = Pwd.Ecoding(_user.LoginPwd); int id = _repository.SaveGetId(_user); return(Json(new { id, state = "0", msg = "添加成功!" })); } catch (Exception ex) { return(Json(new { state = "-1", msg = "非法操作!" })); } }
public IActionResult Token([FromBody] AuthorizeTable _auth) { try { if (ModelState.IsValid) { var a = _dbContext.UserTable.FirstOrDefault(u => (u.LoginName == _auth.userName) && (u.LoginPwd == Pwd.Ecoding(_auth.password))); if (a is null) { return(Json(new { state = "-1", msg = "账号不存在或密码错误!" })); } a.LoginCount = (a.LoginCount is null) ? a.LoginCount = 1 : a.LoginCount + 1; a.LastLoginTime = DateTime.Now; //a.LoginCount = a.LoginCount + 1; _dbContext.UserTable.Update(a); _dbContext.SaveChanges(); //var claims=new Claim[]{ // new Claim(ClaimTypes.Name,userModel.CName), // new Claim(ClaimTypes.Role,"user"), // //new Claim("SuperAdminOnly","true") //}; var claims = new Claim[] { new Claim(ClaimTypes.Name, _auth.userName), new Claim(ClaimTypes.Role, "user"), //new Claim("SuperAdminOnly","true") }; var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_jwtSettings.SecretKey)); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( _jwtSettings.Issuer, _jwtSettings.Audience, claims, DateTime.Now, DateTime.Now.AddMinutes(_jwtSettings.TimeOut), creds); TokenUser _tokenUser = new TokenUser { token = new JwtSecurityTokenHandler().WriteToken(token), user = a, state = "0", msg = "操作成功!" }; //return Ok(new {token=new JwtSecurityTokenHandler().WriteToken(token)}); return(Ok(_tokenUser)); } return(Json(new { state = "-1", msg = "非法操作!" })); } catch (Exception ex) { return(Json(new { state = "-1", msg = "非法操作!" })); } }