Click here to Skip to main content
15,887,449 members
Home / Discussions / C#
   

C#

 
AnswerRe: how to capture a screen shot usng c# and asp.net Pin
Pete O'Hanlon2-Nov-16 22:20
mvePete O'Hanlon2-Nov-16 22:20 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
syedkhaleel20102-Nov-16 22:40
syedkhaleel20102-Nov-16 22:40 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
Pete O'Hanlon2-Nov-16 22:51
mvePete O'Hanlon2-Nov-16 22:51 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
syedkhaleel20102-Nov-16 23:02
syedkhaleel20102-Nov-16 23:02 
GeneralRe: how to capture a screen shot usng c# and asp.net Pin
Pete O'Hanlon2-Nov-16 23:30
mvePete O'Hanlon2-Nov-16 23:30 
QuestionSQL injection prevention Pin
Member 128253812-Nov-16 8:28
Member 128253812-Nov-16 8:28 
AnswerRe: SQL injection prevention Pin
OriginalGriff2-Nov-16 9:46
mveOriginalGriff2-Nov-16 9:46 
AnswerRe: SQL injection prevention PinPopular
Richard Deeming2-Nov-16 10:10
mveRichard Deeming2-Nov-16 10:10 
No, you don't. ADO.NET already has plenty of protection from SQL Injection, in the form of parameterized queries.

Whenever you want to pass parameters to your query, pass them as parameters, rather than trying to stuff them into a dynamic query.
C#
// DON'T DO THIS:
command.CommandText = "SELECT <columns> FROM SomeTable WHERE SomeColumn = '" + parameterValue + "'";

// DO THIS:
command.CommandText = "SELECT <columns> FROM SomeTable WHERE SomeColumn = @Parameter";
command.Parameters.AddWithValue("@Parameter", parameterValue);


If you're writing dynamic SQL in your stored procedures, use sp_executesql[^] and pass the parameters as parameters.
SQL
-- DON'T DO THIS:
EXEC N'SELECT <columns> FROM SomeTable WHERE SomeColumn = ''' + @ParameterValue + '''';

-- DO THIS:
EXEC sp_executesql N'SELECT <columns> FROM SomeTable WHERE SomeColumn = @Parameter',
    N'@Parameter varchar(20)',
    @Parameter = @ParameterValue
;


If you find yourself passing dynamic things that can't be passed as parameters (column names, table names, etc.), try to find a way to avoid doing that. If you can't, then use the system views in SQL to validate the values to death:
SQL
-- DON'T DO THIS:
SET @Query = N'SELECT <columns> FROM ' + @TableName;

-- DO THIS:
DECLARE @TableID int = OBJECT_ID(@TableName);
If @TableID Is Null RAISERROR('Table does not exist.', 16, 1);

DECLARE @SchemaName sysname, @RealTableName sysname;

SELECT
    @SchemaName = S.name,
    @RealTableName = T.name
FROM
    sys.tables As T
    INNER JOIN sys.schemas As S
    ON S.schema_id = T.schema_id
WHERE
    T.id = @TableID
And
    T.type = 'U'
;

If @@ROWCOUNT = 0 RAISERROR('Table does not exist.', 16, 1);

SET @Query = N'SELECT <columns> FROM ' + QUOTENAME(@SchemaName) + N'.' + QUOTENAME(@RealTableName);

If you're passing multiple column names, then you'll need to use one of the many available SQL "split" functions to extract the individual column names.


In other words, rather than wasting your time trying to come up with a complicated scheme to try to detect some types of SQL Injection, use the built-in methods which prevent parameters from ever being treated as code.

Everything you wanted to know about SQL injection (but were afraid to ask) | Troy Hunt[^]
How can I explain SQL injection without technical jargon? | Information Security Stack Exchange[^]
Query Parameterization Cheat Sheet | OWASP[^]



"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer


AnswerRe: SQL injection prevention Pin
V.2-Nov-16 22:29
professionalV.2-Nov-16 22:29 
QuestionAsync Dispatcher Calls - Need an explaination Pin
Foothill1-Nov-16 6:44
professionalFoothill1-Nov-16 6:44 
AnswerRe: Async Dispatcher Calls - Need an explaination Pin
Richard Deeming1-Nov-16 7:48
mveRichard Deeming1-Nov-16 7:48 
GeneralRe: Async Dispatcher Calls - Need an explaination Pin
Foothill1-Nov-16 8:12
professionalFoothill1-Nov-16 8:12 
GeneralRe: Async Dispatcher Calls - Need an explaination Pin
Richard Deeming1-Nov-16 8:23
mveRichard Deeming1-Nov-16 8:23 
QuestionLabel printing with c# Pin
candogu1-Nov-16 0:57
candogu1-Nov-16 0:57 
AnswerRe: Label printing with c# Pin
OriginalGriff1-Nov-16 1:44
mveOriginalGriff1-Nov-16 1:44 
AnswerRe: Label printing with c# Pin
Richard Deeming1-Nov-16 3:37
mveRichard Deeming1-Nov-16 3:37 
AnswerRe: Label printing with c# Pin
cinias8-Nov-16 3:49
cinias8-Nov-16 3:49 
QuestionHow people generate bench mark graph for c# routine execution speed Pin
Tridip Bhattacharjee31-Oct-16 19:08
professionalTridip Bhattacharjee31-Oct-16 19:08 
AnswerRe: How people generate bench mark graph for c# routine execution speed Pin
Pete O'Hanlon31-Oct-16 20:52
mvePete O'Hanlon31-Oct-16 20:52 
QuestionHow to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 0:29
Rıza Berkay Ayçelebi31-Oct-16 0:29 
AnswerRe: How to implement the huffman using c# for image Pin
Pete O'Hanlon31-Oct-16 0:50
mvePete O'Hanlon31-Oct-16 0:50 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi31-Oct-16 23:14
Rıza Berkay Ayçelebi31-Oct-16 23:14 
GeneralRe: How to implement the huffman using c# for image Pin
Richard MacCutchan1-Nov-16 0:54
mveRichard MacCutchan1-Nov-16 0:54 
GeneralRe: How to implement the huffman using c# for image Pin
Rıza Berkay Ayçelebi1-Nov-16 1:36
Rıza Berkay Ayçelebi1-Nov-16 1:36 
GeneralRe: How to implement the huffman using c# for image Pin
Richard MacCutchan1-Nov-16 1:53
mveRichard MacCutchan1-Nov-16 1:53 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.