与数据绑定转发器控件一起使用时如何清理查询字符串?
给定以下 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
- Validate QueryStrings in ASP.NET (check occurs in code behind file)
- How to intercept and pre-process QueryStrings in Asp.Net (seems to drastic for a simple check)
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以使用
SQLDataSource
的Selecting
事件来检查querysting 值。该事件在调用 Select 方法之前触发。You could use the
Selecting
event of theSQLDataSource
where you can check the querysting value. This event fires before the Select Method is called.把字体去掉就可以了。查询字符串已经是一个字符串,除非您将其显式转换为其他内容(例如 int32)。如果出于某种原因将其转换为 int32,您可以稍后在更容易应用逻辑的代码中执行此操作。
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.