Skip to content

Conversation

@joseharriaga
Copy link
Collaborator

System.ClientModel's new AuthenticationPolicy makes it possible to use custom authentication mechanisms. For example, using Azure.Identity's DefaultAzureCredential, we can define a BearerTokenPolicy and use it to communicate with Azure OpenAI like this:

DefaultAzureCredential credential = new();
string scope = "https://cognitiveservices.azure.com/.default";
BearerTokenPolicy bearerTokenPolicy = new(credential, scope);

OpenAIClientOptions clientOptions = new()
{
    Endpoint = new Uri(Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT"))
};

ChatClient client = new(
    model: "gpt-4.1-mini",
    authenticationPolicy: bearerTokenPolicy,
    options: clientOptions);

ChatCompletion completion = client.CompleteChat("Say 'this is a test.'");
Console.WriteLine($"[ASSISTANT]: {completion.Content[0].Text}");

@KrzysztofCwalina
Copy link
Collaborator

why are we doing this only in ChatCLient?

@joseharriaga
Copy link
Collaborator Author

joseharriaga commented Jul 28, 2025

why are we doing this only in ChatCLient?

@KrzysztofCwalina I wanted to start small. If this looks good, I will add it to everything else.

@joseharriaga joseharriaga merged commit fa73161 into main Jul 31, 2025
1 check passed
@joseharriaga joseharriaga deleted the joseharriaga/AuthenticationPolicy branch July 31, 2025 06:57
joseharriaga added a commit that referenced this pull request Jul 31, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants