Table of Contents

Class Token

Namespace
Codebelt.SharedKernel
Assembly
Codebelt.SharedKernel.dll

Represents a Token object that encapsulates an immutable value used for identification or access control.

public record Token : SingleValueObject<string>, IEquatable<ValueObject>, IEquatable<SingleValueObject<string>>, IEquatable<Token>
Inheritance
object
Token
Implements
Derived
Inherited Members
SingleValueObject<string>.ToString()
SingleValueObject<string>.GetHashCode()
SingleValueObject<string>.Equals(object)
SingleValueObject<string>.Equals(ValueObject)
SingleValueObject<string>.Equals(SingleValueObject<string>)
SingleValueObject<string>.<Clone>$()
SingleValueObject<string>.EqualityContract

Examples

The following example constructs a Token from a 32-character hex string that satisfies the default minimum length, maximum length, and character-distribution rules, then logs the token so its presence can be audited in the request flow. Use the optional TokenOptions delegate when the supplied value needs bounds or character-distribution rules that differ from the defaults.

using System;
using Codebelt.SharedKernel;

namespace ApiKeySample;

public static class TokenProbe
{
    public static string AuditToken(string hexCandidate)
    {
        // The default TokenOptions (32-128 chars, 4+ distinct characters) reject unguessable candidates.
        var token = new Token(hexCandidate);
        var length = token.Value.Length;

        // Audit the token length against the configured minimum so the validation rule is observable.
        Console.WriteLine($"token length: {length}");
        return token.Value;
    }
}

Remarks

Constructors

Token(string, Action<TokenOptions>)

Initializes a new instance of the Token class.

public Token(string value, Action<TokenOptions> setup = null)

Parameters

value string

The string value to assign the role of Secret.

setup Action<TokenOptions>

The TokenOptions which may be configured.

Exceptions

ArgumentNullException

value cannot be null.

ArgumentException

value cannot be empty or consist only of white-space characters.

ArgumentOutOfRangeException

value contained one or more white-space characters -or- value was less than MinimumLength characters -or- value was greater than MaximumLength characters -or- value had a character frequency greater than MaximumCharacterFrequency.

See Also