2013-03-05 66 views
1

是否有人知道如何在IIS中使用URL重寫2重定向到所請求URL的https版本,如果用戶通過了身份驗證(通過表單身份驗證)。用戶使用URL重寫登錄時重定向到HTTPS 2

基本上我只是想確保用戶登錄通過https並同時他們已經登錄他們的整個會話保持HTTPS。

我知道我可以設置窗體身份驗證cookie的requireSSL屬性,所以我這樣做也是如此,但也希望從http重定向到https。

我認爲我們可以把代碼在Global.asax中,而不是達到我想要的東西,但它是整潔(以及可能獲得更好的性能),如果我們能在Web.config中使用URL重寫定義這個2

回答

1

如果您使用的是ASP.NET MVC,那麼此操作篩選器可以提供幫助,信用必須轉到原始作者。

//http://weblogs.asp.net/dwahlin/archive/2009/08/25/requiring-ssl-for-asp-net-mvc-controllers.aspx 
public class RequiresSsl : ActionFilterAttribute 
{ 
    public override void OnActionExecuting(ActionExecutingContext filterContext) 
    { 
     var req = filterContext.HttpContext.Request; 
     var res = filterContext.HttpContext.Response; 

     //Check if we're secure or not and if we're on the local box 
     if (!req.IsSecureConnection && !req.IsLocal) 
     { 
      if (req.Url != null) 
      { 
       var builder = new UriBuilder(req.Url) { Scheme = Uri.UriSchemeHttps, Port = 443, Host = req.Url.Host.StartsWith("www.", StringComparison.OrdinalIgnoreCase) ? req.Url.Host : "www." + req.Url.Host }; 
       res.Redirect(builder.Uri.ToString()); 
      } 
     } 
     base.OnActionExecuting(filterContext); 
    } 
} 
+0

感謝凱恩,對不起,我應該說我們使用的ASP.NET web表單不是MVC。 – 2013-03-05 13:16:17

相關問題