与数据绑定转发器控件一起使用时如何清理查询字符串?

发布于 2024-11-28 20:46:50 字数 1055 浏览 3 评论 0原文

给定以下 URL:

domain.com/page.aspx?id=123

当该查询字符串值用于数据绑定控件(例如中继器 SqlDataSource)时,如何清理该查询字符串值?

<asp:SqlDataSource ID="projectDataSource" runat="server" 
    ConnectionString="MyConnectionStrings" 
    SelectCommand="select foo from bar">
    <SelectParameters>
        <asp:QueryStringParameter 
            DefaultValue="0" 
            Name="idfromqs" 
            QueryStringField="id" 
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

这样 ?id=asdf 不会导致错误?

这些类似的问题有很好的答案,但似乎都不太符合我的问题

注意:这是一个内部应用程序,仅限于一小部分本地 IP 地址。我不太担心恶意 SQL 注入,而是更担心不太精明的用户看到令人讨厌的错误消息。

Given the following URL:

domain.com/page.aspx?id=123

How can I sanitize that query string value when it is used on a Databound Control such as a repeaters SqlDataSource?

<asp:SqlDataSource ID="projectDataSource" runat="server" 
    ConnectionString="MyConnectionStrings" 
    SelectCommand="select foo from bar">
    <SelectParameters>
        <asp:QueryStringParameter 
            DefaultValue="0" 
            Name="idfromqs" 
            QueryStringField="id" 
            Type="Int32" />
    </SelectParameters>
</asp:SqlDataSource>

Such that ?id=asdf does not result in an error?

These similar questions have good answers, but none of them seem to quite match my problem

Note: This is an internal application that is limited to a small block of local ip address. I'm less worried about malicious sql injection and more about preventing less savvy users from seeming nasty error messages.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

荒人说梦 2024-12-05 20:46:50

您可以使用SQLDataSourceSelecting 事件来检查querysting 值。该事件在调用 Select 方法之前触发。

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSource1.SelectParameters.Clear(); //Clear existing parameters
    // based on your check, you can pass the default value
    SqlDataSource1.SelectParameters["idfromqs"].DefaultValue = "Set Value here";

}

You could use the Selecting event of the SQLDataSource where you can check the querysting value. This event fires before the Select Method is called.

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{
    SqlDataSource1.SelectParameters.Clear(); //Clear existing parameters
    // based on your check, you can pass the default value
    SqlDataSource1.SelectParameters["idfromqs"].DefaultValue = "Set Value here";

}
杯别 2024-12-05 20:46:50

把字体去掉就可以了。查询字符串已经是一个字符串,除非您将其显式转换为其他内容(例如 int32)。如果出于某种原因将其转换为 int32,您可以稍后在更容易应用逻辑的代码中执行此操作。

<asp:QueryStringParameter 
   DefaultValue="0" 
   Name="idfromqs" 
   QueryStringField="id" />

Just take the type off. A querystring is a already string, unless you convert it explicitly to something else (like an int32). If there's some reason to convert it to an int32, you can do that later int he code where it's easier to apply logic to.

<asp:QueryStringParameter 
   DefaultValue="0" 
   Name="idfromqs" 
   QueryStringField="id" />
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文