Compare commits
6 Commits
dd62af5ada
...
a77c70f655
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a77c70f655 | ||
|
|
031f830b8f | ||
|
|
5f9efa3bb0 | ||
|
|
d46dbbb877 | ||
|
|
e194cd8054 | ||
|
|
d21e0c06e7 |
@ -28,7 +28,19 @@ public class AuthClient : IAuthClient, IHostedService
|
||||
|
||||
_connection.On<string, string, string>(nameof(ReceivePublicKeyAsync), ReceivePublicKeyAsync);
|
||||
|
||||
_connection.Reconnected += async cnnId => await GetAllPublicKeysAsync();
|
||||
_connection.Reconnected += async cnnId =>
|
||||
{
|
||||
_logger?.LogInformation("Auth-client reconnected. Number of connection attempts {nOfAttempts}.", _nOfAttempts);
|
||||
await GetAllPublicKeysAsync();
|
||||
_nOfAttempts = 0;
|
||||
};
|
||||
|
||||
_connection.Reconnecting += (ex) =>
|
||||
{
|
||||
logger?.LogError(ex, "Auth-client disconnected. Attempt to reconnect every {time} seconds.", _params.RetryDelay!.Value.TotalSeconds);
|
||||
_nOfAttempts += 1;
|
||||
return Task.CompletedTask;
|
||||
};
|
||||
|
||||
_logger = logger;
|
||||
}
|
||||
@ -39,11 +51,43 @@ public class AuthClient : IAuthClient, IHostedService
|
||||
|
||||
public async Task StartAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
await _connection.StartAsync(cancellationToken);
|
||||
while(!await TryStartConnectionAsync(cancellationToken))
|
||||
{
|
||||
if (_params.RetryDelay is not null)
|
||||
await Task.Delay(_params.RetryDelay.Value.Milliseconds, cancellationToken);
|
||||
else
|
||||
return;
|
||||
}
|
||||
|
||||
IsConnected = true;
|
||||
await GetAllPublicKeysAsync();
|
||||
}
|
||||
|
||||
private int _nOfAttempts = 0;
|
||||
|
||||
private async Task<bool> TryStartConnectionAsync(CancellationToken cancellationToken = default)
|
||||
{
|
||||
try
|
||||
{
|
||||
_nOfAttempts += 1;
|
||||
await _connection.StartAsync(cancellationToken);
|
||||
_logger?.LogInformation("Auth-client connection successful. Number of connection attempts {nOfAttempts}.", _nOfAttempts);
|
||||
_nOfAttempts = 0;
|
||||
return true;
|
||||
}
|
||||
catch(HttpRequestException ex)
|
||||
{
|
||||
if(_nOfAttempts < 2)
|
||||
{
|
||||
if (_params.RetryDelay is null)
|
||||
_logger?.LogError(ex, "Auth-client connection failed. {message}", ex.Message);
|
||||
else
|
||||
_logger?.LogError(ex, "Auth-client connection failed and will be retried every {time} seconds. The status of being successful can be followed from the information logs.\n{message}", _params.RetryDelay.Value.TotalSeconds, ex.Message);
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public async Task StopAsync(CancellationToken cancellationToken)
|
||||
{
|
||||
await _connection.StopAsync(cancellationToken);
|
||||
|
||||
@ -7,39 +7,30 @@ public class ClientParams
|
||||
{
|
||||
public string Url { get; set; } = string.Empty;
|
||||
|
||||
private readonly Lazy<IRetryPolicy?> _lazyRetryPolicy;
|
||||
|
||||
/// <summary>
|
||||
/// Controls when the client attempts to reconnect and how many times it does so.
|
||||
/// </summary>
|
||||
public IRetryPolicy? RetryPolicy => _lazyRetryPolicy.Value;
|
||||
public IRetryPolicy? RetryPolicy { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// To simplify the assignment of <seealso cref="RetryPolicy"/>
|
||||
/// </summary>
|
||||
public Func<RetryContext, TimeSpan?>? NextRetryDelay { get; set; }
|
||||
private TimeSpan? _retryDelay;
|
||||
|
||||
/// <summary>
|
||||
/// To be able to serilize the simple <seealso cref="RetryPolicy"/>
|
||||
/// </summary>
|
||||
public TimeSpan? RetryDelay { get; set; }
|
||||
public TimeSpan? RetryDelay
|
||||
{
|
||||
get => _retryDelay;
|
||||
set
|
||||
{
|
||||
RetryPolicy = new RetryPolicy(ctx => RetryDelay);
|
||||
_retryDelay = value;
|
||||
}
|
||||
}
|
||||
|
||||
public event ClientEvent OnPublicKeyReceived = ClientEvents.UpdatePublicKeys;
|
||||
|
||||
internal void TriggerOnPublicReceivedEvent(AuthClient client, string issuer, string audience, string key, ILogger? logger = null)
|
||||
=> OnPublicKeyReceived(client, issuer, audience, key, logger);
|
||||
|
||||
public ClientParams()
|
||||
{
|
||||
_lazyRetryPolicy = new(() =>
|
||||
{
|
||||
if (RetryDelay is not null)
|
||||
return new RetryPolicy(ctx => RetryDelay);
|
||||
else if(NextRetryDelay is not null)
|
||||
return new RetryPolicy(NextRetryDelay);
|
||||
return null;
|
||||
});
|
||||
}
|
||||
|
||||
public List<ClientPublicKey> PublicKeys { get; set; } = new();
|
||||
}
|
||||
@ -5,7 +5,7 @@
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Nullable>enable</Nullable>
|
||||
<PackageId>DigitalData.Auth.Client</PackageId>
|
||||
<Version>1.2.1.1</Version>
|
||||
<Version>1.3.3</Version>
|
||||
<Description>DigitalData.Auth.Client is a SignalR-based authentication client that enables applications to connect to a central authentication hub for real-time message exchange. It provides seamless connection management, automatic reconnection (RetryPolicy), and event-driven communication (ClientEvents). The package includes dependency injection support via DIExtensions, allowing easy integration into ASP.NET Core applications. With built-in retry policies and secure message handling, it ensures a reliable and scalable authentication client for real-time authentication workflows.</Description>
|
||||
<Company>Digital Data GmbH</Company>
|
||||
<Product>Digital Data GmbH</Product>
|
||||
@ -14,8 +14,8 @@
|
||||
<PackageIcon>auth_icon.png</PackageIcon>
|
||||
<RepositoryUrl>http://git.dd:3000/AppStd/DigitalData.Auth</RepositoryUrl>
|
||||
<PackageTags>Digital Data Auth Authorization Authentication</PackageTags>
|
||||
<AssemblyVersion>1.2.1.1</AssemblyVersion>
|
||||
<FileVersion>1.2.1.1</FileVersion>
|
||||
<AssemblyVersion>1.3.3</AssemblyVersion>
|
||||
<FileVersion>1.3.3</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
@ -4,9 +4,9 @@
|
||||
<TargetFramework>net8.0</TargetFramework>
|
||||
<Nullable>enable</Nullable>
|
||||
<ImplicitUsings>enable</ImplicitUsings>
|
||||
<Version>1.0.0</Version>
|
||||
<AssemblyVersion>1.0.0</AssemblyVersion>
|
||||
<FileVersion>1.0.0</FileVersion>
|
||||
<Version>1.1.0</Version>
|
||||
<AssemblyVersion>1.1.0</AssemblyVersion>
|
||||
<FileVersion>1.1.0</FileVersion>
|
||||
</PropertyGroup>
|
||||
|
||||
<ItemGroup>
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user