Verifying username and password in C#
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?
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?
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.
精彩评论