web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
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.

(0) ShareShare
ReportReport
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"; }

I have the same question (0)
  • Suggested answer
    Luis Figueiredo Profile Picture
    on at

    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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 74 Super User 2025 Season 2

#2
Daniyal Khaleel Profile Picture

Daniyal Khaleel 32 Most Valuable Professional

#3
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 31 Most Valuable Professional

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans