Forum Xamarin.Forms

Announcement:

The Xamarin Forums have officially moved to the new Microsoft Q&A experience. Microsoft Q&A is the home for technical questions and answers at across all products at Microsoft now including Xamarin!

To create new threads and ask questions head over to Microsoft Q&A for .NET and get involved today.

AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or u

soufianemarlysoufianemarly Member ✭✭✭

Hello I'm using xamarin forms with office authentification
the authentification works fine but lately i receive this error message :
AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.

with some research i find that i need to refresh the token. -->

https://docs.microsoft.com/en-us/azure/active-directory/fundamentals/whats-new#change-notice-authorization-codes-will-no-longer-be-available-for-reuse
the question is how to refresh the token can anyone guide me.

        public async Task<AuthenticationResult> Authenticate(string authority, string resource, string clientId, string returnUri)
        {
            Console.WriteLine("In Authenticate");
            AuthenticationResult authResult = null;
            try
            {

                var authContext = new AuthenticationContext(authority);
                if (authContext.TokenCache.ReadItems().Any())
                authContext = new AuthenticationContext(authContext.TokenCache.ReadItems().First().Authority);
                var uri = new Uri(returnUri);

                PlatformParameters platformParams = null;
#if __ANDROID__

                platformParams = new PlatformParameters((Android.App.Activity)Forms.Context);
#endif
#if __IOS__
                Device.BeginInvokeOnMainThread(() =>
                {
                    UIViewController controller = new UIViewController();
                    controller = UIApplication.SharedApplication.KeyWindow.RootViewController;
                    platformParams = new PlatformParameters(controller);
                });
#endif

                UserDialogs.Instance.HideLoading();

                authResult = await authContext.AcquireTokenAsync(resource, clientId, uri, platformParams);
                Console.WriteLine("Need to be filed : " + authResult);

                authContext.TokenCache.Clear();

            }
            catch (Exception e)
            {
                Console.WriteLine("Execption : " + e.Message);
            }

            return authResult;
        }

Thank you.

Best Answer

Answers

Sign In or Register to comment.