Skip to main content

Notifications

Announcements

No record found.

Customer experience | Sales, Customer Insights,...
Suggested answer

OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.

Posted on by 90

We have a web app connected with CRM Dynamics 365 that retrieve Accounts from crm through API. I can login successfully but when i try to retrieve accounts from crm i was not able to send the token and this error appears ( Microsoft.IdentityModel.Clients.ActiveDirectory.AdalServiceException: 'AADSTS54005: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.) 

Code: 

protected void Page_Load(object sender, EventArgs e)
{
var queryString = Request.QueryString;
if (queryString != null && queryString["code"] != null)
{
var authCode = queryString["code"];

// get the access token using the authorization code
TokenCache tokenCache = new TokenCache();
AuthenticationContext authContext = new AuthenticationContext(string.Format(AuthorityUri, _configuration.ADTenant), tokenCache);
ClientCredential clientCredentials = new ClientCredential(_configuration.ClientId, _configuration.ClientSecret);

AuthenticationResult authResult = authContext.AcquireTokenByAuthorizationCode(authCode, new Uri(_configuration.RedirectUri), clientCredentials);

if (authResult.UserInfo != null)
{
Session["AuthResult"] = authResult;
}}}

protected void Login_Click(object sender, EventArgs e)
{
var authorityUri = string.Format(CompleteAuthUri, _configuration.ADTenant, _configuration.ClientId, _configuration.Resource, Server.UrlEncode(_configuration.RedirectUri));

Response.Redirect(authorityUri);}

protected void GetAccounts_Click(object sender, EventArgs e)
{

if (Session["AuthResult"] != null)
{
var authResult = (AuthenticationResult)Session["AuthResult"];
var webRequest = (HttpWebRequest)WebRequest.Create(new Uri("OrganizationURL/api/data/v9.1/accounts?$select=accountid,name&$top=10"));
webRequest.Method = "GET";
webRequest.ContentLength = 0;
webRequest.Headers.Add("Authorization", String.Format("Bearer {0}", authResult.AccessToken));
webRequest.Headers.Add("OData - MaxVersion", "4.0");
webRequest.Headers.Add("OData - Version", "4.0");
webRequest.ContentType = "application / json; charset = utf - 8"; }

  • Suggested answer
    RE: OAuth2 Authorization code was already redeemed, please retry with a new valid code or use an existing refresh token.

    Good afternoon,

    Regarding your question, I would like to understand why you are using the ADAL method as this is an old authentication method and you can have here some information related to your issue.

    I suggest some changes in your method and start using one of those two:

    1.  If you use .net framework
      • I recommend you use the CrmserviceClient, and you can find more information here I hope this will help you to perform the implementation and in my opinion, is an easy way than the one you are trying to use now. And this is Nuget package that you should import in your SDK
    2. if you use .net core
      • If you are starting to develop this method I recommend you look into this documentation.

    I hope all this information will be helpful.

    Wish you a great day ahead and be safe.

    Best Regards.

    Thank you for using Microsoft Dynamics 365 Communities,

    https://community.dynamics.com/crm/b/dynamicscrmsupportblog

    Dynamics CRM Support Engineer - Customer Engagement

    Microsoft

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,280 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,214 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans