My Mobile API is currently using IdentityServer and for various reasons I need to replace it with something far simpler such as JWTs. I have a service with some very basic JWT issuing and validating etc based on the current standards etc however I'm having trouble plugging it into my current .NET 6 Web API.
My TokenService pseudo-code
public class TokenService : ITokenService
{
public string GetJwt();
public JwtSecurityToken DecodeJwt();
}
My Starup.cs file currently looks something like this which has been working nicely for years. This method is called within ConfigureServices:
private void ConfigureIdentityServer(IServiceCollection services)
{
services.AddAuthentication("Bearer")
// Add the IdentityServer access token validation handler into DI for use by the authentication services.
.AddIdentityServerAuthentication(options =>
{
options.Authority = Config.IdentityServerBaseUri;
options.RequireHttpsMetadata = false;
options.ApiName = Config.AppApiName;
options.ApiSecret = Config.ClientSecret;
});
}
And my AccountController is something like this:
[HttpGet]
[Authorize]
[CustomClaimFilter]
public IActionResult Get()
{
// Get the claims and return the user account details
}
What I want to be able to do with my new JWT stuff is essentially have the user authenticated with the API using either the IdentityServer OR the JWT based on a header supplied by the requesting client for example auth-method: jwt etc.
I'm guessing that there are two main changes I need to make in order to get this to work. Firstly something along the lines of AddJwtBearer within the ConfigureIdentityServer method shown above but also some code to somehow switch between the two based on the header.
I'm having difficulty figuring out the AddJwtBearer stuff and I've no idea where to start with the switching!
Any advice is appreciated, thanks.
You can add a switch with header in your request
Program.cs
Then in your [authorize] endpoint with token passed, add the header of JwtBearer/IdentityServer, it will authorize with your selected way.