MSDN Documentation

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

Properties

Methods

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.");
    }
}
            

See Also