Verifying username and password in C#

837 views Asked by At

I've an application that's getting gmail email address and password from a database table, and verifying it, before sending any message using those emails, but the following part is returning me an exception, whenever there's any validation error occurs.

new System.Net.NetworkCredential(username, password);

How can I verify that username and password are correct without throwing any exception at runtime?

2

There are 2 answers

1
ChrisF On BEST ANSWER

This answer was provided in order to ascertain the exact nature of the problem as the code originally posted was insufficient.

According to the documentation the constructor doesn't raise an exception - unless you are using the version that takes a SecureString for the password.

In that case it raises a NotSupportedException if the SecureString class is not supported.

What exception is actually being raised?

0
Dan Abramov On

Documentation for NetworkCredential(string, string) doesn't say anything about exceptions. Mono's implementation doesn't throw exceptions in constructor either.

Do you get an exception when you try to send an email?

In either case, wrap it in a try/catch block. That's how exceptions are supposed to be handled.
If you're new to exceptions, be sure also to read Eric's post on this matter, it directly relates to your question.