# Hmac using .NET 6.0

**URL:** <https://community.auth0.com/t/hmac-using-net-6-0/101917>\
**Category:** JWT.io\
**Created:** [March 6, 2023, 5:20pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917 "2023-03-06T17:20:04Z")\
**Posts on this page:** 15\
**Page:** 1

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 6, 2023, 5:20pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/1 "2023-03-06T17:20:04Z")

</div>

I am trying to duplicate this line from the jwt documentation in .NET 6.0 using C# hmac(`${encodedHeader}.${encodedPayload}`, secret, sha256); I cannot seem to find the correct library that allows me to produce this code.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 8, 2023, 3:55pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/3 "2023-03-08T15:55:02Z")

</div>

Hi @rguararra,

Welcome to the Auth0 Community!

Can you provide a link to the doc you are referencing?

---

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 8, 2023, 4:16pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/4 "2023-03-08T16:16:51Z")

</div>

> **[jwt-handbook-v0\_14\_1.pdf](https://assets.ctfassets.net/2ntc334xpx65/o5J4X472PQUI4ai6cAcqg/13a2611de03b2c8edbd09c3ca14ae86b/jwt-handbook-v0_14_1.pdf)**
>
> 1688.39 KB

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 8, 2023, 4:52pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/5 "2023-03-08T16:52:31Z")

</div>

It looks like it’s right here: [GitHub - auth0/jwt-handbook-samples: JWT Handbook code samples](https://github.com/auth0/jwt-handbook-samples)

---

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 8, 2023, 9:02pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/6 "2023-03-08T21:02:10Z")

</div>

Those examples are all JavaScript. I’m looking for C# .NET 6 solution for JWT. Everything that I found produces a Byte output and when I do convert it to a string format (Base64UrlEncoder), the JWT debugger states I have an incorrect signature.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 8, 2023, 9:57pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/7 "2023-03-08T21:57:01Z")

</div>

Let’s ignore the JS and JWT handbook stuff for now.

To clarify; _you are looking to encode and sign a JWT using .NET/C#_.

Here’s the most popular library and an example:

> **[GitHub - jwt-dotnet/jwt: Jwt.Net, a JWT (JSON Web Token) implementation for .NET](https://github.com/jwt-dotnet/jwt#creating-encoding-token)**
>
> Jwt.Net, a JWT (JSON Web Token) implementation for .NET - GitHub - jwt-dotnet/jwt: Jwt.Net, a JWT (JSON Web Token) implementation for .NET

If you have trouble, it’s helpful to see the code you are trying to use, and any relevant errors/logs.

---

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 11, 2023, 7:05pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/8 "2023-03-11T19:05:30Z")

</div>

When I paste my token into the jwt debugger, it claims I have an invalid signature. I’ve pasted my code below. What am I doing wrong? Please help.

```auto
IJwtAlgorithm algorithm = new JWT.Algorithms.HMACSHA256Algorithm();
            IJsonSerializer serializer = new JsonNetSerializer();
            IDateTimeProvider provider = new UtcDateTimeProvider();
            IJwtValidator validator = new JwtValidator(serializer, provider);
            IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
            IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);

            string unixTimestampNow = unixTime.ToUnixTime(DateTime.Now);
            DateTime dateTime = DateTime.Now;
            string unixTimestampPlusFive = unixTime.ToUnixTime(dateTime.AddHours(5));

            //string headerData = @"{ ""alg"":""HS256"", ""typ"":""JWT""}";
            string payloadData = @"{ ""iss"": " + apiInfo.APIKey + @", ""iat"": " + unixTimestampNow + @", ""exp"": " + unixTimestampPlusFive + @"}";

            var jsSerializer = new JavaScriptSerializer();
            //Dictionary<string, object> headerObj = (Dictionary<string, object>)jsSerializer.DeserializeObject(headerData);
            Dictionary<string, object> payloadObj = (Dictionary<string, object>)jsSerializer.DeserializeObject(payloadData);

            UTF8Encoding _utf8Encoding = new UTF8Encoding(encoderShouldEmitUTF8Identifier: false);
            byte[] signature = _utf8Encoding.GetBytes(apiInfo.APISecret);
            var token1 = encoder.Encode(payloadObj, signature);
            var token = encoder.Encode(payloadObj, apiInfo.APISecret);

```

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 13, 2023, 12:44pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/9 "2023-03-13T12:44:43Z")

</div>

Can you please provide an example token?

---

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 13, 2023, 1:59pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/10 "2023-03-13T13:59:52Z")

</div>

Here is the token I get when I run the software. You will notice that the Header and the Payload are correct, but the signature is not.

eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJXdEd5aVBrUVBhczRreTV0TWZHTFBrSUZnQUxzNVk0OCIsImlhdCI6MTY3ODcxNTY2MywiZXhwIjoxNjc4NzMzNjYzfQ.Z\_8w9bGdBCmC3lmzygEyUsALvpOfAEiZ9a7c30h9D74

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 13, 2023, 2:10pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/11 "2023-03-13T14:10:14Z")

</div>

That looks like a valid token, it may just be a formatting issue with your secret.

Have tried checking the “base64 encoded” box?

 ![Screenshot 2023-03-13 at 10.09.17 AM](https://global.discourse-cdn.com/auth0/original/3X/a/f/afe62005f7ecaff6e7c519926b8265a883a85b44.png)

---

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 13, 2023, 2:25pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/12 "2023-03-13T14:25:22Z")

</div>

If I first check the box as you suggest, and then paste the token in the encoded section, it still comes up with Invalid Signature.

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 13, 2023, 3:10pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/13 "2023-03-13T15:10:25Z")

</div>

Are you submitting the secret you used to encode the token?

---

<div class="post-metadata">

**Author:** ![rguararra](https://upload.wikimedia.org/wikipedia/commons/thumb/7/7c/User_font_awesome.svg/768px-User_font_awesome.svg.png) [@rguararra](https://community.auth0.com/u/rguararra)\
**Post date:** [March 13, 2023, 4:48pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/14 "2023-03-13T16:48:35Z")

</div>

That was what I was missing. I left the box unchecked, added the secret key, then added the token, and that sequence worked - signature verified. Thank you for all your time and help! It is greatly appreciated!

---

<div class="post-metadata">

**Author:** ![dan.woda](https://sea1.discourse-cdn.com/auth0/user_avatar/community.auth0.com/dan.woda/32/18340_2.png) [@dan.woda](https://community.auth0.com/u/dan.woda)\
**Post date:** [March 13, 2023, 6:01pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/15 "2023-03-13T18:01:00Z")

</div>

No problem!

Let me know if you have any questions about what is happening there.

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/auth0/original/3X/b/5/b560f266932fcce5d57f09a6595bd50518cae3f3.jpeg) [@system](https://community.auth0.com/u/system)\
**Post date:** [April 24, 2023, 1:49pm UTC](https://community.auth0.com/t/hmac-using-net-6-0/101917/16 "2023-04-24T13:49:51Z")

</div>

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.
