You can use awesome library for this purposes AspNetTicketBridge.
Token handler definition:
public class OwinBearerTokenMachineKeyAuthenticationHandler : AuthenticationHandler<AuthenticationSchemeOptions>
{
public const string DefaultAuthScheme = "DefaultAuth";
// List of supported decryption algorithms: DES | 3DES | AES
private const string DefaultDecryptionAlgorithm = "<YOUR DECRYPTION ALGORIGHM>";
// List of supported validation algorithms: SHA1 | MD5 | 3DES | AES | HMACSHA256 | HMACSHA384 | HMACSHA512
private const string DefaultValidationAlgorithm = "<YOUR VALIDATION ALGORITHM>";
private const string DefaultAuthorizationHeader = "Authorization";
public OwinBearerTokenMachineKeyAuthenticationHandler(IOptionsMonitor<AuthenticationSchemeOptions> options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock)
{
}
protected override Task<AuthenticateResult> HandleAuthenticateAsync()
{
var token = Request.Headers[DefaultAuthorizationHeader][0].Remove(0, 7); // Bad code, don't use it... please
// Get keys from machine keys section / another configuration file.
var validationKey = "<YOUR VALIDATION KEY FROM MACHINE KEY CONFIG>";
var decryptionKey = "<YOUR DECRYPTION KEY FROM MACHINE KEY CONFIG>";
var ticket = MachineKeyTicketUnprotector.UnprotectOAuthToken(token, decryptionKey, validationKey, DefaultDecryptionAlgorithm, DefaultValidationAlgorithm);
var newTicket = AuthenticationTicketConverter.Convert(ticket, DefaultAuthScheme);
return Task.FromResult(AuthenticateResult.Success(newTicket));
}
}
App configuration:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// ...
app.UseAuthentication();
app.UseAuthorization(); // Gives ability to use [Authorize] attribute
// ...
}
public void ConfigureServices(IServiceCollection services)
{
// ...
RegisterAuthorization(services);
// ...
}
private void RegisterAuthorization(IServiceCollection services)
{
services.AddAuthentication(o => { o.DefaultScheme = OwinBearerTokenMachineKeyAuthenticationHandler.DefaultAuthScheme; })
.AddScheme<AuthenticationSchemeOptions, OwinBearerTokenMachineKeyAuthenticationHandler>(OwinBearerTokenMachineKeyAuthenticationHandler.DefaultAuthScheme, o => { });
services.AddAuthorization(); // Gives ability to use [Authorize] attribute
}
P.S. I spent 2 days for finding good solution to accomplish this task, but only this seems the best.