Class System.Net.Security.AuthenticationException
Represents the exception that is thrown when authentication fails.
Inheritance
Object
> Exception
> SystemException
> InvalidOperationException
> AuthenticationException
Remarks
The AuthenticationException
class is used to indicate that an authentication operation has failed. This can happen for various reasons, such as incorrect credentials, network issues, or server-side authentication problems.
When an AuthenticationException
is thrown, it typically contains information about the cause of the failure. You can catch this exception to handle authentication errors gracefully in your application.
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.
Properties
- Message Gets a message that describes the current exception.
- InnerException Gets a reference to the innermost exception that is the direct cause of the current exception.
- StackTrace Gets a string representation of the immediate frames of the call stack.
- HelpLink Gets or sets a link to the help file associated with this exception.
- Data Gets a dictionary of key/value pairs that provide additional user-defined information about the exception.
- Source Gets or sets the name of the application or the object that causes the error.
Methods
-
ToString()
Overrides
Exception.ToString()
to return a string representation of the current exception. -
GetObjectData(SerializationInfo info, StreamingContext context)
When overridden in a derived class, sets the
SerializationInfo
with information about the exception.
Example Usage
Here's a simple example of how you might catch an AuthenticationException
:
using System;
using System.Net.Security;
public class Example
{
public static void Main(string[] args)
{
try
{
// Simulate a scenario where authentication fails
PerformAuthentication();
}
catch (AuthenticationException ex)
{
Console.WriteLine($"Authentication failed: {ex.Message}");
if (ex.InnerException != null)
{
Console.WriteLine($"Inner exception: {ex.InnerException.Message}");
}
}
catch (Exception ex)
{
Console.WriteLine($"An unexpected error occurred: {ex.Message}");
}
}
public static void PerformAuthentication()
{
// In a real scenario, this would involve network operations
// and credential validation.
// For demonstration, we'll throw the exception directly.
throw new AuthenticationException("User credentials are not valid.");
}
}