ASP.NET Core 2.1 MVC,我们可以在 appsetting.json 中对控制器设置限制吗?西风全球化 url
ASP.NET Core 2.1 MVC, Can we set a restriction to a controller in appsetting.json? WestWind Globalization url
我的应用程序中有一个路径用于处理网站上的字符串资源。控制器和操作由第 3 方库管理,因此我不能真正申请那里的授权属性。
我正在使用 WestWind 全球化库,它使 URL 类似于 https://localhost:44328/LocalizationAdmin/index.html
。
我可以像在旧 ASP.NET MVC 中的 web.config 中那样在我的 appsetting.json 中重新连接任何控制器吗?
在 ASP.NET Core?
中类似于以下内容
<location path="LocalizationAdmin">
<system.web>
<authorization>
<deny users="*">
</authorization>
</system.web>
</location>
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");
}
}
});
我的应用程序中有一个路径用于处理网站上的字符串资源。控制器和操作由第 3 方库管理,因此我不能真正申请那里的授权属性。
我正在使用 WestWind 全球化库,它使 URL 类似于 https://localhost:44328/LocalizationAdmin/index.html
。
我可以像在旧 ASP.NET MVC 中的 web.config 中那样在我的 appsetting.json 中重新连接任何控制器吗?
在 ASP.NET Core?
中类似于以下内容<location path="LocalizationAdmin">
<system.web>
<authorization>
<deny users="*">
</authorization>
</system.web>
</location>
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");
}
}
});