comdirect Rest API Sample
Posted on August 21, 2024 (Last modified on August 24, 2024) • 6 min read • 1,104 wordsDoit.Comdirect.Rest.Api: C# .NET Rest API for comdirect BackgroundService sample
Source CodeVisual Studio XML documentation Markdown syntax created with: Vsxmd
C# .NET Rest API for comdirect
Get your oAuth client credentials before you start on the comdirect Website
"CdSecondary": {
"type": "oauth2",
"tokenUrl": "https://api.comdirect.de/oauth/token",
"flow": "cd_secondary", >> **"flow": "password"**,
{
"ComdirectCredentials": {
"ClientId": null,
"ClientSecret": null,
"Pin": null,
"Username": null
}
}
"ComdirectSavedSession": {
"SessionId": null,
"RequestId": null,
"RefreshToken": null,
"LastSessionDateTime": null,
"ExpiresInSeconds": null
}
The sample use Hangfire.InMemory for testing only. Use a persistent storage like SQL Server or Redis for production.
The sample use Serilog to log messages configured in the appsettings.json
Use api carefully!
comdirect API doc:
2.3 Anlage Validierung einer Session-TAN POST URL-Präfix/session/clients/{clientId}/v1/sessions/{sessionId}/validate Beschreibung: Für das nun bekannte Session-Objekt wird in diesem Schritt eine TAN-Challenge angefordert.
Caution
Das Abrufen von fünf TAN-Challenges ohne zwischenzeitliche Entwertung einer korrekten TAN führt zur Sperrung des Onlinebanking-Zugangs
type
ConsoleSample
constructor
Initializes a new instance of the ComdirectApiHostedService class. This class is responsible for managing the background service that interacts with the Comdirect API.
Name | Type | Description |
---|---|---|
backgroundJobs | Hangfire.IBackgroundJobClient | An instance of IBackgroundJobClient for managing background jobs. |
recurringJobs | Hangfire.IRecurringJobManager | An instance of IRecurringJobManager for managing recurring jobs. |
logger | Microsoft.Extensions.Logging.ILogger{ConsoleSample.ComdirectApiHostedService} | An instance of ILogger`1 for logging messages. |
configuration | Microsoft.Extensions.Configuration.IConfiguration | An instance of IConfiguration for accessing application settings. |
Name | Description |
---|---|
System.ArgumentNullException | Thrown when any of the required parameters are null. |
System.Exception | Thrown when the ComdirectCredentials configuration is missing in appsettings.json. |
method
Performs the secondary authentication flow for the Comdirect API. This method is responsible for obtaining a valid access token and refresh token, and then saving the session details for future use.
An asynchronous Task that completes when the secondary authentication flow is completed.
Name | Type | Description |
---|---|---|
authClient | Comdirect.Rest.Api.AuthClient | An instance of the AuthClient class, which provides methods for interacting with the Comdirect |
authentication API. | ||
token | Comdirect.Rest.Api.ComdirectOAuthToken | The ComdirectOAuthToken object containing the access token obtained during the initial authentication flow. |
method
Checks if the saved TAN (Transaction Authentication Number) can be reused based on the session validity.
Returns true if the saved TAN can be reused, false otherwise.
This method has no parameters.
method
Initializes a new instance of the AuthClient class for interacting with the Comdirect authentication API. This method handles the authentication flow, including obtaining a valid access token and refresh token. If a saved TAN can be reused, it will be used to refresh the session. Otherwise, the user will be prompted to enter a TAN.
An asynchronous Task that returns an instance of the AuthClient class, representing the authenticated client.
This method has no parameters.
method
This method is responsible for executing the main logic of the ComdirectApiHostedService. It continuously retrieves account balances from the Comdirect API, logs the elapsed time, and waits for a specified delay before the next run.
An asynchronous Task that completes when the execution is stopped or an exception is thrown.
Name | Type | Description |
---|---|---|
stoppingToken | System.Threading.CancellationToken | A CancellationToken that indicates when the execution should be stopped. |
method
Retrieves and displays the account balances for all Comdirect banking accounts.
An asynchronous Task that completes when the account balances have been retrieved and displayed.
Name | Type | Description |
---|---|---|
client | Comdirect.Rest.Api.AuthClient | An instance of the AuthClient class for interacting with the Comdirect authentication API. |
method
Refreshes the TAN (Transaction Authentication Number) session using the saved values.
An asynchronous Task that completes when the TAN session refresh is completed. If the refresh token flow fails, an exception is thrown.
Name | Type | Description |
---|---|---|
authClient | Comdirect.Rest.Api.AuthClient | An instance of the AuthClient class for interacting with the Comdirect authentication API. |
method
Saves the session details for future use by updating the application’s settings.
"ComdirectSavedSession": { "SessionId": null, "RequestId": null, "RefreshToken": null,
"LastSessionDateTime": null, "ExpiresInSeconds": null }
Name | Type | Description |
---|---|---|
authClient | Comdirect.Rest.Api.AuthClient | An instance of the AuthClient class, which provides methods for interacting with the Comdirect |
authentication API. | ||
token | Comdirect.Rest.Api.ComdirectOAuthToken | The ComdirectOAuthToken object containing the access token obtained during the initial authentication flow. |
method
This method is responsible for starting the background service. It logs a message indicating that the service is starting, initializes the Comdirect API client, and then calls the base implementation of StartAsync.
An asynchronous Task that completes when the service has been started. If an exception occurs during the initialization of the Comdirect API client, the exception is logged and rethrown.
Name | Type | Description |
---|---|---|
cancellationToken | System.Threading.CancellationToken | A CancellationToken that indicates when the execution should be stopped. |
method
This method is responsible for stopping the background service. It logs a message indicating that the service is stopping and then calls the base implementation of StopAsync.
An asynchronous Task that completes when the service has been stopped.
Name | Type | Description |
---|---|---|
cancellationToken | System.Threading.CancellationToken | A CancellationToken that indicates when the execution should be stopped. |
type
ConsoleSample
method
Updates or adds a new app setting in the ‘appsettings.json’ file.
Name | Type | Description |
---|---|---|
sectionPathKey | System.String | The section path and key in the format “section1:section2:key”. |
This will navigate through the JSON object to find the correct section. | ||
value | ``0 | The new value to be set for the specified key. |
Name | Description |
---|---|
T | The type of the value to be set. |
method
Recursively sets a value in a JSON object based on a given section path and key.
Name | Type | Description |
---|---|---|
sectionPathKey | System.String | The section path and key in the format “section1:section2:key”. |
This will navigate through the JSON object to find the correct section. | ||
jsonObj | System.Object | The JSON object to be updated. |
value | ``0 | The new value to be set for the specified key. |
Name | Description |
---|---|
T | The type of the value to be set. |