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
-
objectToken
- 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
valuestringsetupAction<TokenOptions>The TokenOptions which may be configured.
Exceptions
- ArgumentNullException
valuecannot be null.- ArgumentException
valuecannot be empty or consist only of white-space characters.- ArgumentOutOfRangeException
valuecontained one or more white-space characters -or-valuewas less than MinimumLength characters -or-valuewas greater than MaximumLength characters -or-valuehad a character frequency greater than MaximumCharacterFrequency.