How can i log an azure ad user out from my blazor server website?

Mao Uyen Tram 126 Reputation points
2024-01-16T10:49:40.97+00:00

Hello, I'm building a website for internal use that uses microsoft entra id to log in and out. I'd like to log users out when they quit the page. I've looked at CircuitHandler as it has events that occur when a user closes the page, but I'm confused how to log a user out from there. Does anyone know how i can log an entra id user out from there programmatically?

Blazor
Blazor
A free and open-source web framework that enables developers to create web apps using C# and HTML being developed by Microsoft.
1,664 questions
Microsoft Entra ID
Microsoft Entra ID
A Microsoft Entra identity service that provides identity management and access control capabilities. Replaces Azure Active Directory.
23,258 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Ken Kam Hung, Lin 91 Reputation points
    2024-01-16T11:05:24.35+00:00

    Here is an example of how to write a log entry to a file using C# in server-side:

    
    //Code sample
    
    public class Logout : ComponentBase
    {
        [Inject]
        private ILogger<Logout> logger { get; set; }
    
        protected override void OnInitialized()
        {
            base.OnInitialized();
            JSRuntime.InvokeVoidAsync("registerUnloadEvent", DotNetObjectReference.Create(this));
        }
    
        [JSInvokable]
        public void LogOut()
        {
            string loginId = HttpContext.Session.GetString("LoginId");
            string logMessage = "Login id logs out " + loginId + " at " + DateTime.Now.ToString();
            logger.LogInformation(logMessage);
        }
    }
    
    

    his code writes a log entry to the server’s log using the ILogger interface. The log entry contains the login ID of the user and the timestamp of when they logged out.

    To trigger this method when the user closes the browser tab, you can use the onbeforeunload event in JavaScript. This event is triggered when the user navigates away from the page, closes the browser, or closes the tab. You can use this event to make an AJAX call to the server and invoke the LogOut() method.
    Please note that this is just one way to achieve this functionality. There are other ways to write a log entry to a file or database, depending on your specific requirements and architecture.


  2. Bruce (SqlWork.com) 71,506 Reputation points
    2024-01-16T16:53:20.5633333+00:00

    it depend on your requirements. To officially logout you need to delete the local login cookie, and to logout of azure, you need to redirect to the azure logout url. while you can detect lost connection on the server, you can not log the user out. you can save in persistent state that the user needs to logout, and on the next access force a logout. client side you can use the onbeforeunload event, but browsers are getting more restrictive in what is allowed in the event. Ideally you would want to redirect to the logout page, but this is not allowed. if popups are not blocked, you might be able to open a new window that logs out.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.