Asp.net core mvc ASP.NET Core 2.1 MVC,我们可以在appsetting.json中对控制器设置限制吗?西风环球网址

Asp.net core mvc ASP.NET Core 2.1 MVC,我们可以在appsetting.json中对控制器设置限制吗?西风环球网址,asp.net-core-mvc,authorization,asp.net-identity,westwind-globalization,Asp.net Core Mvc,Authorization,Asp.net Identity,Westwind Globalization,我在应用程序中有一个路径,用于处理站点上的字符串资源。控制器和操作由第三方库管理,因此我无法在那里真正应用授权属性 我正在使用WestWind全球化库,它生成了一个类似https://localhost:44328/LocalizationAdmin/index.html 我是否可以像在旧ASP.NET MVC的web.config中一样重新设置appsetting.json中的任何控制器 在ASP.NET核心中类似于下面的内容 <location path="LocalizationAd

我在应用程序中有一个路径,用于处理站点上的字符串资源。控制器和操作由第三方库管理,因此我无法在那里真正应用授权属性

我正在使用WestWind全球化库,它生成了一个类似
https://localhost:44328/LocalizationAdmin/index.html

我是否可以像在旧ASP.NET MVC的web.config中一样重新设置appsetting.json中的任何控制器

在ASP.NET核心中类似于下面的内容

<location path="LocalizationAdmin">
  <system.web>
    <authorization>
      <deny users="*">
    </authorization>
  </system.web>
</location>


Web.config
IIS
使用。但是
ASP.NET核心
可以在没有
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");
        }
    }
});
app.Use(异步(ctx,下一步)=>{
//通过所有其他请求
如果(!ctx.Request.Path.StartsWithSegments(“/LocalizationAdmin”)){
等待下一个();
}
否则{
var user=ctx.user;//现在我们有了当前用户
var resource=new{/*…*/};//根据需要构造描述
var authZService=ctx.RequestServices.GetRequiredService();
var accessible=await authZService.AuthorizeAsync(用户、资源,“MyPolicyName”);
if(可访问。成功){
等待下一个();
}否则{
ctx.Response.StatusCode=403;
等待ctx.Response.WriteAsync(“不允许”);
}
}
});

听起来是个好主意。让我试试。谢谢,谢谢你提出这个想法。我没有使用它,但使用了下面的链接,它非常适合wwwroot中的静态文件。