Authorization其目標就是驗證Http請求可否經過驗證。ASP.Net Core提供了不少種Authorization方式,詳細能夠參考 微軟官方文檔。在這裏只詳細介紹三種方式:ide
Policyui
Middlewarespa
Custom Attribute3d
#region jwt驗證 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"])) }; }); #endregion
#region jwt驗證 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"])) }; }); #endregion #region 受權 services.AddAuthorization(options => { options.AddPolicy("EmployeeOnly", policy => { policy.RequireClaim("EmployeeNumber"); }); }); //如上,咱們定義了一個名稱爲EmployeeOnly的受權策略,它要求用戶的Claims中必須包含類型爲EmployeeNumber的Claim。 #endregion
自定義相似jwt的token驗證,也就是說直接從header中拿取咱們想要的tokencode
#region jwt驗證 services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) .AddJwtBearer(options => { /*options.TokenValidationParameters = new TokenValidationParameters { ValidIssuer = Configuration["Jwt:Issuer"], ValidAudience = Configuration["Jwt:Audience"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(Configuration["Jwt:SecretKey"])) };*/ #region 自定義Jwt的token驗證 options.SecurityTokenValidators.Clear();//將SecurityTokenValidators清除掉,不然它會在裏面拿驗證 options.SecurityTokenValidators.Add(new MyTokenValidator()); //自定義的MyTokenValidator驗證方法 options.Events = new JwtBearerEvents { //重寫OnMessageReceived OnMessageReceived = context => { var token = context.Request.Headers["mytoken"]; context.Token = token.FirstOrDefault(); return Task.CompletedTask; } }; #endregion }); #endregion
using Microsoft.AspNetCore.Authentication.JwtBearer; using Microsoft.IdentityModel.Tokens; using System; using System.Collections.Generic; using System.Linq; using System.Security.Claims; using System.Threading.Tasks; namespace ZanLveCore { public class MyTokenValidator : ISecurityTokenValidator { bool ISecurityTokenValidator.CanValidateToken => true; int ISecurityTokenValidator.MaximumTokenSizeInBytes { get; set; } bool ISecurityTokenValidator.CanReadToken(string securityToken) { return true; } //驗證token ClaimsPrincipal ISecurityTokenValidator.ValidateToken(string securityToken, TokenValidationParameters validationParameters, out SecurityToken validatedToken) { validatedToken = null; //判斷token是否正確 if (securityToken != "abcdefg") return null; //給Identity賦值 var identity = new ClaimsIdentity(JwtBearerDefaults.AuthenticationScheme); identity.AddClaim(new Claim("name", "wyt")); identity.AddClaim(new Claim(ClaimsIdentity.DefaultRoleClaimType, "admin")); var principle = new ClaimsPrincipal(identity); return principle; } } }
其實這種驗證方式是 簡稱 清除驗證規則,自定義驗證方式jwt
options.SecurityTokenValidators.Clear();//將SecurityTokenValidators清除掉,不然它會在裏面拿驗證 options.SecurityTokenValidators.Add(new MyTokenValidator()); //自定義的MyTokenValidator驗證方法
還有一種方式是 簡稱 重寫驗證方式
權限受權Handler的繼承AuthorizationHandler的自定義受權類