var queryList = _usersss.GetAll().Where(s => s.User == user).FirstOrDefault(); if (queryList == null) { throw new UserFriendlyException("数据异常", "用户未注册"); } else { if (queryList.Password != passWord) { throw new UserFriendlyException("数据异常", "密码错误"); } else { var cleims = new[] { new Claim(ClaimTypes.Name, queryList.User), new Claim(ClaimTypes.NameIdentifier, queryList.Id.ToString()), }; var key = new SymmetricSecurityKey(Encoding.ASCII.GetBytes("netcore")); var creds = new SigningCredentials(key, SecurityAlgorithms.HmacSha256); var token = new JwtSecurityToken( issuer: "cc", audience: "cc", claims: cleims, expires: DateTime.Now.AddYears(1), signingCredentials: creds); string jwtToken = new JwtSecurityTokenHandler().WriteToken(token); } }