Table of Contents

Class OAuth2ClientCredentialsHelper

Espace de noms
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
Héritage
OAuth2ClientCredentialsHelper
Implements
Membres hérités

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)

Paramètres

tokenEndpoint string

URL du endpoint token (ex: https://login.microsoftonline.com/{tenant}/oauth2/v2.0/token)

clientId string

Client ID de l'application

clientSecret string

Client Secret de l'application

scope string

Scope demandé (ex: https://graph.microsoft.com/.default)

httpClient HttpClient

HttpClient 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)

Paramètres

tenantId string

Tenant ID Azure

clientId string

Client ID de l'application

clientSecret string

Client Secret

scope string

Scope (ex: https://graph.microsoft.com/.default)

Retour

OAuth2ClientCredentialsHelper

ForDynamics365(string, string, string, string)

Crée un helper pour Dynamics 365.

public static OAuth2ClientCredentialsHelper ForDynamics365(string tenantId, string clientId, string clientSecret, string dynamicsUrl)

Paramètres

tenantId string

Tenant ID Azure

clientId string

Client ID

clientSecret string

Client Secret

dynamicsUrl string

URL Dynamics (ex: https://org.crm.dynamics.com)

Retour

OAuth2ClientCredentialsHelper

ForSharePoint(string, string, string, string)

Crée un helper pour SharePoint Online.

public static OAuth2ClientCredentialsHelper ForSharePoint(string tenantId, string clientId, string clientSecret, string sharePointUrl)

Paramètres

tenantId string

Tenant ID Azure

clientId string

Client ID

clientSecret string

Client Secret

sharePointUrl string

URL du site SharePoint (ex: https://contoso.sharepoint.com)

Retour

OAuth2ClientCredentialsHelper

GetAccessTokenAsync()

Obtient un access token valide. Le token est mis en cache et rafraîchi automatiquement si expiré.

public Task<string> GetAccessTokenAsync()

Retour

Task<string>

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()

Retour

Task<string>

TestConnectionAsync()

Teste si les credentials sont valides.

public Task<bool> TestConnectionAsync()

Retour

Task<bool>