Solved

SqlException - is there a specific exception for 'login failed'?

Posted on 2006-11-22
2
274 Views
Last Modified: 2010-04-16
Hi,

I have a login dialogue for an application that connects to a database server.  If the connection.open fails after the user has entered their credentials I handle if with an SqlException and print the message.  I want to be a bit more specific though because I want to handle network failures differently to login failures.  Is there a way of distinguishing the types of exception?  Has anyone got a link to a list of all exceptions, or ways of getting information out of SqlException?

Thanks,

David
0
Comment
Question by:dave_gr
2 Comments
 
LVL 22

Accepted Solution

by:
Mohammed Nasman earned 200 total points
ID: 17995130
Yes, it's 18456

check sql server documentation for errors number

if you need to look for specific error numbers, you can make a test application that return the error numbers than search about them in the sql books online, for example

                  SqlConnection cn = new SqlConnection("Data source=.;user id =sa;password=123;");
                  try
                  {
                        cn.Open();
                  }
                  catch(SqlException ex)
                  {
                        MessageBox.Show(ex.Number.ToString());
                  }

will return the error number, use this number to get the detail of it, then use replace it with your specific error

or you can just use
MessageBox.Show(ex.Message);

this will give you login fail for the user ....


HTH

Best regards,
Mohammed Nasman
0
 
LVL 5

Assisted Solution

by:Collindsouza
Collindsouza earned 50 total points
ID: 17996942
Login Fail error number is 18456

If the server encounters an error that prevents a login from succeeding, the client will display the following error mesage.

Msg 18456, Level 14, State 1, Server <server name>, Line 1
Login failed for user '<user name>'

Note that the message is kept fairly nondescript to prevent information disclosure to unauthenticated clients.   In particular, the 'State' will always be shown to be '1' regardless of the nature of the problem.  To determine the true reason for the failure, the administrator can look in the server's error log where a corresponding entry will be written.  An example of an entry is:

2006-02-27 00:02:00.34 Logon     Error: 18456, Severity: 14, State: 8.

2006-02-27 00:02:00.34 Logon     Login failed for user '<user name>'. [CLIENT: <ip address>]


The key to the message is the 'State' which the server will accurately set to reflect the source of the problem.  In the example above, State 8 indicates that the authentication failed because the user provided an incorrect password.  The common error states and their descriptions are provided in the following table:

ERROR STATE  ERROR DESCRIPTION
 
2 and 5      Invalid userid
6       Attempt to use a Windows login name with SQL Authentication
7       Login disabled and password mismatch
8       Password mismatch
9       Invalid password
11 and 12 Valid login but server access failure
13       SQL Server service paused
18       Change password required

Now.. using try catch you know the type of exception occured and display messages accordingly


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
This article describes a simple method to resize a control at runtime.  It includes ready-to-use source code and a complete sample demonstration application.  We'll also talk about C# Extension Methods. Introduction In one of my applications…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now