既然选择了远方,便只顾风雨兼程 __ HANS许html
引言:挺久没更新了,以前作了Vue的系列,后面想作作服务端的系列,上下衔接,咱们就讲讲WebApi(网络应用程序接口),接口免不了用户认证,因此接下来咱们的主题系列文章即是“基于ASP.NET Core的用户认证”,分为市面上流行的JWT(JSON WebToken)与OAuth2(开放受权)web
JWT(JSON Web Token)
简单的介绍下JWT,更多的话,能够这边文章看看。咱们着重讲下实现。
ASP.NET Core 的Middleware实现
-
建立JWT
首先咱们要先建立token,毕竟这个是最重要的。Core自带JWT帮助类,因此咱们按照帮助类的意思来写个方法建立token。
- public string CreateJsonWebToken(Dictionary<string, string> payLoad)
- {
- if (string.IsNullOrWhiteSpace(setting.SecurityKey))
- {
- throw new ArgumentNullException("JsonWebTokenSetting.securityKey",
- "securityKey为NULL或空字符串。请在\"appsettings.json\"配置\"JsonWebToken\"节点及其子节点\"securityKey\"");
- }
- var now = DateTime.UtcNow;
-
-
-
- var claims = new List<Claim>();
- foreach (var key in payLoad.Keys)
- {
- var tempClaim = new Claim(key, payLoad[key]?.ToString());
- claims.Add(tempClaim);
- }
-
-
- var jwt = new JwtSecurityToken(
- issuer: null,
- audience: null,
- claims: claims,
- notBefore: now,
- expires: now.Add(TimeSpan.FromMinutes(setting.ExpiresMinute)),
- signingCredentials: new SigningCredentials(new SymmetricSecurityKey(Encoding.ASCII.GetBytes(setting.SecurityKey)), SecurityAlgorithms.HmacSha256));
- var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);
- return encodedJwt;
- }
从方法咱们看到,咱们传入的是负载这个片断,而失效时间与秘钥咱们是放在了appsettings.json
来进行配置的。使用DI来获取配置文件中的节点值。
-
编写中间件
咱们都知道,中间件是Core的管道模型组成部分,因此咱们在中间件作验证,来判断每次请求用户是有有权限是有该WebApi或者其余API。
- 中间件
- public JwtCustomerAuthorizeMiddleware(RequestDelegate next, IOptions<JsonWebTokenSetting> options, IJsonWebTokenValidate jsonWebTokenValidate, Func<Dictionary<string, string>, JsonWebTokenSetting, bool> validatePayLoad, List<string> anonymousPathList)
- {
- this._next = next;
- this._setting = options.Value;
- this._jsonWebTokenValidate = jsonWebTokenValidate;
- this._validatePayLoad = validatePayLoad;
- this._anonymousPathList = anonymousPathList;
- }
-
- public async Task Invoke(HttpContext context)
- {
-
-
- if (_anonymousPathList.Contains(context.Request.Path.Value))
- {
-
- await _next(context);
- return;
- }
- var result = context.Request.Headers.TryGetValue("Authorization", out StringValues authStr);
- if (!result || string.IsNullOrEmpty(authStr.ToString()))
- {
- throw new UnauthorizedAccessException("未受权,请传递Header头的Authorization参数。");
- }
-
-
- result = _jsonWebTokenValidate.Validate(authStr.ToString().Substring("Bearer ".Length).Trim()
- , _setting, _validatePayLoad);
- if (!result)
- {
- throw new UnauthorizedAccessException("验证失败,请查看传递的参数是否正确或是否有权限访问该地址。");
- }
-
- await _next(context);
- }
从代码来看,anonymousPathList
是URL路径,如果在这个List
内的URL,即可直接跳过验证,
接着将authStr
token代入验证函数,validatePayLoad
倒是咱们自代入的委托函数,用于服务器自定义验证。
- 验证
验证方法,我只是作了签名验证与时间验证。并无定得死死的,让用户自由度的去进行验证。
- public bool Validate(string encodeJwt, JsonWebTokenSetting setting, Func<Dictionary<string, string>, JsonWebTokenSetting, bool> validatePayLoad)
- {
- if (string.IsNullOrWhiteSpace(setting.SecurityKey))
- {
- throw new ArgumentNullException("JsonWebTokenSetting.securityKey",
- "securityKey为NULL或空字符串。请在\"appsettings.json\"配置\"JsonWebToken\"节点及其子节点\"securityKey\"");
- }
-
- var success = true;
- var jwtArr = encodeJwt.Split('.');
- var header = JsonConvert.DeserializeObject<Dictionary<string, string>>(Base64UrlEncoder.Decode(jwtArr[0]));
- var payLoad = JsonConvert.DeserializeObject<Dictionary<string, string>>(Base64UrlEncoder.Decode(jwtArr[1]));
-
- var hs256 = new HMACSHA256(Encoding.ASCII.GetBytes(setting.SecurityKey));
-
- success = success && string.Equals(jwtArr[2], Base64UrlEncoder.Encode(hs256.ComputeHash(Encoding.UTF8.GetBytes(string.Concat(jwtArr[0], ".", jwtArr[1])))));
- if (!success)
- {
- return success;
- }
-
- var now = ToUnixEpochDate(DateTime.UtcNow);
- success = success && (now >= long.Parse(payLoad["nbf"].ToString()) && now < long.Parse(payLoad["exp"].ToString()));
-
-
- success = success && validatePayLoad(payLoad, setting);
-
- return success;
- }
-
加载中间件
- 使用扩展方法,来封装中间件
- public static IApplicationBuilder UseJwtCustomerAuthorize(this IApplicationBuilder app, Action<IJwtCustomerAuthorezeOption> action)
- {
- var _JwtCustomerAuthorezeOption = app.ApplicationServices.GetService<IJwtCustomerAuthorezeOption>() as JwtCustomerAuthorezeOption;
- action(_JwtCustomerAuthorezeOption);
- return app.UseMiddleware<JwtCustomerAuthorizeMiddleware>(_JwtCustomerAuthorezeOption.validatePayLoad, _JwtCustomerAuthorezeOption.anonymousPath);
- }
- 在
Startup.cs
使用
- 注册服务
- public void ConfigureServices(IServiceCollection services) {
- services.AddJwt(Configuration);}
- 使用中间件
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- app.UseJwtCustomerAuthorize(option =>
- {
-
- option.SetAnonymousPaths(new System.Collections.Generic.List<string>()
- {
-
- "/Home/Privacy",
- "/Home/CreateJsonToken"
- });
-
- option.SetValidateFunc((playLoad, sertting) =>
- {
- return true;
- });
- });
- }
总结下,经过上面,就完成了JWT在ASP.NET Core使用中间件的方式的实现。简单来讲就是用自带方法建立token,验证则使用中间件的形式,每次请求都须要进行验证固然你能够设置特殊URL。在下篇文章咱们来说讲使用策略模式的JWT实现。