Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Encoding JWT Token Using System.IdentityModel.Tokens.Jwt in ASP.NET 5

I have a use case where I want my new application (which is the source of authentication) to provide a JWT to legacy applications.

I am attempting to use the package, System.IdentityModel.Tokens.Jwt 5.0.0-beta7 (and I could easily move to beta8 if necessary). Creating the token was simple, but I am having issues properly signing it. Here is my current code:

Claim[] claims = {
    new Claim(ClaimTypes.Email, "[email protected]"),
    new Claim(ClaimTypes.Role, "admin"),
    new Claim(ClaimTypes.Uri, ""),
    new Claim(ClaimTypes.Expiration, DateTime.UtcNow.Add(TimeSpan.FromDays(1)).ToString()),
    new Claim("personId", personId.ToString())
};

var key = Convert.FromBase64String("my super secret key goes here");
var signingCredentials = new SigningCredentials(
    new SymmetricSecurityKey(key),
    SecurityAlgorithms.HMAC_SHA256,
    SecurityAlgorithms.Sha256Digest
);
var jwt = new JwtSecurityToken("localhost", "all", claims, 
    DateTime.UtcNow, DateTime.UtcNow.AddDays(1), signingCredentials);            

var handler = new JwtSecurityTokenHandler();
handler.WriteToken(jwt);

return Content($"{handler.WriteToken(jwt)}");

If I do not sign the token, everything works correctly. However, as soon as I add signing credentials, I receive an error that HMAC is not supported. I did find another SO post that says support for symmetric keys does not yet exist. However, in my search, I see extensive use of the library. In particular, I see the use of InMemorySymmetricSecurityKey. However, when I try to use it myself, it can't be found in any namespace, so I am a bit confused as where I go from here.

This is a long-winded explanation to basically ask - how do I properly sign the JWT with a simple secret?

like image 669
JasCav Avatar asked Nov 05 '15 17:11

JasCav


People also ask

How to create and validate JWT token in ASP core?

ASP.NET Core 3.1 - Create and Validate JWT Tokens + Use Custom JWT Middleware 1 Installing the JWT Token Library via NuGet. ... 2 Creating a JWT Token in ASP.NET Core. ... 3 Validating a JWT Token in ASP.NET Core. ... 4 Validating Tokens in Custom JWT Middleware. ... 5 Checking Token Validated with Custom Authorize Attribute. ...

What parts of the JWT token system are used in code samples?

The code samples use the jwt token handler and a few related classes to create and validate JWT tokens, no other parts of the ASP.NET Core Identity system are used. .NET Core CLI: dotnet add package System.IdentityModel.Tokens.Jwt

How to configure JWT (JSON Web Tokens) in Visual Studio?

To configure the JWT (JSON web tokens) we must have the Nuget package installed inside the project, so let's first add the project dependencies. Inside the Visual Studio - Click on Tools -> Nuget Package Manager -> Manage Nuget packages for solution. Install-Package Microsoft.AspNetCore.Authentication.JwtBearer -Version 5.0.7

What is JWT in ASP NET?

Let us test the Get API. JWT is very famous in web development. It is an open standard that allows transmitting data between parties as a JSON object in a secure and compact way. In this article, we learned how to create and Validate JWT with ASP.NET core application.


1 Answers

When we moved to the new versions of CoreClr, we had to temporarily drop support for HMAC. We choose to add support for ECDSA in RC1 and plan on adding HMAC back in RC2.

Sorry for the hassle. You could add your own SignatureProvider OR wait a couple of weeks.

like image 188
Brent Schmaltz Avatar answered Sep 29 '22 20:09

Brent Schmaltz