//获得token public string GetToken(Blog_Users userInfo) { _configuration["ValidAudience"] = userInfo.UserName + userInfo.PassWord + DateTime.Now.ToString(); //把用户的名字推到一个声明中 var claims = new[] { new Claim(ClaimTypes.Name, userInfo.UserName) //自定义参数 //new Claim(ClaimTypes.NameIdentifier,userInfo.UserName) }; //使用密钥对令牌进行签名。此密钥将在您的API和任何需要检查令牌是否合法的对象之间共享 var key = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(_configuration["SecurityKey"]));//获取密钥 //第一个参数是根据预先的二进制字节数组生成一个安全秘钥,就是密码, //第二个参数是编码方式 var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256);//凭证 ,根据密钥生成 var token = new JwtSecurityToken( //颁发者 issuer: "igbom_web", //接收者 audience: _configuration["ValidAudience"], //自定义参数 claims: claims, //过期时间 expires: DateTime.Now.AddMinutes(10), //token的有效期 这里设置100分钟token就失效 //签名证书 signingCredentials: creds ); return(new JwtSecurityTokenHandler().WriteToken(token)); }
public async Task <ActionResult <ExcutedResult> > Register([FromBody] Blog_Users user) { try { //请求状态 int statecode = (int)ExcutedResult.status.成功; user.CreationTime = DateTimeHelper.GettimeStamp(); //执行添加 var result = await _userService.Register(user); if (result == null) { statecode = (int)ExcutedResult.status.添加数据失败; return(ExcutedResult.SuccessResult("添加数据失败", statecode)); } return(ExcutedResult.SuccessResult(result, statecode)); } catch (Exception ex) { return(ExcutedResult.FailedResult(ex.Message, (int)ExcutedResult.status.请求失败)); } }
/// <summary> /// 修改用户Token /// </summary> /// <param name="user"></param> /// <returns></returns> public async Task <int> UpdateUserToken(Blog_Users user) { var service = this.CreateService <Blog_Users>(); return(await service.Update(user)); }
/// <summary> /// 注册用户 /// </summary> /// <param name="user">用户实体</param> /// <returns></returns> public async Task <Blog_Users> Register(Blog_Users user) { var service = this.CreateService <Blog_Users>(); return(await service.Add(user)); }