欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 文旅 > 八卦 > ASP.NET Core对JWT的封装

ASP.NET Core对JWT的封装

2025/2/8 12:07:56 来源:https://blog.csdn.net/Anoxia_li/article/details/145477329  浏览:    关键词:ASP.NET Core对JWT的封装

目录

JWT封装

[Authorize]的注意事项


JWT封装

NuGet 库 |Microsoft.AspNetCore.Authentication.JwtBearer 9.0.1https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.JwtBearer

  1. 配置JWT节点,节点下创建SigningKeyExpireSeconds两个配置项,分别代表JWT的密钥和过期时间(单位:秒)。再创建配置类JWTOptions,包含SigningKey、ExpireSeconds两个属性。
  2. Nuget:Microsoft.AspNetCore.Authentication.JwtBearer
  3. 对JWT进行配置
    using Microsoft.AspNetCore.Authentication.JwtBearer;
    using Microsoft.IdentityModel.Tokens;
    builder.Services.Configure<JWTSettings>(builder.Configuration.GetSection("JWT"));
    builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme).AddJwtBearer(opt =>
    {var jwtOpt=builder.Configuration.GetSection("JWT").Get<JWTSettings>();byte[] key = Encoding.UTF8.GetBytes(jwtOpt.SecKey);//设置对称秘钥var secKey = new SymmetricSecurityKey(key);//设置验证参数opt.TokenValidationParameters = new (){ValidateIssuer = false,//是否验证颁发者ValidateAudience = false,//是否验证订阅者ValidateLifetime = true,//是否验证生命周期ValidateIssuerSigningKey = true,//是否验证签名IssuerSigningKey = secKey//签名秘钥};
    });
  4. Program.cs的app.UseAuthorization()这行代码之前添加app.UseAuthentication()
  5. Controller类中进行登录
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class DemoContrller : ControllerBase
    {private readonly IOptionsSnapshot<JWTSettings> jwtSettingsOpt;public DemoContrller(IOptionsSnapshot<JWTSettings> jwtSettingsOpt){this.jwtSettingsOpt = jwtSettingsOpt;}[HttpPost]public ActionResult<string> Login(string username, string password){if (username == "admin" && password == "123"){List<Claim> claims = new List<Claim>{new Claim(ClaimTypes.NameIdentifier, "1"),new Claim(ClaimTypes.Name, username)};string key = jwtSettingsOpt.Value.SecKey;DateTime expire = DateTime.Now.AddSeconds(jwtSettingsOpt.Value.ExpireSeconds);byte[] keyBytes = Encoding.UTF8.GetBytes(key);var secKey = new SymmetricSecurityKey(keyBytes);var credentials = new SigningCredentials(secKey, SecurityAlgorithms.HmacSha256Signature);var tokenDescriptor = new JwtSecurityToken(claims: claims,//声明expires: expire,//过期时间signingCredentials: credentials//签名凭据);string jwt = new JwtSecurityTokenHandler().WriteToken(tokenDescriptor);return jwt;}else{return BadRequest("登录失败");}}
    }
  6. 在需要登录才能访问的控制器类或者Action方法上添加[Authorize]。
  7. 可以使用this.User.FindFirst(ClaimTypes.Name).Value来访问登录用户的身份信息。
  8. 登录和访问。用PostMan自定义报文头:Authorization的值为“Bearer JWTToken”, Authorization的值中的“Bearer”和JWT令牌之间一定要通过空格分隔。前后不能多出来额外的空格、换行等。

[Authorize]的注意事项

  1. ASP.NET Core中身份验证和授权验证的功能由Authentication、Authorization中间件提供:app.UseAuthentication()、app.UseAuthorization()。
  2. 控制器类上标注[Authorize],则所有操作方法都会被进行身份验证和授权验证;对于标注了[Authorize]的控制器中,如果其中某个操作方法不想被验证,可以在操作方法上添加[AllowAnonymous]。如果没有在控制器类上标注[Authorize],那么这个控制器中的所有操作方法都允许被自由地访问;对于没有标注[Authorize]的控制器中,如果其中某个操作方法需要被验证,我们也可以在操作方法上添加[Authorize]。
  3. ASP.NET Core会按照HTTP协议的规范,从Authorization取出来令牌,并且进行校验、解析,然后把解析结果填充到User属性中,这一切都是ASP.NET Core完成的,不需要开发人员自己编写代码。但是一旦出现401,没有详细的报错信息,很难排查,这是初学者遇到的难题。
  4. RBAC角色控制:可以设置[Authorize(Roles ="admin,user")]来限制角色登录

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com