Created
June 27, 2025 08:38
-
-
Save oliverlabs/e0dad511467cb2762248a6782cd29414 to your computer and use it in GitHub Desktop.
Azure AI Foundry Auth using Entra ID in C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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