2015-02-23 54 views
0

在IIS中,有沒有辦法定義某種filtering rule拒絕訪問虛擬目錄中的文件,除非這些請求是通過某種加密查詢「預先簽名」的串?還有一種方法可以使請求過期嗎?我找不到控制這個的方法。IIS虛擬目錄獲取預先登記的URL到期

我在找什麼與Amazon S3 Amazon.S3.Model.GetPreSignedUrlRequest.Expires屬性提供的內容非常相似,但在IIS中。 Here是指向Amazon S3示例代碼的鏈接。

方案的預期目標:

請求:http://MyServerName/MyFolderThatIsAddedAsAVirtualDirectoryToDefaultWebsiteInIIS/MyImage.jpg 應該導致「拒絕訪問」的默認。但是,將特定的查詢字符串附加到請求URL應該可以訪問該文件。此外,我需要URL在一段時間後過期,直到提供了新的有效查詢字符串。

回答

1

這裏需要一些HTTP模塊來處理這個問題,因爲有自定義邏輯要實現QueryString匹配和過期。

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; 
    } 
} 
+0

很好的答案。謝謝, 感興趣的人:下面是一個鏈接,顯示如何在IIS中註冊HTTP模塊: https://msdn.microsoft.com/en-us/library/vstudio/ms227673%28v=vs.100 %29.aspx – yazanpro 2015-02-27 19:24:38

+0

謝謝,很好的鏈接,我應該包括它... – 2015-02-28 00:34:01

+0

我以前的評論的新鏈接,因爲上一個鏈接可能會過期:https://msdn.microsoft.com/en-us/library/ms227673% 28V = VS.100%29.aspx – yazanpro 2015-09-17 20:55:24