asp.net使用自定义httphandlers保护图像

时间:2013-06-20 09:07:16

标签: asp.net image protection custom-headers

我想保护我网页上的图片。

我想显示图像,但用户想要下载或保存图像,图像不应保存到本地硬盘。

我编写了自定义处理程序以限制下载图像的访问权限,但此处理程序也限制了图像的显示。

我的代码是

public class MyFileHandler : IHttpHandler
{

    public void ProcessRequest(HttpContext context)
    {
       context.Response.ContentType = "image/Jpeg";
       context.Response.Write("you dont have access");
    }

    public bool IsReusable
    {
        get
        {
            return false;
        }
    }
}
web.config中的

<configuration>
  <system.web>
    <compilation debug="true" targetFramework="4.0"/>
    <httpHandlers>
      <add verb="*" path="*.jpg" validate="true" type="MyFileHandler"/>
    </httpHandlers>
  </system.web>
</configuration>

是否有任何解决方案可以在网页中显示图像,但不保存或下载图像。

2 个答案:

答案 0 :(得分:0)

简而言之,没有。您向客户端浏览器提供的内容位于客户端计算机上,并且可以保存。它可能会保存在缓存中。无论你直接或通过处理程序提供图像,从浏览器的角度来看都是一样的。

如果您不希望某人能够将图像保存到硬盘驱动器,请不要将该图像放在网上。

答案 1 :(得分:0)

我能想到的一件事是为图像分配一个令牌,并让到期值非常短,例如以分钟为单位。例如,在 azure 存储帐户中,可以创建一个私有容器并使用 SAS 令牌在客户端显示图像。但正如其他人所说,没有办法阻止用户下载图片。

        [HttpGet("SasExample")]
        public async Task<IActionResult> SasExample()
        {
            string connectionString = "your-container-connection-string";
            BlobServiceClient blobServiceClient = new BlobServiceClient(connectionString);
            var sourceContainer = blobServiceClient.GetBlobContainerClient("container-name");
            await sourceContainer.CreateIfNotExistsAsync();

            BlobClient blob = sourceContainer.GetBlobClient("file-name");
            var res = GetServiceSasUriForBlob(blob, null);

            return Ok(res.AbsoluteUri);
        }



private static Uri GetServiceSasUriForBlob(BlobClient blobClient, string storedPolicyName = null)
        {
            // Check whether this BlobClient object has been authorized with Shared Key.
            if (blobClient.CanGenerateSasUri)
            {
                // Create a SAS token that's valid for one hour.
                BlobSasBuilder sasBuilder = new BlobSasBuilder()
                {
                    BlobContainerName = blobClient.GetParentBlobContainerClient().Name,
                    BlobName = blobClient.Name,
                    Resource = "b" //b for blob //c for container
                };

                if (storedPolicyName == null)
                {
                    sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1);
                    sasBuilder.SetPermissions(BlobSasPermissions.Read |
                        BlobSasPermissions.Write);
                }
                else
                {
                    sasBuilder.Identifier = storedPolicyName;
                }

                Uri sasUri = blobClient.GenerateSasUri(sasBuilder);
                Console.WriteLine("SAS URI for blob is: {0}", sasUri);
                Console.WriteLine();

                return sasUri;
            }
            else
            {
                Console.WriteLine(@"BlobClient must be authorized with Shared Key 
                          credentials to create a service SAS.");
                return null;
            }
        }
相关问题