【问题标题】:How to pass part of url as variable in c#? [duplicate]如何在c#中将部分url作为变量传递? [复制]
【发布时间】:2013-07-29 10:37:35
【问题描述】:

我有以下网址:http://localhost:52416/Controls/Support_Survey.aspx?GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9

我需要从 URL 中获取 GUID 作为变量并将其传递到以下存储过程中:

 database.InsertUpdate(String.Format("CALL spSurveyAnswer_Insert('{0}', '{1}','{2}');", selectValue1, txtFeedBack.Text, PassGUID_HERE));

有什么想法吗??

提前致谢

【问题讨论】:

  • Eeekk... SQL 注入警报! :-) 使用参数开始!然后像Request.Querystring["GUID"]
  • 您可以从末尾倒数 36 个字符并将其转换为 guid,前提是您可以确定它始终位于末尾/相同格式
  • @Jonesy 这是个糟糕的主意。请永远不要那样做。
  • URL 上调用一个简单的split 函数怎么样?问题是你的url 中是否总是有一个?。如果是这样,那么两行代码可以解决你的问题关于将GUID 作为参数返回,如果您想看一个简单的示例,请告诉我
  • @DJKRAZE:内置函数可以从查询字符串中获取参数...

标签: c# url


【解决方案1】:

应该这样做:

Guid myGuid = new Guid(Request.Params["GUID"])

将其转换为实际的 Guid 也可以防止 SQL 注入攻击

【讨论】:

  • 根据问题中的代码,仅凭这一点还不足以防止SQL注入
  • @bengoesboom 但它会阻止从查询字符串中的 GUID 变量注入 SQL,不是吗?他没有注意sql字符串其他部分的值来自哪里
【解决方案2】:

我建议你这样做:

var requestGuid = Request.Params["GUID"];

if (string.IsNullOrEmpty(requestGuid))
{
    throw new InvalidOperationException("The request GUID is missing from the URL");
}

Guid guid;

if (!Guid.TryParse(requestGuid, out guid))
{
    throw new InvalidOperationException("The request GUID in the URL is not correctly formatted");
}

using(var connection = new SqlConnection("connection_string"))
{
    using(var command = new SqlCommand("spSurveyAnswer_Insert", connection))
    {
        command.CommandType = CommandType.StoredProcedure;        
        command.Parameters.AddWithValue("firstParamName", selectValue1);
        command.Parameters.AddWithValue("feedbackParamName", txtFeedBack.Text);
        command.Parameters.AddWithValue("guidParamName", guid);

        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

您不能保证 GUID 将在 URL 中是一个有效的 GUID,所以要防御并检查两者!然后使用参数化查询来帮助防止 SQL 注入 - 由于您正在调用存储过程,如果您在 proc 中滥用参数值,您仍然可以仍然进行 sql 注入,因此您也需要仔细编写。最后,还要妥善处理一次性资源。

【讨论】:

  • 非常感谢你 :) 完美运行 :D
【解决方案3】:

您应该使用RequestParamsQueryString(请参阅他们的文档以了解区别)来获取 GUID,并且出于安全原因,您应该在所有 SQL 命令和查询中使用参数,而不是字符串连接/格式化。我正在使用CommandType.StoredProcedure 允许的简化语法。参数名称("firstParamName" 等)应与存储过程中声明的实际参数名称匹配。

Guid myGuid = new Guid(Request.Params["GUID"]);

using (SqlConnection conn = // get connection)
using (SqlCommand command = new SqlCommand("spSurveyAnswer_Insert", conn))
{
    conn.Open();
    command.CommandType = CommandType.StoredProcedure;

    command.Parameters.AddWithValue("firstParamName", selectValue1);
    command.Parameters.AddWithValue("feedbackParamName", txtFeedBack.Text);
    command.Parameters.AddWithValue("guidParamName", myGuid);

    command.ExecuteNonQuery();
}

【讨论】:

    【解决方案4】:
    string url = "http://localhost:52416/Controls/Support_Survey.aspx?GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9";
    string lastPart = url.Split('?').Last().Replace("GUID=",string.Empty);
    

    您的代码是对 SQL 注入的探测,因此请使用 SqlCommand.Parameters Property

     SqlCommand command = // your sql command;
        database.InsertUpdate(String.Format("CALL spSurveyAnswer_Insert('{0}', '{1}','{2}');", @selectValue1, @txtFeedBack, @PassGUID_HERE));
    
        command.Parameters.AddWithValue("@selectValue1", selectValue1);
        command.Parameters.AddWithValue("@txtFeedBack", txtFeedBack.Text);
        command.Parameters.AddWithValue("@PassGUID_HERE", lastPart );
    

    【讨论】:

    • command.Parameters.Add 语法应该是 command.Parameters.AddWithValue 如果你想使用你的答案
    • 也使用Request.QueryString,不要自己解析url!你最终会得到GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9 而不是4aa4caca-f5cb-11e2-b582-635fb56c00b9
    • @TrevorPilley 对。但我用.Replace("GUID=",string.Empty); 摆脱了GUID=。感谢您指出。
    • @user1671639 只有在有人决定他们需要 url 中的另一个值并且它变为 GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9&Foo=Bar 时才会起作用,此时您的代码再次中断!
    • user1671639 如果您不理解为什么有人否决了您的答案,为了让您的答案有效,您需要在服务器上创建 StoredProcedure 并根据您的 Sql 命令调用存储过程,命令类型i.e StoredProcedure,并添加参数.. 看看蒂姆的例子非常简单,也是一个很好的学习工具
    【解决方案5】:
    Uri url = new Uri("http://localhost:52416/Controls/Support_Survey.aspx?GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9");
    
    string query = url.Query //query is now "GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9"
    
    string guidStr = query.Replace("GUID=", "");
    Guid guid = new Guid(guidStr);
    

    【讨论】:

    • 只有在有人决定他们需要 url 中的另一个值并且它变为 GUID=4aa4caca-f5cb-11e2-b582-635fb56c00b9&Foo=Bar 时才会起作用,此时您的代码会中断......
    猜你喜欢
    • 1970-01-01
    • 2019-01-19
    • 2013-09-29
    • 1970-01-01
    • 2019-12-04
    • 2019-08-10
    • 1970-01-01
    • 2020-01-27
    • 2013-04-13
    相关资源
    最近更新 更多