Extensions for jwt(https://github.com/jwt-dotnet/jwt) and asp.net core 2.0
It's easy for you to use Jwt when you're using asp.net core 2.0.
For example, if I want to create a token without this extension in asp.net core 2.0, I should write the code below:
var payload = new Dictionary<string, object>
{
{ "claim1", 0 },
{ "claim2", "claim2-value" }
};
const string secret = "GQDstcKsx0NHjPOuXOYg5MbeJ1XT0uFiwDVvVBrk";
IJwtAlgorithm algorithm = new HMACSHA256Algorithm();
IJsonSerializer serializer = new JsonNetSerializer();
IBase64UrlEncoder urlEncoder = new JwtBase64UrlEncoder();
IJwtEncoder encoder = new JwtEncoder(algorithm, serializer, urlEncoder);
var token = encoder.Encode(payload, secret);
Console.WriteLine(token);
as you can see, I should new
a few of instance to create the token I want.
After using this extension,I can use the DI system to get the instance of IJwtEncoder
interface, then I just need to write a few code below:
var payload = new Dictionary<string, object>
{
{ "claim1", 0 },
{ "claim2", "claim2-value" }
};
var token = _jwtEncoder.Encode(payload, secret);
Using the commond below in the Package Manager
Install-Package Jwt.Extensions
If you just need to use the IJwtEncoder
or IJwtDecoder
easily, you could modify your Startup
class and add services.AddJwt();
to the ConfigureServices
method.
This will use the default configuration listed below:
HMACSHA256Algorithm
asIJwtAlgorithm
UtcDateTimeProvider
asIDateTimeProvider
JsonNetSerializer
asIJsonSerializer
JwtBase64UrlEncoder
asIBase64UrlEncoder
You can write the code like blow to use the IJwtEncoder
or IJwtDecoder
:
public class MyClass
{
private readonly IJwtEncoder _jwtEncoder;
private readonly IJwtDecoder _jwtDecoder;
public MyClass(IJwtEncoder jwtEncoder, IJwtDecoder jwtDecoder)
{
_jwtEncoder = jwtEncoder;
_jwtDecoder = jwtDecoder;
}
}
then you can use them in you class.
These methods use the Decode
methods within its implement and the verify flag is always true
,they're listed below:
bool TryDecode(string token, string key, out string result)
bool TryDecode(string token, byte[] key, out string result)
bool TryDecodeToObject(string token, string key, out IDictionary<string, object> result)
bool TryDecodeToObject(string token, byte[] key, out IDictionary<string, object> result)
bool TryDecodeToObject<T>(string token, string key, out T result)
bool TryDecodeToObject<T>(string token, byte[] key, out T result)
Defining a Payload class which includes the basic properties such as sub
, iss
, aud
and exp
.
The JwtControllerBase
class is inherited from Controller
class and override the OnActionExecuting
method to verify the token.
To use this future, please follow the steps below:
Modifying the Startup
class and adding the folowing code into the ConfigureServices
class:
services.AddJwt(opt =>
{
opt.Bearer = TokenBearer.QueryString;
opt.TokenBearerKey = "SomeKey";
opt.SecretStr = "secret";
opt.RedirectAction = "Login";
opt.RedirectController = "User";
});
Bearer
means where is the location to bearer the tokenTokenBearerKey
means which key is the bearer key, the default key is "Token"SecretStr
means the jwt secret, you can also use theSecretBytes
to provide abyte[]
type keyRedirectAction
means which action you want to redirect after the token is illegalRedirectController
means which controller you want to redirect after the token is illegal
Something Important: If you provide both SecretStr
and SecretBytes
the SecretStr
would be used by default, if you don't provide SecretStr
nor SecretBytes
, there would be a NoSecretSpecifiedException
exception be thrown.
Using the following code to define a controller:
public class AdminController : JwtControllerBase
Setting the JwtCheck
attribute to the controller or action which needs to be authorized, for example
[JwtCheck]
public class AdminController : JwtControllerBase
or
public class AdminController : JwtControllerBase
{
[JwtCheck]
public ActionResult Index()
{
return View();
}
}
If some methods don't need to be authorize, you can use like this:
[JwtCheck]
public class AdminController : JwtControllerBase
{
public ActionResult Index()
{
return View();
}
[JwtCheck(Ignore = true)]
public ActionResult Login()
{
return View();
}
}