重定向到安全区域(ssl)

时间:2011-06-21 15:15:14

标签: asp.net redirect https

登录后,我想重定向到我网站上具有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 个答案:

答案 0 :(得分: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)

答案 1 :(得分:1)

使用UriBuilder获取所需信息