小编Rik*_*i B的帖子

清理字符串以防止相对URI路径

我创建了这个HTTP处理程序来更新本地SQL Express数据库中的信息.

我意识到用户可以使用相对URI路径"/../../file.zip"作为查询字符串,并且能够下载受限区域之外的文件.

该网站尚未生效,所以现在不是安全问题,但我真的想要阻止这样的事情.

我添加了一个简单的string.replace行,它从输入查询中删除任何"..".

我还有什么需要做的吗?

public void ProcessRequest(HttpContext context)
{
    string filesPath = "C:/Downloads/";
    string fileName = context.Request.QueryString["filename"];
    fileName = fileName.Replace("'", "''").Replace("..", "").Replace("/", "").Replace("\\", "");

    if (!string.IsNullOrEmpty(fileName) && File.Exists(filesPath + fileName))
    {
        context.Response.ContentType = "application/octet-stream";
        context.Response.AddHeader("Content-Disposition", string.Format("attachment; filename=\"{0}\"", fileName));
        context.Response.WriteFile(filesPath + fileName);
        //Do work to update SQL database here
    }
    else
    {
        context.Response.ContentType = "text/plain";
        context.Response.Write(filesPath + fileName + " Invalid filename");
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net security httphandler sanitize

6
推荐指数
1
解决办法
1944
查看次数

计算从抵达时间和旅行时间开始的时间

我需要从给定的到达和旅行时间计算发射时间.我已经研究过DateTime,但我不太确定我会怎么做.我使用monthCalander以下面的格式获取到达dateTime.

Example:

Arrival_time = 20/03/2013 09:00:00
Travel_time = 00:30:00

Launch_time = Arrival_time - Travel_time

Launch_time should equal: 20/03/2013 08:30:00
Run Code Online (Sandbox Code Playgroud)

有人能告诉我一个简单的方法来实现这一点.非常感谢.

c# datetime

3
推荐指数
1
解决办法
533
查看次数

TimeSpan不会解析一些字符串

我不确定这里发生了什么,但它会接受一些时间跨度,但不会接受其他时间跨度.有人能告诉我一种检查这种格式99:59:59的时间跨度的方法.

//50:30:00 is bad
//50:20:00 is good

try
{
    TimeSpan ts = new TimeSpan();
    ts = TimeSpan.Parse("50:30:00");
}
catch //(Exception ex)
{
    MessageBox.Show("bad time span");
}
Run Code Online (Sandbox Code Playgroud)

c# parsing timespan

-1
推荐指数
1
解决办法
275
查看次数

标签 统计

c# ×3

asp.net ×1

datetime ×1

httphandler ×1

parsing ×1

sanitize ×1

security ×1

timespan ×1