Skip to main content

Definition

Assembly: Microsoft.OData.Mcp.Authentication.dll Namespace: Microsoft.OData.Mcp.Authentication.Models Inheritance: System.Object

Syntax

Microsoft.OData.Mcp.Authentication.Models.TokenValidationResult

Summary

Represents the result of a token validation operation.

Remarks

This class encapsulates the outcome of token validation, including success/failure status, the validated principal, and any validation errors that occurred during the process.

Constructors

.ctor

Initializes a new instance of the TokenValidationResult class.

Syntax

public TokenValidationResult()

.ctor

Initializes a new instance of the TokenValidationResult class for a successful validation.

Syntax

public TokenValidationResult(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe validated claims principal.

Exceptions

ExceptionDescription
ArgumentNullExceptionThrown when principal is null.

.ctor

Initializes a new instance of the TokenValidationResult class for a failed validation.

Syntax

public TokenValidationResult(string error, string errorDescription = null, System.Exception exception = null)

Parameters

NameTypeDescription
errorstringThe validation error.
errorDescriptionstring?The detailed error description.
exceptionSystem.Exception?The exception that caused the failure.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when error is null or whitespace.

.ctor Inherited

Inherited from object

Syntax

public Object()

Properties

Error

Gets or sets the validation error that occurred during token validation.

Syntax

public string Error { get; set; }

Property Value

Type: string? The validation error if validation failed; otherwise, null.

Remarks

This provides detailed information about why token validation failed, which can be useful for debugging and security auditing.

ErrorDescription

Gets or sets the detailed error description.

Syntax

public string ErrorDescription { get; set; }

Property Value

Type: string? A detailed description of the validation error; otherwise, null.

Remarks

This provides additional context about the validation failure, such as specific claims that were invalid or missing.

Exception

Gets or sets the exception that caused the validation failure.

Syntax

public System.Exception Exception { get; set; }

Property Value

Type: System.Exception? The exception that occurred during validation; otherwise, null.

Remarks

This is typically used for logging and debugging purposes to understand the root cause of validation failures.

ExpiresAt

Gets or sets the token expiration time.

Syntax

public System.Nullable<System.DateTime> ExpiresAt { get; set; }

Property Value

Type: System.Nullable<System.DateTime?>? The UTC date and time when the token expires; otherwise, null.

Remarks

This is extracted from the token’s ‘exp’ claim and represents when the token will no longer be valid for authentication.

IssuedAt

Gets or sets the token issued time.

Syntax

public System.Nullable<System.DateTime> IssuedAt { get; set; }

Property Value

Type: System.Nullable<System.DateTime?>? The UTC date and time when the token was issued; otherwise, null.

Remarks

This is extracted from the token’s ‘iat’ claim and represents when the token was originally created by the authorization server.

IsValid

Gets or sets a value indicating whether the token validation was successful.

Syntax

public bool IsValid { get; set; }

Property Value

Type: bool true if the token is valid; otherwise, false.

Metadata

Gets or sets additional validation metadata.

Syntax

public System.Collections.Generic.Dictionary<string, object> Metadata { get; set; }

Property Value

Type: System.Collections.Generic.Dictionary<string, object> A dictionary of additional information about the validation process.

Remarks

This can include information such as the validation time, token issuer, audience, or other metadata that might be useful for auditing or debugging.

NotBefore

Gets or sets the token not-before time.

Syntax

public System.Nullable<System.DateTime> NotBefore { get; set; }

Property Value

Type: System.Nullable<System.DateTime?>? The UTC date and time before which the token is not valid; otherwise, null.

Remarks

This is extracted from the token’s ‘nbf’ claim and represents the earliest time the token can be used for authentication.

Principal

Gets or sets the claims principal from the validated token.

Syntax

public System.Security.Claims.ClaimsPrincipal Principal { get; set; }

Property Value

Type: System.Security.Claims.ClaimsPrincipal? The claims principal if validation was successful; otherwise, null.

Remarks

This principal contains all the claims extracted from the JWT token and can be used for authorization decisions and user context extraction.

Methods

AddMetadata

Adds metadata to the validation result.

Syntax

public void AddMetadata(string key, object value)

Parameters

NameTypeDescription
keystringThe metadata key.
valueobjectThe metadata value.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when key is null or whitespace.

Equals Inherited Virtual

Inherited from object

Syntax

public virtual bool Equals(object obj)

Parameters

NameTypeDescription
objobject?-

Returns

Type: bool

Equals Inherited

Inherited from object

Syntax

public static bool Equals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

Failure

Creates a failed validation result.

Syntax

public static Microsoft.OData.Mcp.Authentication.Models.TokenValidationResult Failure(string error, string errorDescription = null, System.Exception exception = null)

Parameters

NameTypeDescription
errorstringThe validation error.
errorDescriptionstring?The detailed error description.
exceptionSystem.Exception?The exception that caused the failure.

Returns

Type: Microsoft.OData.Mcp.Authentication.Models.TokenValidationResult A failed token validation result.

Exceptions

ExceptionDescription
ArgumentExceptionThrown when error is null or whitespace.

GetHashCode Inherited Virtual

Inherited from object

Syntax

public virtual int GetHashCode()

Returns

Type: int

GetMetadata

Gets metadata value by key.

Syntax

public T GetMetadata<T>(string key)

Parameters

NameTypeDescription
keystringThe metadata key.

Returns

Type: T? The metadata value if found and of the correct type; otherwise, the default value.

Type Parameters

  • T - The type of the metadata value.

GetRemainingLifetime

Gets the remaining lifetime of the token.

Syntax

public System.Nullable<System.TimeSpan> GetRemainingLifetime()

Returns

Type: System.Nullable<System.TimeSpan?>? The remaining time before the token expires, or null if no expiration is set.

GetType Inherited

Inherited from object

Syntax

public System.Type GetType()

Returns

Type: System.Type

IsExpired

Determines whether the token is currently expired.

Syntax

public bool IsExpired()

Returns

Type: bool true if the token is expired; otherwise, false.

IsNotYetValid

Determines whether the token is not yet valid.

Syntax

public bool IsNotYetValid()

Returns

Type: bool true if the token is not yet valid; otherwise, false.

MemberwiseClone Inherited

Inherited from object

Syntax

protected internal object MemberwiseClone()

Returns

Type: object

ReferenceEquals Inherited

Inherited from object

Syntax

public static bool ReferenceEquals(object objA, object objB)

Parameters

NameTypeDescription
objAobject?-
objBobject?-

Returns

Type: bool

Success

Creates a successful validation result.

Syntax

public static Microsoft.OData.Mcp.Authentication.Models.TokenValidationResult Success(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe validated claims principal.

Returns

Type: Microsoft.OData.Mcp.Authentication.Models.TokenValidationResult A successful token validation result.

Exceptions

ExceptionDescription
ArgumentNullExceptionThrown when principal is null.

ToString Override

Returns a string representation of the token validation result.

Syntax

public override string ToString()

Returns

Type: string A summary of the validation result.

ToString Inherited Virtual

Inherited from object

Syntax

public virtual string ToString()

Returns

Type: string?