【问题标题】:ASP.NET MVC 6 folder authorizationASP.NET MVC 6 文件夹授权
【发布时间】:2015-04-10 16:09:28
【问题描述】:

我正在 ASP.NET MVC 6 中准备应用程序。该应用程序有一个文件夹,其中包含一些用于管理目的的静态文件。我想限制具有特定角色的用户访问此内容。

在 MVC 6 之前,可以创建一个 web.config 文件并将其放在这个受限文件夹中(例如:asp.net folder authorization)。

vNext 有类似的方法吗?

【问题讨论】:

  • Before MVC 6 there was a possibility to create a web.config file and place it in this restricted folder 你试过了吗?这应该仍然有效(您可能需要启用“通过 ASP.NET 路由静态文件”)...
  • 在发布 Stack Overflow 问题之前总是更容易尝试。避免尴尬。
  • vNext 中的 web.config 不是已经过时了吗? “添加新项目”向导中没有这样的模板。
  • @azachert 你会看到很多人说你不需要 web.config 但如果你使用的是 IIS(大多数人都是),那么你仍然需要 web.config 它来配置 IIS。那些人在散布半真半假的真相。

标签: c# asp.net asp.net-mvc asp.net-core


【解决方案1】:

您可以关注Scott Allen's 博客文章,该文章展示了如何使用一些中间件来做到这一点:

// First, in the Startup class for the application, we will add the required services. 
public void ConfigureServices(IServiceCollection services)
{
    services.AddAuthentication();
    services.AddAuthorization(options =>
    {
        options.AddPolicy("Authenticated", policy => policy.RequireAuthenticatedUser());
    });
}

ProtectFolder 类是中间件本身。中间件对象上的 Invoke 方法是可注入的,因此如果当前请求指向受保护的文件夹,我们将请求当前授权服务并使用该服务授权用户。如果授权失败,我们使用身份验证管理器来质询用户,这通常会将浏览器重定向到登录页面,具体取决于应用程序的身份验证选项。

public class ProtectFolderOptions
{
    public PathString Path { get; set; }
    public string PolicyName { get; set; }
}

public static class ProtectFolderExtensions
{
    public static IApplicationBuilder UseProtectFolder(
        this IApplicationBuilder builder, 
        ProtectFolderOptions options)
    {
        return builder.UseMiddleware<ProtectFolder>(options);
    }
}

public class ProtectFolder
{
    private readonly RequestDelegate _next;
    private readonly PathString _path;
    private readonly string _policyName;

    public ProtectFolder(RequestDelegate next, ProtectFolderOptions options)
    {
        _next = next;
        _path = options.Path;
        _policyName = options.PolicyName;
    }

    public async Task Invoke(HttpContext httpContext, 
                             IAuthorizationService authorizationService)
    {
        if(httpContext.Request.Path.StartsWithSegments(_path))
        {
            var authorized = await authorizationService.AuthorizeAsync(
                                httpContext.User, null, _policyName);
            if (!authorized)
            {
                await httpContext.Authentication.ChallengeAsync();
                return;
            }
        }

        await _next(httpContext);
    }
}

回到应用程序的 Startup 类,我们将配置新的中间件以使用“Authenticated”策略保护 /secret 目录。

public void Configure(IApplicationBuilder app)
{
    app.UseCookieAuthentication(options =>
    {
        options.AutomaticAuthentication = true;
    });

    // This must be before UseStaticFiles.
    app.UseProtectFolder(new ProtectFolderOptions
    {
        Path = "/Secret",
        PolicyName = "Authenticated"
    });

    app.UseStaticFiles();

    // ... more middleware
}

【讨论】:

    【解决方案2】:

    如果您将其托管在 IIS 中,您仍然可以以相同的方式设置文件夹的安全性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-29
      • 2012-10-04
      • 2013-06-08
      相关资源
      最近更新 更多