IIS虚拟目录中获取pre标识的URL与到期 [英] IIS Virtual Directory Get Pre Signed URL with Expiration

查看:168
本文介绍了IIS虚拟目录中获取pre标识的URL与到期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在IIS中,有没有一种方法来定义某种类型的过滤明确规条拒绝一个虚拟目录中访问文件,除非请求是pre签署通过某种加密的查询字符串?也就是有办法让请求过期吗?我不能找到一种方法,有超过这个控制。

in IIS, Is there a way to define some kind of a filtering rule to deny access to files within a virtual directory unless the requests are "pre signed" by some sort of an encrypted query string? Also is there a way to make the request expire? I can't find a way to have control over this.

我正在寻找的是非常相似,亚马逊S3 Amazon.S3.Model.Get preSignedUrlRequest.Expires 属性提供,但在IIS中。 这里是一个链接到Amazon S3的样本code。

What I'm looking for is very similar to what Amazon S3 Amazon.S3.Model.GetPreSignedUrlRequest.Expires property delivers, but in IIS. Here is a link to the Amazon S3 sample code.

的预期目标情景:

请求:的http://MyServerName/MyFolderThatIsAddedAsAVirtualDirectoryToDefaultWebsiteInIIS/MyImage.jpg 应该导致拒绝访问的默认。然而,具有附加在请求URL的特定查询字符串应该给访问该文件。另外,我需要URL的一段时间,直到提供了一种新的有效的查询字符串后过期。

Requesting: http://MyServerName/MyFolderThatIsAddedAsAVirtualDirectoryToDefaultWebsiteInIIS/MyImage.jpg should always result in "Access Denied" by default. However, having a particular query string appended to the request URL should give access to the file. Also, I need the URL to expire after a certain period of time until a new valid query string is provided.

推荐答案

您将需要某种形式的HTTP模块这里来处理这个,因为是定制逻辑实现的查询字符串匹配和过期。

You will need some sort of HTTP Module here to deal with this as there is custom logic to implement for QueryString matching and expiration.

  public class HttpFilterModule : IHttpModule
  {
    public void Dispose()
    {
    }

    public void Init(HttpApplication context)
    {
        context.BeginRequest += context_BeginRequest;
    }

    void context_BeginRequest(object sender, EventArgs e)
    {
        var qs = HttpContext.Current.Request.QueryString["SomeKeyToCheck"];
        var url = HttpContext.Current.Request.Url;

        if (MatchesUrl(url))
        {
            if (!IsAuthenticatedByQueryString(qs))
            {
                HttpContext.Current.Response.StatusCode = HttpStatusCode.Unauthorized;
                HttpContext.Current.Response.End();
            }
        }
    }

    private bool IsAuthenticatedByQueryString(string qs)
    {
        //  implement code here to check qs value
        //  probably against a DB or cache of tokens
        return true;
    }

    private bool MatchesUrl(Uri url)
    {
        //  implement code here to match the URL, 
        //  probably against configuration
        return true;
    }
}

这篇关于IIS虚拟目录中获取pre标识的URL与到期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆