如何强制相对 URI 使用 https?

发布于 2024-09-19 10:51:30 字数 668 浏览 7 评论 0原文

我有一个相对 URI:

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

问题是,根据用户是否在 Web 浏览器中输入 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。那么如何将UriScheme更改为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 has typed https:// or http:// into their web browser to get to the silverlight application, it may use either http or https when trying to contact the service.

I want to force the program to use https for connecting to the service eitherway.

Initially I 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 at U.AbsoluteUri because it can't convert the relative Uri into an absolute Uri at that stage. So how do I change the Uri Scheme to https?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

天生の放荡 2024-09-26 10:51:34

“Scheme”在相对 URI 中没有任何意义。您必须在某个时候将其转换为绝对 URI 才能更改方案。

"Scheme" does not have any meaning in a relative URI. You'll have to convert it to an absolute URI at some point to change the scheme.

贱人配狗天长地久 2024-09-26 10:51:33

必须先将相对路径转换为绝对路径。我使用执行 Silverlight XAP 文件的 Uri 来执行此操作。

可能有一些方法可以稍微减少这种情况(使用 Uris 进行字符串操作感觉不对),但这只是一个开始:

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

这不包括传输端口号(您可能希望在其他情况下这样做)。就我个人而言,我会将上面的代码放在一个辅助方法中,该方法也可以处理端口(以及运行本地主机时您想要执行的不同操作)。

希望这有帮助。

The relative path has to be converted to absolute first. I do that using the Uri of the excuting Silverlight XAP file.

There might be ways to reduce this a bit (it feels wrong doing string operations with Uris) but it's a start:

    // 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 transferring a portnumber (which you might want to do in other circumstance). Personally I would put the above code in a helper method that also handles the port (and whatever you want to do differently when running localhost).

Hope this helps.

缱绻入梦 2024-09-26 10:51:33

相反,您应该更改托管 silverlight 的 ASPX 文件,并强制用户仅在使用非 SSL url 登录时重定向到 SSL。因为理想情况下,如果 silverlight 仅打开与其加载的相同域和方案的连接,那将是完美的。

Instead, you should change your ASPX file which hosts your silverlight, and force user to redirect to SSL only if he/she is logged in using non SSL url. Because ideally it would be perfect if silverlight opens connection only to the same domain and scheme it loaded from.

旧情别恋 2024-09-26 10:51:33

该协议是一个单独的组件,因此我认为您可以将其放在相对地址的前面:

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

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

Uri U = new Uri("https:../Services/Authenticated/VariationsService.svc", UriKind.Relative);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文