How to force relative URI to use https?

I have a relative URI:

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

The problem is that depending on whether the user typed https: // or http: // into his web browser to access the silverlight application, he can use either http or https when trying to contact the service.

I want to force the program to use https to connect to the service anyway.

I originally tried this:

  Uri U = new Uri("../Services/Authenticated/VariationsService.svc", UriKind.Relative); string NU = U.AbsoluteUri; U = new Uri(NU.Replace("http://", "https://"), UriKind.Absolute); 

But it fails in U.AbsoluteUri because it cannot convert relative Uri to absolute Uri at this point. So how do I change the Uri scheme to https?

+4
source share
4 answers

The relative path must first be converted to absolute. I do this using the Uri from an excull Silverlight XAP file.

There may be ways to reduce this a bit (he is mistaken when doing string operations with Uris), but this is the beginning:

  // 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"); 

This does not include port number porting (which you might want to do in other circumstances). Personally, I would put the above code in a helper method that also handles the port (and everything you want to do differently when running localhost).

Hope this helps.

+1
source

Instead, you should change your .aspx file, which hosts your Silverlight, and force the user to redirect to SSL only if he / she is logged in using a non-SSL-url. Because it would be ideal if silverlight opens a connection only with the same domain and its loading scheme.

+1
source

The protocol is a separate component, so I think you can just put it in front of your relative address:

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

"Schema" has no meaning in the relative URI. You will need to convert it to an absolute URI at some point in order to change the scheme.

0
source

All Articles