Skip to content

Commit 9cf485e

Browse files
committed
Added sample code for App Insights Entra Auth
1 parent 197a5c2 commit 9cf485e

9 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
<Solution>
2+
<Project Path="src/SampleApp/SampleApp.csproj" />
3+
</Solution>

AppInsightsEntraAuth/README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# Application Insights with Entra ID authentication
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
targetScope = 'subscription'
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
targetScope = 'resourceGroup'
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
namespace codingfreaks.samples.AppInsightsEntraAuth.Controllers
2+
{
3+
using Microsoft.AspNetCore.Mvc;
4+
5+
[ApiController]
6+
[Route("[controller]")]
7+
public class TestController : ControllerBase
8+
{
9+
#region constructors and destructors
10+
11+
public TestController(ILogger<TestController> logger)
12+
{
13+
Logger = logger;
14+
}
15+
16+
#endregion
17+
18+
#region methods
19+
20+
[HttpGet]
21+
public IActionResult Get()
22+
{
23+
Logger.LogInformation("Get was called.");
24+
var result = new
25+
{
26+
Timestamp = DateTimeOffset.Now,
27+
Message = "Hello",
28+
AppInsights = new
29+
{
30+
ConnectionString =
31+
Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_CONNECTION_STRING") ?? "-",
32+
AuthenticationString =
33+
Environment.GetEnvironmentVariable("APPLICATIONINSIGHTS_AUTHENTICATION_STRING") ?? "-"
34+
}
35+
};
36+
37+
return Ok(result);
38+
}
39+
40+
#endregion
41+
42+
#region properties
43+
44+
private ILogger<TestController> Logger { get; }
45+
46+
#endregion
47+
}
48+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
using Azure.Identity;
2+
using Azure.Monitor.OpenTelemetry.AspNetCore;
3+
4+
var builder = WebApplication.CreateBuilder(args);
5+
builder.Services.AddControllers();
6+
builder.Services.AddOpenApi();
7+
builder.Services.AddOpenTelemetry()
8+
.UseAzureMonitor(options =>
9+
{
10+
if (builder.Environment.IsProduction())
11+
{
12+
options.Credential = new ManagedIdentityCredential(new ManagedIdentityCredentialOptions());
13+
}
14+
else
15+
{
16+
// DON'T DO THIS BECAUSE IT ONLY WORKS IN VS!!!
17+
//options.Credential = new DefaultAzureCredential();
18+
}
19+
});
20+
var app = builder.Build();
21+
if (app.Environment.IsDevelopment())
22+
{
23+
app.MapOpenApi();
24+
}
25+
app.UseHttpsRedirection();
26+
app.UseAuthorization();
27+
app.MapControllers();
28+
app.Run();
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"$schema": "https://json.schemastore.org/launchsettings.json",
3+
"profiles": {
4+
"https": {
5+
"commandName": "Project",
6+
"dotnetRunMessages": true,
7+
"launchBrowser": false,
8+
"applicationUrl": "https://localhost:7028",
9+
"environmentVariables": {
10+
"ASPNETCORE_ENVIRONMENT": "Development",
11+
"APPLICATIONINSIGHTS_CONNECTION_STRING": "InstrumentationKey=98074d8a-276c-4cdc-9a4b-24540b763a86;IngestionEndpoint=https://westeurope-5.in.applicationinsights.azure.com/;LiveEndpoint=https://westeurope.livediagnostics.monitor.azure.com/;ApplicationId=6b63afe9-6105-4149-83fd-8c3198962d70",
12+
"APPLICATIONINSIGHTS_AUTHENTICATION_STRING": "Authorization=AAD"
13+
}
14+
}
15+
}
16+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<Project Sdk="Microsoft.NET.Sdk.Web">
2+
<PropertyGroup>
3+
<RootNamespace>codingfreaks.samples.AppInsightsEntraAuth</RootNamespace>
4+
<AssemblyName>AppInsightsEntraAuth</AssemblyName>
5+
<TargetFramework>net10.0</TargetFramework>
6+
<Nullable>enable</Nullable>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
</PropertyGroup>
9+
<ItemGroup>
10+
<PackageReference Include="Azure.Identity" Version="1.21.0" />
11+
<PackageReference Include="Azure.Monitor.OpenTelemetry.AspNetCore" Version="1.4.0" />
12+
<PackageReference Include="Microsoft.AspNetCore.OpenApi" Version="10.0.7" />
13+
</ItemGroup>
14+
</Project>
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"Logging": {
3+
"LogLevel": {
4+
"Default": "Information",
5+
"Microsoft.AspNetCore": "Warning"
6+
}
7+
},
8+
"AllowedHosts": "*"
9+
}

0 commit comments

Comments
 (0)