Skip to main content

ClaimsExtensions Class

Abblix.Oidc.Server

Abblix.Oidc.Server.Common

ClaimsExtensions Class

Helpers for working with System.Security.Claims.ClaimsPrincipal and System.Security.Claims.Claim sequences: classify claims as registered/public/private (per IANA), filter or replace them by type, and look up values by claim name in a case-insensitive manner.

public static class ClaimsExtensions

Inheritance System.Object → ClaimsExtensions

Methods

ClaimsExtensions.ExcludeClaims(this IEnumerable<Claim>, string[]) Method

Returns the claims whose type does not match any of the given types, using case-insensitive comparison.

public static System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> ExcludeClaims(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims, params string[] claimTypes);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

The source sequence of claims to filter.

claimTypes System.String[]

The claim type names to drop from the sequence.

Returns

System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

ClaimsExtensions.ExcludeRegisteredClaims(this IEnumerable<Claim>) Method

Returns the claims whose type is not in the IANA registered list (e.g. iss, sub, aud, exp). Useful when projecting only application-specific claims into a downstream payload.

public static System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> ExcludeRegisteredClaims(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

Returns

System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

ClaimsExtensions.Find(this IEnumerable<Claim>, string) Method

Returns the first claim whose type matches the given name (case-insensitive), or null when none exists.

public static System.Security.Claims.Claim? Find(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims, string name);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>
name System.String

Returns

System.Security.Claims.Claim

ClaimsExtensions.FindRequired(this IEnumerable<Claim>, string) Method

Returns the first claim of the given name, or throws when no such claim exists. Use when the caller treats absence of the claim as a programming error rather than a recoverable case.

public static System.Security.Claims.Claim FindRequired(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims, string name);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>
name System.String

Returns

System.Security.Claims.Claim

Exceptions

System.InvalidOperationException
No claim with the requested name was found.

ClaimsExtensions.FindValue(this IEnumerable<Claim>, string) Method

Returns the value of the first claim whose type matches the given name, or null when no such claim exists.

public static string? FindValue(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims, string name);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>
name System.String

Returns

System.String

ClaimsExtensions.GetPrivateClaims(this IEnumerable<Claim>) Method

These are the custom claims created to share information between parties that agree on using them and are neither registered or public claims.

public static System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> GetPrivateClaims(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

Returns

System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

ClaimsExtensions.GetPublicClaims(this IEnumerable<Claim>) Method

These can be defined at will by those using JWTs. But to avoid collisions they should be defined in the IANA JSON Web Token Registry or be defined as a URI that contains a collision resistant namespace.

public static System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> GetPublicClaims(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

Returns

System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

ClaimsExtensions.GetRegisteredClaims(this IEnumerable<Claim>) Method

These are a set of predefined claims which are not mandatory but recommended, to provide a set of useful, interoperable claims. Some of them are: iss (issuer), exp (expiration time), sub (subject), aud (audience), and others.

public static System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> GetRegisteredClaims(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

Returns

System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

ClaimsExtensions.GetUserClaimsOnly(this IEnumerable<Claim>) Method

Returns the user-facing claim subset: the subject claim plus everything that is not an IANA registered JWT claim. Drops protocol metadata (iss, exp, aud, etc.) while preserving identity and profile claims.

public static System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> GetUserClaimsOnly(this System.Collections.Generic.IEnumerable<System.Security.Claims.Claim> claims);

Parameters

claims System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

Returns

System.Collections.Generic.IEnumerable<System.Security.Claims.Claim>

ClaimsExtensions.IsAuthenticated(this ClaimsPrincipal) Method

Determines whether the principal carries an authenticated identity, treating a null principal or an unauthenticated identity as not authenticated.

public static bool IsAuthenticated(this System.Security.Claims.ClaimsPrincipal principal);

Parameters

principal System.Security.Claims.ClaimsPrincipal

The principal to inspect; may be null.

Returns

System.Boolean
true when the principal has an identity flagged as authenticated.

ClaimsExtensions.SetClaim(this ICollection<Claim>, string, string) Method

Inserts, updates, or removes a claim by name in the given collection so that exactly one claim with that type remains, holding the supplied value. Passing null for value deletes the claim. When the existing value already equals value, the collection is left untouched.

public static void SetClaim(this System.Collections.Generic.ICollection<System.Security.Claims.Claim> claims, string name, string? value);

Parameters

claims System.Collections.Generic.ICollection<System.Security.Claims.Claim>

The mutable claim collection to update in place.

name System.String

The claim type to set or remove.

value System.String

The new value, or null to remove any existing claim of that name.