11.07.2015 Views

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

Improving Web Application Security: Threats and - CGISecurity

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

390 Part III: Building Secure <strong>Web</strong> <strong>Application</strong>sLogging ExceptionsYou should also log details from the SqlException class. This class exposes propertiesthat contain details of the exception condition. These include a Message property thatdescribes the error, a Number property that uniquely identifies the type of error, <strong>and</strong>a State property that contains additional information. The State property is usuallyused to indicate a particular occurrence of a specific error condition. For example, if astored procedure generates the same error from more than one line, the Stateproperty indicates the specific occurrence. Finally, an Errors collection containsSqlError objects that provide detailed SQL server error information.The following code fragment shows how to h<strong>and</strong>le a SQL Server error condition byusing the SQL Server .NET Framework data provider:using System.Data;using System.Data.SqlClient;using System.Diagnostics;// Method exposed by a Data Access Layer (DAL) Componentpublic string GetProductName( int ProductID ){SqlConnection conn = new SqlConnection("server=(local);Integrated <strong>Security</strong>=SSPI;database=products");// Enclose all data access code within a try blocktry{conn.Open();SqlComm<strong>and</strong> cmd = new SqlComm<strong>and</strong>("LookupProductName", conn );cmd.Comm<strong>and</strong>Type = Comm<strong>and</strong>Type.StoredProcedure;cmd.Parameters.Add("@ProductID", ProductID );SqlParameter paramPN =cmd.Parameters.Add("@ProductName", SqlDbType.VarChar, 40 );paramPN.Direction = ParameterDirection.Output;cmd.ExecuteNonQuery();// The finally code is executed before the method returnsreturn paramPN.Value.ToString();}catch (SqlException sqlex){// H<strong>and</strong>le data access exception condition// Log specific exception detailsLogException(sqlex);// Wrap the current exception in a more relevant// outer exception <strong>and</strong> re-throw the new exceptionthrow new Exception("Failed to retrieve product details for product ID: " +ProductID.ToString(), sqlex );}

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!