none
Inside Application_PostAcquireRequestState method, Context.GetRequestTelemetry() method always returns null value RRS feed

  • Question

  • Integrating AppInsights to ASP.NET MVC application. Want to send authenticated user id information to the app insights.

    Followed the steps mentioned in the below link.

    https://docs.microsoft.com/en-us/azure/azure-monitor/app/usage-send-user-context

    protected void Application_PostAcquireRequestState() { var requestTelemetry = Context.GetRequestTelemetry(); if (HttpContext.Current.Session != null && requestTelemetry != null && string.IsNullOrEmpty(requestTelemetry.Context.User.Id)) { requestTelemetry.Context.User.Id = Session.SessionID; } }

    Context.GetRequestTelemetry() method in the above method always returns null.

    Any suggestions ?


    Wednesday, October 16, 2019 3:23 PM

All replies

  • Hello, Mahendra!

    By default, web apps identify users by using cookies.  A new ASP.NET MVC application will give you something like this when you run it:

     

    That long line of text will include something like the following:

    ai.user.id":"abc4e"

     

    For authenticated users, you'll need to follow our documentation here:

    https://docs.microsoft.com/en-us/azure/azure-monitor/app/api-custom-events-metrics#authenticated-users

     

    Which will include code that may look like this:

    @if (Request.IsAuthenticated)
    {
        <script>
            appInsights.setAuthenticatedUserContext("@User.Identity.Name
                .Replace("\\", "\\\\")"
                .replace(/[,;=| ]+/g, "_"));
        </script>
    }

    Don't forget that in your project in Visual Studio, you have "Connected Services" that will allow you to configure authentication with Azure Active Directory if you haven't already done so.

    Wednesday, October 23, 2019 5:28 AM
    Moderator