为了防止规范化错误之类的安全风险,也可以使用Path类

例如,它从一个固定的文档目录返回文件数据,

 

FileInfo file=new FileInfo(Server.MapPath(@"Document\"+TextBox1.Text));


这里用的是客户端的路径,存在一个漏洞,
  举个例子,当客户端输入..\fileName这个路径的时候,
 这样这个路径就变化了,其实他已经忽略了Document\这个路径,我们可以借助Path类


 string fileName=Path.GetFileName(TextBox1.Text);//获取最后的文件名
 FileInfo file=new FileInfo(Server.MapPath(Path.Combine(@"Documents",fileName)));//把fileName加入到Documents路径之后

 

如果处理Url

string uriString = "http://www.cnblogs.com/gull";
System.Uri uri= new Uri(uriString);
string page = Path.GetFileName(uri.AbsolutePath);
System.Uri baseUri = new Uri("http://g.cn");
uri = new Uri(baseUri, page);

相关文章:

  • 2022-12-23
  • 2021-07-28
  • 2022-01-06
  • 2021-11-02
  • 2021-07-03
  • 2022-12-23
  • 2022-12-23
  • 2021-12-10
猜你喜欢
  • 2021-12-24
  • 2021-08-15
  • 2021-05-13
  • 2022-01-18
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
相关资源
相似解决方案