Skip to main content

Notifications

Announcements

No record found.

Customer Service forum

Authorization bearer token need to be generated as a sha256 hash of base64 encoded json payload and secret key

(0) ShareShare
ReportReport
Posted on by Microsoft Employee

Below my code where i'm generating bearer token key

bc2589931f6f7182d53d918fe7c29f387612179d74a45b17a872967433a6d77f  //wrong token
62A5B9C82E7C195CF65DDFB4FCE26576F51ACB2DB6AB1F496C424AC7365E0A8  //wrong tokencorrect token

why m i getting wrong token generated from below code?

public string ComputeSha256Hash(string rawData)
{

using (SHA256 sha256Hash = SHA256.Create())
{

byte[] bytes = sha256Hash.ComputeHash(Encoding.UTF8.GetBytes(rawData));
String sty= Convert.ToBase64String(bytes);

//Convert byte array to a string
StringBuilder builder = new StringBuilder();
for (int i = 0; i < bytes.Length; i++)
{
builder.Append(bytes[i].ToString("x2"));
}

Console.WriteLine(builder.ToString());
Console.ReadKey();

return builder.toString();
}
}

Categories:
  • Suggested answer
    RaviKashyap Profile Picture
    RaviKashyap 55,410 on at
    RE: Authorization bearer token need to be generated as a sha256 hash of base64 encoded json payload and secret key

    Hi,

    The token requirement varies with the client. I worked on an integration with WEB API where the token was generated in the following format (key + secret + unixtimestamp)

    ==============

    var apiKey = "<keyvalue>";

               var apiSecret = "<secret>";

               var unixTimestamp = (Int32)(DateTime.UtcNow.Subtract(new DateTime(1970, 1, 1))).TotalSeconds;

               System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

               byte[] sinByte = encoding.GetBytes(apiKey + apiSecret + unixTimestamp);

               HMACSHA256 hmacsha256 = new HMACSHA256();

               hmacsha256.ComputeHash(sinByte);

               byte[] hashmessage = hmacsha256.ComputeHash(sinByte);

               var signature = ByteToString(hashmessage);

    ==========

    Hope this helps.

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

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

#2
Martin Dráb Profile Picture

Martin Dráb 230,503 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans