使用 imageresizer 和 IVirtualImageProvider 的认证和授权

Working with Auhtentication and Authorization for imageresizer and IVirtualImageProvider

我正在使用 http://imageresizing.net/ 组件在我的网络应用程序上显示图像。

我的问题是尽管我允许匿名用户,但在检索图像之前用户需要进行(表单)身份验证。

我通过实现它们的接口为 VirtualImageProvider 和 VirtualFile 创建了自己的实现:

    public class CustomVirtualImageProvider : IVirtualImageProvider, IPlugin
{
    public bool FileExists(string virtualPath, NameValueCollection queryString)
    {
        return true;
    }

    public IVirtualFile GetFile(string virtualPath, NameValueCollection queryString)
    {
        var customVirtualFile = new CustomVirtualFile(virtualPath, queryString);

        return customVirtualFile;
    }

    public IPlugin Install(Config c)
    {
        c.Plugins.add_plugin(this);
        return this;
    }

    public bool Uninstall(Config c)
    {
        c.Plugins.remove_plugin(this);
        return true;
    }
}

    public class CustomVirtualFile : IVirtualFile
{
    private readonly string _virtualPath;

    public CustomVirtualFile(string virtualPath, NameValueCollection query)
    {
        _virtualPath = virtualPath;
        this.query = new ResizeSettings(query);
    }

    protected ResizeSettings query;

    public System.IO.Stream Open()
    {
        var pathService = new ICustomImagePathService();

        string path = pathService.GetPhysicalPathByVirtual(_virtualPath);

        var fi = new FileInfo(path);
        if (!fi.Exists)
        {
            return null;
        }

        CurrentLogger.Logger.Debug("Processing: " + _virtualPath);

        var ms = new MemoryStream();
        using (var file = new FileStream(path, FileMode.Open, FileAccess.Read))
        {
            var bytes = new byte[file.Length];
            file.Read(bytes, 0, (int)file.Length);
            ms.Write(bytes, 0, (int)file.Length);
        }

        ms.Seek(0, SeekOrigin.Begin);

        return ms;
    }

    public string VirtualPath { get { return _virtualPath; } }
}

在 web.config 我设置了 DiskCache 插件:

<diskcache dir="/ImageCache" />

这个 /ImageCache 是 IIS 中的一个虚拟目录。

我正在使用 asp.net FormsAuthentication。

要求用户在从以下位置获取图像时需要进行身份验证: https://myhost/i/userprofile/(guid).

未经身份验证的 users/requests 可以从以下位置获取图像: https://myhost/i/public/logos/(guid).

为此,我在我的网络应用程序中创建了这些文件夹:

在这些文件夹中,我添加了一个测试 default.aspx 页面和这些 web.config 的:

/i

<?xml version="1.0"?>
<configuration>
  <system.web>
    <authorization>
     <deny users="?"/>
    </authorization>
  </system.web>
</configuration>

/i/public

<?xml version="1.0"?>
<configuration>
  <system.web>
    <authorization>
      <allow users="?"/>
    </authorization>
  </system.web>
</configuration>

使用网页进行测试有效:

/i/default.aspx --> 重定向到登录页面。好。

/i/public/default.aspx --> 显示页面。好。

/i/userprofile/d39a2fe0-2f1d-9750-e8d4-ebbe0f87d790.jpg?w=50&h=50 --> 重定向到登录页面。好。

/i/public/applogo/297/3347df8d-ef47-4280-ac2d-75a740b5898e.jpg?w=100&h=100 --> 重定向到登录页面。 不好.

有人可以帮我解决这个问题吗?我希望向未经身份验证的用户显示最后一张图片。

非常感谢。

我认为您可能在这里错误地使用了 URL 授权。我认为 ”?”表示任何经过身份验证的用户,但“*”表示所有用户,包括未经过身份验证的用户。

您可能还需要使用 <location> 元素来确保您的配置特定于某些 URL。

您是否考虑过使用 IIS URL 授权而不是 ASP.NET URL 授权? https://www.iis.net/learn/manage/configuring-security/understanding-iis-url-authorization

我通过进行以下更改使其正常工作:

1) 将匿名身份验证凭据(在 IIS 内)从 IUSR 更改为应用程序池标识

2) 将 runAllManagedModulesForAllRequests 设置回 "true"。在测试期间,我尝试禁用它。

全部使用ASP.NETUrl授权。