2011-06-21 60 views
0

登錄後,我想重定向到我的網站上具有SSL保護的安全區域。我試試這個:重定向到安全區域(ssl)

' After successfull authentication 
Dim serverName As String = HttpUtility.UrlEncode(HttpContext.Current.Request.ServerVariables("SERVER_NAME")) 

Dim vdirName As String = HttpContext.Current.Request.ApplicationPath 

Context.Response.Redirect("https://" & serverName & vdirName & "/Restrictedarea/Default.aspx", True) 

這是在服務器上工作,但當運行本地的端口號丟失。我如何編寫上述內容以在線和本地工作?

回答

2

謝謝你帶領我到URI建設者。我想出了這個解決方案,它既適用於本地(使用端口號),也適用於遠程。

Private Function pathCombine(ByVal p1 As String, ByVal p2 As String) As String 
    Return String.Format("{0}/{1}", p1.TrimEnd.TrimEnd(CChar("/")), p2.TrimStart(CChar("/"))) 
    End Function 

    Private Function getURI(ByVal https As Boolean, ByVal appendPath As String) As UriBuilder 
    Dim uri As New UriBuilder 

    ' set scheme/protocol 
    uri.Scheme = CStr(IIf(https, "https", "http")) 

    ' set port 
    Dim port As String = System.Web.HttpContext.Current.Request.ServerVariables("SERVER_PORT") 
    Select Case port 
     Case Nothing, "80", "443" 
     uri.Port = -1 
     Case Else 
     uri.Port = CInt(port) 
    End Select 

    ' set server/host   
    uri.Host = HttpUtility.UrlEncode(HttpContext.Current.Request.ServerVariables("SERVER_NAME")) 

    ' set the path 
    uri.Path = pathCombine(System.Web.HttpContext.Current.Request.ApplicationPath, appendPath) 

    Return uri 
    End Function 

兩個用例:


Context.Response.Redirect(getURI(True, "secure/Default.aspx").ToString, True) 

Dim uri As New UriBuilder(getURI(True, "secure/Default.aspx").ToString) 
uri.Query("foo=bar") 
Context.Response.Redirect(uri.ToString, True)