Blazor WASM .NET8 Authentication, Error displays after clicking the Logout button

Daren Almonina 40 Reputation points
2024-02-13T06:57:32.9566667+00:00

Hello, There was a weird error message that displays whenever I clicked the logout button.
User's image

after that it will proceed on the logout site. What should be the fix for that or what causes that error? this is the only code that I'm using for the logout button
Navigation.NavigateToLogout("authentication/logout"); Thank you

.NET
.NET
Microsoft Technologies based on the .NET software framework.
3,861 questions
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,580 questions
0 comments No comments
{count} votes

Accepted answer
  1. Zhi Lv - MSFT 32,336 Reputation points Microsoft Vendor
    2024-02-14T03:22:20.3133333+00:00

    Hi @Daren Almonina

    The document (aka.ms/msaljs/browser-errors) has made a detailed analysis of the problem and given corresponding solutions, you can check them:

    Error Message: Interaction is currently in progress. Please ensure that this interaction has been completed before calling an interactive API.

    This error is thrown when an interactive API (loginPopup, loginRedirect, acquireTokenPopup, acquireTokenRedirect) is invoked while another interactive API is still in progress. The login and acquireToken APIs are async so you will need to ensure that the resulting promises have resolved before invoking another one.

    Using loginPopup or acquireTokenPopup

    Ensure that the promise returned from these APIs has resolved before invoking another one.

    To resolve this you should ensure all interactive APIs have resolved before invoking another one:

    const request = { scopes: ["openid", "profile"] };
    await msalInstance.loginPopup();
    await msalInstance.acquireTokenPopup(request);
    

    Using loginRedirect or acquireTokenRedirect

    When using redirect APIs, handleRedirectPromise must be invoked when returning from the redirect. This ensures that the token response from the server is properly handled and temporary cache entries are cleaned up. This error is thrown when handleRedirectPromise has not had a chance to complete before the application invokes loginRedirect or acquireTokenRedirect.

    To resolve, you should wait for handleRedirectPromise to resolve before calling any interactive API:

    await msalInstance.handleRedirectPromise();
    
    const accounts = msalInstance.getAllAccounts();
    if (accounts.length === 0) {
        // No user signed in
        msalInstance.loginRedirect();
    }
    
    

    Or alternatively:

    msalInstance
        .handleRedirectPromise()
        .then((tokenResponse) => {
            if (!tokenResponse) {
                const accounts = msalInstance.getAllAccounts();
                if (accounts.length === 0) {
                    // No user signed in
                    msalInstance.loginRedirect();
                }
            } else {
                // Do something with the tokenResponse
            }
        })
        .catch((err) => {
            // Handle error
            console.error(err);
        });
    
    

    Note: If you are calling loginRedirect or acquireTokenRedirect from a page that is not your redirectUri you will need to ensure handleRedirectPromise is called and awaited on both the redirectUri page as well as the page that you initiated the redirect from. This is because the redirectUri page will initiate a redirect back to the page that originally invoked loginRedirect and that page will process the token response.

    Besides, after clicking the logout button, ensure that any authentication-related state (such as tokens or user information) is properly cleared to prevent any lingering issues. Refer to this issue.


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.
    Best regards,
    Dillion


0 additional answers

Sort by: Most helpful

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.