MSDN Library

System.Net.Security.AuthenticationException

Represents the exception that is thrown when an error occurs during authentication.

Summary

The System.Net.Security.AuthenticationException class inherits from System.Net.Security.SecurityException. It is thrown by the .NET Framework when an authentication process fails. This can occur in various network security scenarios, such as establishing an SSL/TLS connection or using Windows integrated authentication.

Syntax


public class AuthenticationException : System.Net.Security.SecurityException
{
    // Public constructors
    public AuthenticationException();
    public AuthenticationException(string message);
    public AuthenticationException(string message, Exception innerException);

    // Protected constructors
    protected AuthenticationException(System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context);
}
            

Constructors

AuthenticationException()

Initializes a new instance of the AuthenticationException class.

AuthenticationException(string message)

Initializes a new instance of the AuthenticationException class with a specified error message.

AuthenticationException(string message, Exception innerException)

Initializes a new instance of the AuthenticationException class with a specified error message and a reference to the inner exception that is the cause of this exception.

Remarks

This exception is typically thrown when the client and server cannot agree on security protocols or credentials, or when there are issues with certificates during SSL/TLS negotiation. Developers should catch this exception to handle network authentication failures gracefully and provide informative feedback to the user or log the error for debugging.

Usage Example


try
{
    // Code that performs network authentication, e.g., SslStream.AuthenticateAsClient()
    // If authentication fails, an AuthenticationException might be thrown.
}
catch (System.Net.Security.AuthenticationException authEx)
{
    // Handle the authentication error
    Console.WriteLine($"Authentication failed: {authEx.Message}");
    if (authEx.InnerException != null)
    {
        Console.WriteLine($"Inner exception: {authEx.InnerException.Message}");
    }
}
catch (Exception ex)
{
    // Handle other potential exceptions
    Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
            

See Also