我的应用程序中有一个处理站点上字符串资源的路径。控制器和操作是由第三方库管理的,因此我不能真正应用授权属性。
我正在使用WestWind全球化库,它生成一个类似于https://localhost:44328/LocalizationAdmin/index.html的URL。
我是否可以像我们在旧的appsetting.json MVC中的web.config中一样,重新字符串web.config中的任何控制器?
类似于ASP.NET核心中下面的内容?
<location path="LocalizationAdmin">
<system.web>
<authorization>
<deny users="*">
</authorization>
</system.web>
</location>发布于 2018-12-03 01:42:23
Web.config由IIS使用。但是ASP.NET Core可以在没有IIS的情况下部署。在与Nginx合作时,没有在appsettings.json中配置授权的方法。
一个更简单的方法是设置一个简单的中间件:
app.Use(async(ctx , next)=>{
// passby all other requests
if(!ctx.Request.Path.StartsWithSegments("/LocalizationAdmin")){
await next();
}
else {
var user = ctx.User; // now we have the current user
var resource = new { /* ... */ }; // construct description as you like
var authZService = ctx.RequestServices.GetRequiredService<IAuthorizationService>();
var accessible =await authZService.AuthorizeAsync(user, resource,"MyPolicyName");
if(accessible.Succeeded){
await next();
}else{
ctx.Response.StatusCode = 403;
await ctx.Response.WriteAsync("not allowed");
}
}
});https://stackoverflow.com/questions/53583865
复制相似问题