User login and logout events

i have an application that is using microsoft azure ad for login.

I need to hook into the user login and logout events so that i can add some code to my application.
but i can not find a place to do this.

what i need to do will looks something like this:

public async Task OnLogInSucceeded()
    {
        var user = (await AuthenticationState).User;

        await AppInsights.SetAuthenticatedUserContext(user.FindFirst("preferred_username")?.Value);
    }

    public async Task OnLogOutSucceeded()
    {
        await AppInsights.ClearAuthenticatedUserContext();
    }

can anyone help me make that work ?
i see the code in security service.cs that has calls to the navigation manager but how to catch that the user has just logged in and just logged out.

You can use IsAuthenticated() method to check if the user is authenticated or not when needed.

that will not quite work in this case...
i have been looking at the code created by the radzen tool and i feel like i am close to an answer but still not there...

i am getting some of the picture and perhaps you can fill in some of the gaps in what i have so far?

i see that there is an "Account" controller , the "SecurityService" and an applicationauth state object (might not be giving the right name there)

what i need to do is when a user has just logged in and before they start doing actions to get the username to notify microsofts ApplicationInsights of the user name for tracking.
and when a user logs out i need that event to then notify Application Insights that the user has logged out.

i see that the account controller is how the azure ad auth is accessed.
i see the code in securty service .......

i have been trying to work out where and how to add my code to make things work.....

it seems like i need to possibly modify security service to have the points i need to hook into but i am not sure .....

I think i found what i was looking for and if you want to share this or possibly add this to your code generation here is what i found.

to start to get full application insights that identify the user and the pages they view you need to do several things.

this is with dot net core 7 right now and i do not know if they will change in v8

first in program cs one needs to hookup Application Insights

builder.Services.AddApplicationInsightsTelemetry();

now i found a package that helps activate some parts of what ai can do so i also added this from nuget:

then i had to modify
_host.chtml
adding the javascript for application insights
right now this is not ideal in how i am doing it, theconnection sting for ai is hard coded in my file.
need to find a better way to do that for getting the connection string from app config.

then in program.cs i also added this:

builder.Services.AddBlazorApplicationInsights(async applicationInsights =>
{
var telemetryItem = new TelemetryItem() {
Tags = new Dictionary<string, object>()
{
{ "ai.cloud.role", "SPA" },
{ "ai.cloud.roleInstance", "Blazor Server" },
}
};

await applicationInsights.AddTelemetryInitializer(telemetryItem);

});

and then to track the user login / logout i had to modify the security service.cs

setup to inject
private readonly IApplicationInsights AppInsights;

public SecurityService(NavigationManager navigationManager, IHttpClientFactory factory, IApplicationInsights Insights) {
this.httpClient = factory.CreateClient("RiaApp");
this.navigationManager = navigationManager;
this.AppInsights = Insights;

then...
in initalize

public bool Initialize(AuthenticationState result) {
Principal = result.User;

        var name = Principal.FindFirstValue(ClaimTypes.Name) ?? Principal.FindFirstValue("name");

        if (name != null) {

            var foo = Principal.FindFirst("preferred_username")?.Value;
            AppInsights.SetAuthenticatedUserContext(foo);


            User = new ApplicationUser { Name = name, preferred_username=foo };
        }

        return IsAuthenticated();
    }

and logout:

public void Logout() {
AppInsights.ClearAuthenticatedUserContext();
navigationManager.NavigateTo("Account/Logout", true);
}

all of this may not be the best way but it looks like it works right now.....

it might be worth seeing if RadZen Blazor Studio might get an added option to do something like this by letting the user select "Add Application Insights" and provide the ai key.