如何在UWP App中编码/解码URL字符串 [英] How to encode/decode url strings in an UWP App
问题描述
我正在构建一个与Web-Api通信的UWP应用.在某个时候,我正在将URL中的字符串发送到Web-Api,可由用户操纵.因此,该字符串可以包含可能对Web-Api造成不良影响的字符.
例如:
这是我的UserController
I'm building a UWP App which communicates with a Web-Api. At some Point I'm sending a string in the url to the Web-Api which can be manipulated by the user. Because of that the string can include characters which could do evil things to the Web-Api.
For example:
This is my UserController
[Route("api/user/{uid}")]
public User GetUser(string uid)
{
return userRepository.GetByUid(uid);
}
在此示例中,我们假设用户可以在文本框中手动输入 uid
.现在,如果他放入
For the sake of this example we assume that the user can put in the uid
manually in a textbox. Now if he puts in
../vipuser
他可以访问VipUserController.因为 ../
上升了一个水平.
He could have access to the VipUserController. Because the ../
goes one hirachy up.
我进行了一些搜索,发现这篇SO文章建议使用 System.Web.UrlEncodeUnicode
和 System.Web.UrlDecode
.
I searched a little and found this SO article which recommends the use of System.Web.UrlEncodeUnicode
and System.Web.UrlDecode
.
但是,由于UWP Apps不包含 System.Web
命名空间,所以我想知道是否可以使用此方法的替代方法,以便在UWP-App中使用?
But since UWP Apps doesn't include the System.Web
namespace I was wondering if there is an alternative to this methods, which I can use into a UWP-App?
推荐答案
Uri.EscapeDataString()
和 Uri.UnescapeDataString()
将解决问题.
这篇关于如何在UWP App中编码/解码URL字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!