Definition
Assembly: Microsoft.OData.Mcp.Authentication.dll Namespace: Microsoft.OData.Mcp.Authentication.Models Inheritance: System.ObjectSyntax
Summary
Represents the user context extracted from an authenticated request.Remarks
This class encapsulates the user’s identity, authorization information, and other context data needed for processing MCP requests on behalf of the user.Constructors
.ctor
Initializes a new instance of the UserContext class.Syntax
.ctor
Initializes a new instance of the UserContext class with the specified user ID.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
userId | string | The user’s unique identifier. |
Exceptions
| Exception | Description |
|---|---|
ArgumentException | Thrown when userId is null or whitespace. |
.ctor Inherited
Inherited from
objectSyntax
Properties
AdditionalClaims
Gets or sets additional user claims.Syntax
Property Value
Type:System.Collections.Generic.Dictionary<string, string>
A dictionary of additional claims extracted from the token.
Remarks
This contains any custom claims that are not covered by the standard properties but may be needed for authorization or business logic.Audience
Gets or sets the audience for which the token was issued.Syntax
Property Value
Type:string?
The audience identifier from the JWT token.
Remarks
This identifies the intended recipient of the token, which should match the MCP server’s configuration.AuthenticatedAt
Gets or sets the time when the user was authenticated.Syntax
Property Value
Type:System.DateTime?
The UTC date and time when authentication occurred.
Remarks
This timestamp is used for session management, auditing, and security analysis.AuthenticationMethod
Gets or sets the authentication method used.Syntax
Property Value
Type:string?
The method used to authenticate the user (e.g., “Bearer”, “JWT”).
Remarks
This indicates how the user was authenticated, which can be useful for security auditing and compliance reporting.ClientId
Gets or sets the client application identifier.Syntax
Property Value
Type:string?
The identifier of the client application that initiated the request.
Remarks
This identifies which application the user is accessing the MCP server through, which can be useful for auditing and access control.DisplayName
Gets or sets the user’s display name.Syntax
Property Value
Type:string?
The display name or username of the authenticated user.
Remarks
This is typically extracted from claims like ‘name’, ‘preferred_username’, or ‘upn’ and is used for display purposes in logs and audit trails.Syntax
Property Value
Type:string?
The email address of the authenticated user.
Remarks
This is typically extracted from the ‘email’ claim and can be used for notifications or audit purposes.Issuer
Gets or sets the issuer of the authentication token.Syntax
Property Value
Type:string?
The issuer identifier from the JWT token.
Remarks
This identifies which authorization server issued the token, which is important for multi-provider scenarios and security auditing.Roles
Gets or sets the user’s roles.Syntax
Property Value
Type:System.Collections.Generic.List<string>
A collection of roles assigned to the user.
Remarks
Roles provide a higher-level grouping of permissions and are typically extracted from ‘roles’ or similar claims in the token.Scopes
Gets or sets the OAuth2 scopes granted to the user.Syntax
Property Value
Type:System.Collections.Generic.List<string>
A collection of OAuth2 scopes that define the user’s permissions.
Remarks
These scopes are extracted from the token and determine what operations the user is authorized to perform through the MCP server.TenantId
Gets or sets the tenant identifier for multi-tenant scenarios.Syntax
Property Value
Type:string?
The identifier of the tenant the user belongs to.
Remarks
This is used in multi-tenant deployments to isolate data and operations between different organizational units or customers.Token
Gets or sets the original JWT token.Syntax
Property Value
Type:string?
The raw JWT token that was used for authentication.
Remarks
This token can be forwarded to downstream services for delegation scenarios while maintaining the user’s identity.TokenExpiresAt
Gets or sets the token expiration time.Syntax
Property Value
Type:System.Nullable<System.DateTime?>?
The UTC date and time when the token expires.
Remarks
This is used to determine when the user’s session will expire and when token refresh might be needed.UserId
Gets or sets the user’s unique identifier.Syntax
Property Value
Type:string
The unique identifier for the authenticated user.
Remarks
This is typically extracted from the ‘sub’ (subject) claim in the JWT token and uniquely identifies the user across the system.Methods
Equals Inherited Virtual
Inherited from
objectSyntax
Parameters
| Name | Type | Description |
|---|---|---|
obj | object? | - |
Returns
Type:bool
Equals Inherited
Inherited from
objectSyntax
Parameters
| Name | Type | Description |
|---|---|---|
objA | object? | - |
objB | object? | - |
Returns
Type:bool
FromClaimsPrincipal
Creates a user context from a claims principal.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
principal | System.Security.Claims.ClaimsPrincipal | The claims principal to extract user context from. |
token | string? | The original JWT token (optional). |
Returns
Type:Microsoft.OData.Mcp.Authentication.Models.UserContext
A user context populated with information from the claims principal.
Exceptions
| Exception | Description |
|---|---|
ArgumentNullException | Thrown when principal is null. |
InvalidOperationException | Thrown when the principal does not contain a subject claim. |
GetAdditionalClaim
Gets an additional claim value by type.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
claimType | string | The claim type to retrieve. |
Returns
Type:string?
The claim value if found; otherwise, null.
GetHashCode Inherited Virtual
Inherited from
objectSyntax
Returns
Type:int
GetRemainingTokenLifetime
Gets the remaining time before the token expires.Syntax
Returns
Type:System.Nullable<System.TimeSpan?>?
The remaining time before token expiration, or null if no expiration is set.
GetType Inherited
Inherited from
objectSyntax
Returns
Type:System.Type
HasAllScopes
Determines whether the user has all of the specified scopes.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
requiredScopes | System.Collections.Generic.IEnumerable<string> | The scopes to check for. |
Returns
Type:bool
true if the user has all of the required scopes; otherwise, false.
Exceptions
| Exception | Description |
|---|---|
ArgumentNullException | Thrown when requiredScopes is null. |
HasAnyRole
Determines whether the user has any of the specified roles.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
requiredRoles | System.Collections.Generic.IEnumerable<string> | The roles to check for. |
Returns
Type:bool
true if the user has at least one of the required roles; otherwise, false.
Exceptions
| Exception | Description |
|---|---|
ArgumentNullException | Thrown when requiredRoles is null. |
HasAnyScope
Determines whether the user has any of the specified scopes.Syntax
Parameters
| Name | Type | Description |
|---|---|---|
requiredScopes | System.Collections.Generic.IEnumerable<string> | The scopes to check for. |
Returns
Type:bool
true if the user has at least one of the required scopes; otherwise, false.
Exceptions
| Exception | Description |
|---|---|
ArgumentNullException | Thrown when requiredScopes is null. |
IsTokenExpired
Determines whether the user’s token is expired.Syntax
Returns
Type:bool
true if the token is expired; otherwise, false.
MemberwiseClone Inherited
Inherited from
objectSyntax
Returns
Type:object
ReferenceEquals Inherited
Inherited from
objectSyntax
Parameters
| Name | Type | Description |
|---|---|---|
objA | object? | - |
objB | object? | - |
Returns
Type:bool
ToString Override
Returns a string representation of the user context.Syntax
Returns
Type:string
A summary of the user context.
ToString Inherited Virtual
Inherited from
objectSyntax
Returns
Type:string?