Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to prevent SQL Injection in a asp.net website

For email entry in a text box by the user i am doing client side check, to find whether the email is valid or not

 string emailexist = "SELECT COUNT(DISTINCT UserID) as count FROM tbl_user WHERE Email=@Email ";     


   <asp:RegularExpressionValidator ID="RegularExpressionValidator2" ValidationGroup="Login" ControlToValidate="txtUserName"
                            ValidationExpression="\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*" CssClass="Error"
                             runat="server" />

is this regular expression good enough to prevent sql injection for email.

Other Text:

   string groupExistQuery = "SELECT COUNT(DISTINCT GroupID) as count FROM tbl_group WHERE GroupName=@GroupName";   

I am doing a query in server side to check whether the group name entered by the user is already available in the database, there is a strong possibility to perform sql injection here. How should I prevent from it.

like image 497
Mark Avatar asked Nov 17 '11 14:11

Mark


People also ask

Is ASP Net vulnerable to SQL injection?

In fact, you don't even need to be using ASP.NET to be susceptible to SQL injection attacks. Any application that queries a database using user-entered data, including Windows Forms applications is a potential target of an injection attack. Protecting yourself against SQL injection attacks is not very difficult.

How can SQL injection be prevented?

The only sure way to prevent SQL Injection attacks is input validation and parametrized queries including prepared statements. The application code should never use the input directly. The developer must sanitize all input, not only web form inputs such as login forms.

What is the SQL injection how we can prevent SQL injection in Ado net?

SQL injection attacks can be performed in Entity SQL by supplying malicious input to values that are used in a query predicate and in parameter names. To avoid the risk of SQL injection, you should never combine user input with Entity SQL command text.

What is SQL injection in asp net?

SQL injection is a code injection technique that might destroy your database. SQL injection is one of the most common web hacking techniques. SQL injection is the placement of malicious code in SQL statements, via web page input.


1 Answers

A regex is unrelated to SQL injection (blacklisting etc is never the strongest approach); however, the use of the parameter @Email means (assuming it remains parameterised) that is not susceptible to SQL injection.

SQL injection relates to inappropriate concatenation of input; the main tool to fight it is parameters, which has already happened here.

For example, if you did:

var sql = "SELECT ...snip... WHERE Email='" + email + "'"; // BAD!!!!!

then that is heavily susceptible to SQL injection. By using a parameter, the value is not treated as part of the query, so the attacker does not have at attack vector.

like image 126
Marc Gravell Avatar answered Oct 04 '22 02:10

Marc Gravell