如何强制相对 URI 使用 https?
我有一个相对 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
“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.
必须先将相对路径转换为绝对路径。我使用执行 Silverlight XAP 文件的 Uri 来执行此操作。
可能有一些方法可以稍微减少这种情况(使用 Uris 进行字符串操作感觉不对),但这只是一个开始:
这不包括传输端口号(您可能希望在其他情况下这样做)。就我个人而言,我会将上面的代码放在一个辅助方法中,该方法也可以处理端口(以及运行本地主机时您想要执行的不同操作)。
希望这有帮助。
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:
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.
相反,您应该更改托管 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.
该协议是一个单独的组件,因此我认为您可以将其放在相对地址的前面:
The protocol is a separate component, so I think that you can just put it in front of your relative address: