Skip to content

Instantly share code, notes, and snippets.

@oliverlabs
Created June 27, 2025 08:38
Show Gist options
  • Select an option

  • Save oliverlabs/e0dad511467cb2762248a6782cd29414 to your computer and use it in GitHub Desktop.

Select an option

Save oliverlabs/e0dad511467cb2762248a6782cd29414 to your computer and use it in GitHub Desktop.
Azure AI Foundry Auth using Entra ID in C#
using Microsoft.Extensions.Configuration;
using Microsoft.SemanticKernel;
using Microsoft.SemanticKernel.ChatCompletion;
using Microsoft.SemanticKernel.Connectors.OpenAI;
using Azure.Identity;
string filePath = Path.GetFullPath("appsettings.json");
var config = new ConfigurationBuilder()
.AddJsonFile(filePath)
.Build();
// Set your values in appsettings.json
string endpoint = config["PROJECT_ENDPOINT"]!;
string deploymentName = config["DEPLOYMENT_NAME"]!;
// Create DefaultAzureCredential for managed identity authentication
var credential = new DefaultAzureCredential();
// Create a kernel with Azure OpenAI chat completion
var builder = Kernel.CreateBuilder();
builder.AddAzureOpenAIChatCompletion(deploymentName, endpoint, credential);
// app.settings.json template:
//{
// "PROJECT_ENDPOINT": "https://<project-name>.cognitiveservices.azure.com/",
// "DEPLOYMENT_NAME": "<name>"
//}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment