Skip to main content

Definition

Assembly: System.Security.Claims.dll Namespace: System.Security.Claims

Syntax

System.Security.Claims.ClaimsPrincipal

Summary

This type is defined in System.Security.Claims.

Remarks

See Microsoft documentation for more information about the rest of the API.

Methods

GetUserEmail Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Gets the user email from the claims principal.

Syntax

public static string GetUserEmail(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to extract the email from.

Returns

Type: string? The user email, or null if not found.

Remarks

This method looks for the email in common claim types used by different authentication providers.

GetUserId Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Gets the user identifier from the claims principal.

Syntax

public static string GetUserId(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to extract the user ID from.

Returns

Type: string? The user identifier, or null if not found.

Remarks

This method looks for the user identifier in common claim types used by different authentication providers.

GetUserName Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Gets the username from the claims principal.

Syntax

public static string GetUserName(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to extract the username from.

Returns

Type: string? The username, or null if not found.

Remarks

This method looks for the username in common claim types used by different authentication providers.

GetUserRoles Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Gets the user roles from the claims principal.

Syntax

public static System.Collections.Generic.IEnumerable<string> GetUserRoles(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to extract roles from.

Returns

Type: System.Collections.Generic.IEnumerable<string> A collection of user roles, or empty if no roles are found.

Examples

var userRoles = User.GetUserRoles();
if (userRoles.Contains("Administrator"))
{
    // User has administrator role
}

Remarks

This method looks for roles in various claim types commonly used by different authentication providers. Roles can be in individual claims or comma-separated values.

GetUserScopes Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Gets the user scopes from the claims principal.

Syntax

public static System.Collections.Generic.IEnumerable<string> GetUserScopes(System.Security.Claims.ClaimsPrincipal principal)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to extract scopes from.

Returns

Type: System.Collections.Generic.IEnumerable<string> A collection of user scopes, or empty if no scopes are found.

Examples

var userScopes = User.GetUserScopes();
if (userScopes.Contains("read:users"))
{
    // User has permission to read users
}

Remarks

This method looks for scopes in various claim types commonly used by different authentication providers. Scopes are typically space-separated values in a single claim.

HasRole Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Determines whether the user has the specified role.

Syntax

public static bool HasRole(System.Security.Claims.ClaimsPrincipal principal, string role)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to check.
rolestringThe role to check for.

Returns

Type: bool true if the user has the specified role; otherwise, false.

HasScope Extension

Extension method from System.Security.Claims.McpAuthentication_ClaimsPrincipalExtensions
Determines whether the user has the specified scope.

Syntax

public static bool HasScope(System.Security.Claims.ClaimsPrincipal principal, string scope)

Parameters

NameTypeDescription
principalSystem.Security.Claims.ClaimsPrincipalThe claims principal to check.
scopestringThe scope to check for.

Returns

Type: bool true if the user has the specified scope; otherwise, false.