Class SharedKeyToken
Provides shared key token generation and validation for secure API authentication.
public class SharedKeyToken
- Inheritance
-
SharedKeyToken
- Inherited Members
Remarks
This class uses SHA-512 hashing with a shared secret key, timestamp, and IP address to generate and validate time-limited authentication tokens.
Constructors
SharedKeyToken()
Initializes a new instance of the SharedKeyToken class.
public SharedKeyToken()
Remarks
The Initialize(string, string, bool) method must be called before using this instance.
SharedKeyToken(string, bool)
Initializes a new instance of the SharedKeyToken class with the specified secret key.
public SharedKeyToken(string secretKey, bool isOnlineMode)
Parameters
secretKeystringThe shared secret key used for token generation and validation.
isOnlineModeboolA value indicating whether to use online services to determine the external IP address.
SharedKeyToken(string, string, bool)
Initializes a new instance of the SharedKeyToken class with the specified secret key and remote server.
public SharedKeyToken(string secretKey, string remoteServer, bool isOnlineMode)
Parameters
secretKeystringThe shared secret key used for token generation and validation.
remoteServerstringThe remote server URL to query for the external IP address, or
nullto skip.isOnlineModeboolA value indicating whether to use online services to determine the external IP address.
Exceptions
- Exception
Thrown when initialization fails.
Properties
Expiration
Gets or sets the token expiration time window.
public TimeSpan Expiration { get; set; }
Property Value
Remarks
Tokens older than this time span will be considered expired during validation. The default value is 60 seconds.
Methods
Generate(string, string)
Generates a new shared key token for the specified HTTP method and API function.
public string Generate(string httpMethod, string apiFunction)
Parameters
httpMethodstringThe HTTP method (e.g., GET, POST) for the request.
apiFunctionstringThe API function or endpoint path.
Returns
- string
A token string containing the version, timestamp, and hash.
Initialize(string, string, bool)
Initializes the shared key token with the specified parameters.
public Task<bool> Initialize(string secretKey, string remoteServer, bool isOnlineMode)
Parameters
secretKeystringThe shared secret key used for token generation and validation.
remoteServerstringThe remote server URL to query for the external IP address, or
nullto skip.isOnlineModeboolA value indicating whether to use online services to determine the external IP address.
Returns
IsSharedKeyToken(string)
Determines whether the specified string is a valid shared key token format.
public bool IsSharedKeyToken(string token)
Parameters
tokenstringThe token string to check.
Returns
- bool
trueif the token has a valid shared key token format; otherwise,false.
Remarks
This method parses the token and stores its components for subsequent validation.
Validate(string, string, string, string)
Validates the specified token against the HTTP method, API function, and request IP.
public bool Validate(string token, string httpMethod, string apiFunction, string requestIp)
Parameters
tokenstringThe token string to validate.
httpMethodstringThe HTTP method (e.g., GET, POST) for the request.
apiFunctionstringThe API function or endpoint path.
requestIpstringThe IP address of the requester.
Returns
- bool
trueif the token is valid and not expired; otherwise,false.
Remarks
If the request IP does not match, validation is also attempted using the external IP address to handle NAT scenarios.