azure-docs icon indicating copy to clipboard operation
azure-docs copied to clipboard

Setup application insights authentication in asp.net framework

Open sabbadino opened this issue 2 years ago • 2 comments

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.

sabbadino avatar Oct 06 '22 17:10 sabbadino

@sabbadino Thanks for your feedback! We will investigate and update as appropriate.

ManoharLakkoju-MSFT avatar Oct 07 '22 03:10 ManoharLakkoju-MSFT

@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

AnuragSingh-MSFT avatar Oct 21 '22 10:10 AnuragSingh-MSFT

@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.

AnuragSingh-MSFT avatar Oct 26 '22 12:10 AnuragSingh-MSFT

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 avatar Oct 26 '22 12:10 sabbadino

@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.

AnuragSingh-MSFT avatar Oct 28 '22 12:10 AnuragSingh-MSFT

Yes, now it makes to me .. Pls update documentation accordingly... Thanks

sabbadino avatar Oct 28 '22 13:10 sabbadino