标签:Core 自定义 app 中间件 httpContext NET public CustomAuthorizeAttribute
您有一个Web API,并且想要实现自己的授权逻辑,该怎么做?您需要做四件事。
1. 创建您的自定义授权属性
2. 在控制器上使用自定义授权属性
3. 在自定义请求管道中间件中创建授权逻辑
4. 启动时注册中间件
创建您的自定义授权属性
1 [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, AllowMultiple = false)] 2 public class CustomAuthorizeAttribute : Attribute 3 { 4 public string[] AllowedUserRoles { get; private set; } 5 6 public CustomAuthorizeAttribute(params string[] allowedUserRoles) 7 { 8 this.AllowedUserRoles = allowedUserRoles; 9 } 10 }
在控制器上使用自定义授权属性
1 [ApiController] 2 [Route("[controller]")] 3 public class WeatherForecastController : ControllerBase 4 { 5 6 [HttpGet] 7 [CustomAuthorize("Admin", "Supervisor", "Worker")] 8 public string Get() 9 { 10 return "Sunny"; 11 } 12 }
在自定义请求管道中间件中创建授权逻辑
1 public static class CustomAuthorizationMiddleware 2 { 3 public static async Task Authorize(HttpContext httpContext, Func next) 4 { 5 var endpointMetaData = httpContext.GetEndpoint().Metadata; 6 7 bool hasCustomAuthorizeAttribute = endpointMetaData.Any(x => x is CustomAuthorizeAttribute); 8 9 if (!hasCustomAuthorizeAttribute) 10 { 11 await next.Invoke(); 12 return; 13 } 14 15 CustomAuthorizeAttribute customAuthorizeAttribute = endpointMetaData 16 .FirstOrDefault(x => x is CustomAuthorizeAttribute) as CustomAuthorizeAttribute; 17 18 // TODO: change authorization logic 19 bool isAuthorized = customAuthorizeAttribute.AllowedUserRoles 20 .Any(allowedRole => httpContext.User.IsInRole(allowedRole)); 21 22 if (isAuthorized) 23 { 24 await next.Invoke(); 25 return; 26 } 27 28 httpContext.Response.StatusCode = (int)HttpStatusCode.Unauthorized; 29 await httpContext.Response.WriteAsync("unauthorized"); 30 } 31 }
启动时注册中间件
1 public class Startup 2 { 3 public Startup(IConfiguration configuration) 4 { 5 Configuration = configuration; 6 } 7 8 public IConfiguration Configuration { get; } 9 10 // This method gets called by the runtime. Use this method to add services to the container. 11 public void ConfigureServices(IServiceCollection services) 12 { 13 services.AddControllers(); 14 } 15 16 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. 17 public void Configure(IApplicationBuilder app, IWebHostEnvironment env) 18 { 19 if (env.IsDevelopment()) 20 { 21 app.UseDeveloperExceptionPage(); 22 } 23 24 app.UseHttpsRedirection(); 25 26 app.UseRouting(); 27 28 app.Use(CustomAuthorizationMiddleware.Authorize); 29 30 app.UseEndpoints(endpoints => 31 { 32 endpoints.MapControllers(); 33 }); 34 } 35 }
确保在调用app.UseRouting()之后添加中间件。这样可以确保在将路由 信息添加到HttpContext 后执行您的中间件。
标签:Core,自定义,app,中间件,httpContext,NET,public,CustomAuthorizeAttribute 来源: https://www.cnblogs.com/bisslot/p/12330985.html
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。