Created
May 22, 2022 11:04
-
-
Save Toumash/3936dd8ae255ac4cf6ae97e8e9de993b to your computer and use it in GitHub Desktop.
JWT shortcut .NET 6
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.IdentityModel.Tokens; | |
using System.IdentityModel.Tokens.Jwt; | |
using System.Security.Claims; | |
using System.Text; | |
var plainTextSecurityKey = "This is secret"; | |
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(plainTextSecurityKey)); | |
var signingCredentials = new SigningCredentials(signingKey, SecurityAlgorithms.HmacSha256Signature); | |
var claimsIdentity = | |
new ClaimsIdentity(new List<Claim>() | |
{ | |
new Claim(JwtRegisteredClaimNames.Name, "username1") | |
}); | |
var securityTokenDescriptor = new SecurityTokenDescriptor() | |
{ | |
Subject = claimsIdentity, | |
SigningCredentials = signingCredentials | |
}; | |
var tokenHandler = new JwtSecurityTokenHandler(); | |
var plainToken = tokenHandler.CreateToken(securityTokenDescriptor); | |
var signedAndEncodedToken = tokenHandler.WriteToken(plainToken); | |
Console.WriteLine(plainToken); | |
Console.WriteLine(signedAndEncodedToken); | |
HttpClient client = new HttpClient(); | |
client.DefaultRequestHeaders.Add("Authorization", $"Bearer {signedAndEncodedToken}"); | |
var res = await client.GetAsync("http://localhost:5242/api/Example"); | |
if (res.StatusCode == System.Net.HttpStatusCode.OK) | |
{ | |
var result = await res.Content.ReadAsStringAsync(); | |
Console.WriteLine(result); | |
} | |
else | |
{ | |
Console.WriteLine($"Status: {res.StatusCode}"); | |
} | |
Console.ReadLine(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Authorize(AuthenticationSchemes = JwtBearerDefaults.AuthenticationScheme)] | |
[ApiController] | |
[Route("api/[controller]")] | |
public class ExampleController : ControllerBase | |
{ | |
public ExampleController() | |
{ | |
} | |
[HttpGet] | |
public IActionResult Get() | |
{ | |
return this.Ok("webapi service. authenticated as " + this.User.Identity.Name); | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using Microsoft.AspNetCore.Authentication.JwtBearer; | |
using Microsoft.IdentityModel.Tokens; | |
using System.Text; | |
var builder = WebApplication.CreateBuilder(args); | |
builder.Services | |
.AddAuthentication() | |
.AddJwtBearer(cfg => | |
{ | |
var plainTextSecurityKey = "This is secret"; | |
var signingKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(plainTextSecurityKey)); | |
var signingCredentials = new SigningCredentials(signingKey,SecurityAlgorithms.HmacSha256Signature); | |
cfg.RequireHttpsMetadata = false; | |
cfg.TokenValidationParameters = new TokenValidationParameters() | |
{ | |
ValidateAudience = false, | |
ValidateIssuer = false, | |
IssuerSigningKey = signingKey | |
}; | |
cfg.Events = new JwtBearerEvents() | |
{ | |
OnAuthenticationFailed = async context => | |
{ | |
var ex = context.Exception; | |
Console.WriteLine(ex.Message); | |
} | |
}; | |
}); | |
builder.Services.AddControllers(); | |
var app = builder.Build(); | |
app.UseAuthentication(); | |
app.UseAuthorization(); | |
app.MapControllers(); | |
app.Run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment