@@ -47,17 +47,12 @@ public PermissionHandler(IAuthenticationSchemeProvider schemes, IRoleModulePermi
4747 protected override async Task HandleRequirementAsync ( AuthorizationHandlerContext context , PermissionRequirement requirement )
4848 {
4949 /*
50- * .netcore3.0 启用EndpointRouting后,权限filter不再添加到ActionDescriptor ,而将权限直接作为中间件运行,
51- * 同时所有filter都会添加到endpoint.Metadata。因此,文中的
52- * context.Resource as Microsoft.AspNetCore.Mvc.Filters.AuthorizationFilterContext不再成立。
53- *
54- * 解决方案有两个:
5550 *
5651 * 首先必须在 controller 上进行配置 Authorize ,可以策略授权,也可以角色等基本授权
5752 *
5853 * 1、开启公约, startup 中的全局授权过滤公约:o.Conventions.Insert(0, new GlobalRouteAuthorizeConvention());
5954 *
60- * 2、不开启公约,使用 IHttpContextAccessor ,也能实现效果,但是不能自定义返回格式,详细看下边配置 ;
55+ * 2、不开启公约,使用 IHttpContextAccessor ,也能实现效果;
6156 */
6257
6358 // 将最新的角色和接口列表更新
@@ -93,14 +88,7 @@ orderby item.Id
9388 {
9489 if ( await handlers . GetHandlerAsync ( httpContext , scheme . Name ) is IAuthenticationRequestHandler handler && await handler . HandleRequestAsync ( ) )
9590 {
96- //context.Fail();
97- //return;
98-
99- //自定义返回数据
100- var payload = JsonConvert . SerializeObject ( new { Code = "401" , Message = "很抱歉,您无权访问该接口,请确保已经登录!" } ) ;
101- httpContext . Response . StatusCode = StatusCodes . Status401Unauthorized ;
102- httpContext . Response . ContentType = "application/json" ;
103- await httpContext . Response . WriteAsync ( payload ) ;
91+ context . Fail ( ) ;
10492 return ;
10593 }
10694 }
@@ -146,17 +134,8 @@ orderby item.Id
146134 //if (currentUserRoles.Count <= 0 || requirement.Permissions.Where(w => currentUserRoles.Contains(w.Role) && w.Url.ToLower() == questUrl).Count() <= 0)
147135 if ( currentUserRoles . Count <= 0 || ! isMatchRole )
148136 {
149- // 可以在这里设置跳转页面
150- //context.Fail();
151- //return;
152-
153- var payload = JsonConvert . SerializeObject ( new { Code = "403" , Message = "很抱歉,您的访问权限等级不够,联系管理员!" } ) ;
154- httpContext . Response . StatusCode = StatusCodes . Status403Forbidden ;
155- httpContext . Response . ContentType = "application/json" ;
156- await httpContext . Response . WriteAsync ( payload ) ;
137+ context . Fail ( ) ;
157138 return ;
158-
159-
160139 }
161140 }
162141
@@ -167,14 +146,7 @@ orderby item.Id
167146 }
168147 else
169148 {
170- //context.Fail();
171- //return;
172-
173- //自定义返回数据
174- var payload = JsonConvert . SerializeObject ( new { Code = "401" , Message = "很抱歉,您无权访问该接口,请确保已经登录!" } ) ;
175- httpContext . Response . StatusCode = StatusCodes . Status401Unauthorized ;
176- httpContext . Response . ContentType = "application/json" ;
177- await httpContext . Response . WriteAsync ( payload ) ;
149+ context . Fail ( ) ;
178150 return ;
179151 }
180152 return ;
@@ -183,14 +155,7 @@ orderby item.Id
183155 //判断没有登录时,是否访问登录的url,并且是Post请求,并且是form表单提交类型,否则为失败
184156 if ( ! questUrl . Equals ( requirement . LoginPath . ToLower ( ) , StringComparison . Ordinal ) && ( ! httpContext . Request . Method . Equals ( "POST" ) || ! httpContext . Request . HasFormContentType ) )
185157 {
186- //context.Fail();
187- //return;
188-
189- //自定义返回数据
190- var payload = JsonConvert . SerializeObject ( new { Code = "401" , Message = "很抱歉,您无权访问该接口,请确保已经登录!" } ) ;
191- httpContext . Response . StatusCode = StatusCodes . Status401Unauthorized ;
192- httpContext . Response . ContentType = "application/json" ;
193- await httpContext . Response . WriteAsync ( payload ) ;
158+ context . Fail ( ) ;
194159 return ;
195160 }
196161 }
0 commit comments