azure-docs
azure-docs copied to clipboard
Setup application insights authentication in asp.net framework
in my current code I do TelemetryConfiguration.Active.ConnectionString = ...
And below is the code to ask for ad authentication found at https://learn.microsoft.com/en-us/azure/azure-monitor/app/azure-ad-authentication?tabs=net
var config = new TelemetryConfiguration { ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/" } var credential = new DefaultAzureCredential(); config.SetAzureTokenCredential(credential);
It's not clear to me how to mix the code to setup ad authentication for ai with the way i am using to provide cn string Thanks
Document Details
⚠ Do not edit this section. It is required for learn.microsoft.com ➟ GitHub issue linking.
- ID: 20c3df07-5950-bd23-4bfc-47fe636a5f44
- Version Independent ID: c12e72fa-3996-0246-a5cd-e2f9856e04a3
- Content: Azure AD authentication for Application Insights - Azure Monitor
- Content Source: articles/azure-monitor/app/azure-ad-authentication.md
- Service: azure-monitor
- Sub-service: application-insights
- GitHub Login: @AaronMaxwell
- Microsoft Alias: aaronmax
@sabbadino Thanks for your feedback! We will investigate and update as appropriate.
@sabbadino, apologies for the delayed response. If I understand correctly, you are trying to use AD authentication along with connection string to submit telemetry to Application Insights.
By default, the Connection string (which contains the InstrumentationKey and injestion endpoint) is only required for submitting data to Application Insights. This is true when Local authentication is enabled for the Application Insights resource. Local authentication only requires Instrumentation Key for data ingestion.
After you have updated the telemetry configuration as mentioned below, along with the connectionstring (which has the instrumentationKey) the AD auth token is also required. Therefore, it has been recommended to follow this step before disabling the local authentication to avoid any gaps in data. Configuring and enabling Azure AD based authentication
Hope this helps. In case it does not help answer your question, please share additional details/context about the question to help me understand it better. Thank you
@sabbadino, We will now proceed to close this thread as we have not heard back from you. If there are further questions regarding this matter, please tag me in your reply and reopen the issue. We will gladly continue the discussion.
Sorry I did not reply you back soon . Its not clear how to apply the code mentioned in that article for asp.net not core apps. In such apps the ai cn string is in the appinsights file .. one does not need to write any code to provide ai cn string .. In other docs is shown how to setup ai cn string read from app settings
TelemetryConfiguration.Active.ConnectionString = ...
It's not clear at all how one would wire the code above with
var config = new TelemetryConfiguration { ConnectionString = "InstrumentationKey=00000000-0000-0000-0000-000000000000;IngestionEndpoint=https://xxxx.applicationinsights.azure.com/" } var credential = new DefaultAzureCredential(); config.SetAzureTokenCredential(credential);
Where should I place the piece of code above ? Global.asax app start ? What should I do with that var config variable ?
I think you should provide a full project sample on GitHub for an aspnet not core app.
Enrico
@sabbadino, thank you for the clarification.
Yes, you could do this in Application_Start()
method of Global.asax file. The following sample snippet should help understand it better.
protected void Application_Start()
{
...
...
var credential = new DefaultAzureCredential();
TelemetryConfiguration.Active.SetAzureTokenCredential(credential);
}
The TelemetryConfiguration.Active.SetAzureTokenCredential()
method is used here to update the credential on the active ApplicationInsights configuration - read from config file.
Hope this helps. Please let me know if you have any questions. I will also share this feedback with our team to have the example/code snippet in doc clearer.
Yes, now it makes to me .. Pls update documentation accordingly... Thanks