2010-09-10 95 views
4

我有一個相對URI:如何強制相對URI使用https?

Uri U = new Uri("../Services/Authenticated/VariationsService.svc", 
           UriKind.Relative); 

的問題是,根據用戶是否已經輸入https://開頭或http://到他們的網絡瀏覽器才能到Silverlight應用程序,它可以使用任http或https嘗試聯繫服務時。

我想強制程序使用https連接到服務。

起初我嘗試這樣:

  Uri U = new Uri("../Services/Authenticated/VariationsService.svc", 
           UriKind.Relative); 

      string NU = U.AbsoluteUri; 

      U = new Uri(NU.Replace("http://", "https://"), UriKind.Absolute); 

但它在U.AbsoluteUri失敗,因爲它不能相對URI轉換成絕對URI在那個階段。那麼如何將Uri計劃更改爲https?

+0

有沒有必要在標題中重複標籤。我去爲你解決這個問題。 – Robaticus 2010-09-10 12:18:00

回答

1

相對路徑必須首先轉換爲絕對路徑。我使用流離失所的Silverlight XAP文件的Uri來做到這一點。

可能有辦法降低這個有點(感覺的錯誤行爲與尤里斯字符串操作),但它是一個開始:

// Get the executing XAP Uri 
    var appUri = App.Current.Host.Source; 

    // Remove the XAP filename 
    var appPath = appUri.AbsolutePath.Substring(0, appUri.AbsolutePath.LastIndexOf('/')); 

    // Construct the required absolute path 
    var rootPath = string.Format("https://{0}{1}", appUri.DnsSafeHost, appUri.AbsolutePath); 

    // Make the relative target Uri absolute (relative to the target Uri) 
    var uri = new Uri(new Uri(rootPath), "../Services/Authenticated/VariationsService.svc"); 

這不包括傳送端口號(你可能想在做其他情況)。就我個人而言,我會把上面的代碼放在一個幫助程序方法中,該方法也處理端口(以及在運行本地主機時您想要做的不同操作)。

希望這會有所幫助。

0

的協議是一個單獨的組件,所以我認爲你可以把它放在你的相對地址的面前:

Uri U = new Uri("https:../Services/Authenticated/VariationsService.svc", UriKind.Relative); 
+0

似乎不起作用: URI無效:無法分析權限/主機。 此錯誤在下列位置引發: EndpointAddress address = new EndpointAddress(U,null); – ForbesLindesay 2010-09-10 12:23:22

+0

它可能需要'//:'https://../ Services/Authenticated/VariationsService.svc' – 2010-09-10 12:29:13

+0

也許「https://../Services/Authenticated/VariationsService.svc」? – 2010-09-10 12:30:10

0

「計劃」沒有在一個相對URI任何意義。您必須在某個時刻將其轉換爲絕對URI才能更改該方案。

1

相反,您應該更改託管Silverlight的ASPX文件,並強制用戶只有在使用非SSL網址登錄時才重定向到SSL。因爲理想情況下,如果silverlight僅將連接打開到同一個域並且從其加載方案,那將是完美的。

+0

這並不回答這個問題,但我認爲這實際上是我要做的。謝謝 – ForbesLindesay 2010-09-13 15:21:35