Class OAuth2ClientCredentialsHelper
- Namespace
- SESARWebHook.Core.Auth
Helper pour l'authentification OAuth 2.0 Client Credentials.
Utilisé pour les APIs qui supportent le flow "client credentials" :
- Microsoft Azure AD (SharePoint, Dynamics 365, Graph API)
- Google Cloud (avec service account)
- AWS Cognito
- Okta
FONCTIONNALITÉS :
- Obtention automatique du token
- Cache du token en mémoire
- Refresh automatique avant expiration
- Thread-safe
EXEMPLE D'UTILISATION :
var auth = new OAuth2ClientCredentialsHelper(
tokenEndpoint: "https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token",
clientId: "your-client-id",
clientSecret: "your-client-secret",
scope: "https://graph.microsoft.com/.default"
);
var token = await auth.GetAccessTokenAsync();
client.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
public class OAuth2ClientCredentialsHelper : IDisposable
- Inheritance
-
OAuth2ClientCredentialsHelper
- Implements
- Inherited Members
Constructors
OAuth2ClientCredentialsHelper(string, string, string, string, HttpClient)
Crée un helper OAuth2 Client Credentials.
public OAuth2ClientCredentialsHelper(string tokenEndpoint, string clientId, string clientSecret, string scope, HttpClient httpClient = null)
Parameters
tokenEndpointstringURL du endpoint token (ex: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token)
clientIdstringClient ID de l'application
clientSecretstringClient Secret de l'application
scopestringScope demandé (ex: https://graph.microsoft.com/.default)
httpClientHttpClientHttpClient optionnel (si null, un nouveau sera créé)
Methods
Dispose()
Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
public void Dispose()
ForAzureAD(string, string, string, string)
Crée un helper pour Azure AD / Microsoft Identity Platform.
public static OAuth2ClientCredentialsHelper ForAzureAD(string tenantId, string clientId, string clientSecret, string scope)
Parameters
tenantIdstringTenant ID Azure
clientIdstringClient ID de l'application
clientSecretstringClient Secret
scopestringScope (ex: https://graph.microsoft.com/.default)
Returns
ForDynamics365(string, string, string, string)
Crée un helper pour Dynamics 365.
public static OAuth2ClientCredentialsHelper ForDynamics365(string tenantId, string clientId, string clientSecret, string dynamicsUrl)
Parameters
tenantIdstringTenant ID Azure
clientIdstringClient ID
clientSecretstringClient Secret
dynamicsUrlstringURL Dynamics (ex: https://org.crm.dynamics.com)
Returns
ForSharePoint(string, string, string, string)
Crée un helper pour SharePoint Online.
public static OAuth2ClientCredentialsHelper ForSharePoint(string tenantId, string clientId, string clientSecret, string sharePointUrl)
Parameters
tenantIdstringTenant ID Azure
clientIdstringClient ID
clientSecretstringClient Secret
sharePointUrlstringURL du site SharePoint (ex: https://contoso.sharepoint.com)
Returns
GetAccessTokenAsync()
Obtient un access token valide. Le token est mis en cache et rafraîchi automatiquement si expiré.
public Task<string> GetAccessTokenAsync()
Returns
InvalidateToken()
Invalide le token en cache (force un refresh au prochain appel).
public void InvalidateToken()
RefreshTokenAsync()
Force le rafraîchissement du token.
public Task<string> RefreshTokenAsync()
Returns
TestConnectionAsync()
Teste si les credentials sont valides.
public Task<bool> TestConnectionAsync()