AADSTS165000: invalid Request: The request tokens do not match the user context,

AlexJK 6 Reputation points
2025-04-02T05:47:49.1466667+00:00

MAUI. Android app, use Oauth2D to login. Always get the error:

Requestld:50664542-03a0-48dc-b330-acf123cf9f00

Correlationld:27fdf9bd-1dd4-4b3b-a556-34f7c03c17af

Timestamp:2025-04-02T02:58:37Z

Message:AADSTS165000: invalid Request: The request tokens do not match the user

context, Do not copy the user context values (cookies, form fields, headers) betweendifferent requests or user sessions; always maintain the Al of the supplied values across acomplete single user flow. Failure Reasons: Token values do not match:

.NET MAUI
.NET MAUI
A Microsoft open-source framework for building native device applications spanning mobile, tablet, and desktop.
4,084 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Yonglun Liu (Shanghai Wicresoft Co,.Ltd.) 50,121 Reputation points Microsoft External Staff
    2025-04-03T05:54:42.9833333+00:00

    Hello,

    The application can run as expected when using pca certification in the following way.

    
    public class AuthService
    
        {
    
            private readonly IPublicClientApplication _pca;
    
            private readonly string[] _scopes = new[] { "User.Read" }; // Change as needed
    
            private IAccount _userAccount;
    
     
    
            public AuthService()
    
            {
    
    #if ANDROID
    
                _pca = PublicClientApplicationBuilder.Create("
    
                    .WithAuthority(AzureCloudInstance.AzurePublic, "
    
                    .WithRedirectUri("msauth://com.companyname.mauiapp1/") // Varies by platform
    
                    .WithParentActivityOrWindow(() => Platform.CurrentActivity) // Android platform specific line
    
                    .Build();
    
    #else
    
                _pca = PublicClientApplicationBuilder.Create("_clientId")
    
                    .WithAuthority(AzureCloudInstance.AzurePublic, "{_tenantId}")
    
                    .WithRedirectUri("msauth://com.companyname.mauiapp1/") // Varies by platform
    
                    .Build();
    
     
    
    #endif
    
     
    
            }
    
     
    
            public async Task<AuthenticationResult> LoginAsync()
    
            {
    
                try
    
                {
    
                    _userAccount = (await _pca.GetAccountsAsync()).FirstOrDefault();
    
                    return await _pca.AcquireTokenSilent(_scopes, _userAccount).ExecuteAsync();
    
                }
    
                catch (MsalUiRequiredException)
    
                {
    
                    return await _pca.AcquireTokenInteractive(_scopes).ExecuteAsync();
    
                }
    
            }
    
     
    
            public async Task LogoutAsync()
    
            {
    
                _userAccount = (await _pca.GetAccountsAsync()).FirstOrDefault();
    
                if (_userAccount != null)
    
                {
    
                    await _pca.RemoveAsync(_userAccount);
    
                }
    
            }
    
        }
    
    

    Best Regards,

    Alec Liu.


    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.


  2. Deleted

    This answer has been deleted due to a violation of our Code of Conduct. The answer was manually reported or identified through automated detection before action was taken. Please refer to our Code of Conduct for more information.


    Comments have been turned off. Learn more

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.