WS-federation with JWT in .net 7

42 Views Asked by At
builder.Services.AddAuthentication(sharedOptions =>
{
    sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    sharedOptions.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    sharedOptions.DefaultChallengeScheme = WsFederationDefaults.AuthenticationScheme;
})
.AddWsFederation(authenticationScheme: "WsFederation", options =>
{
    options.MetadataAddress = "http://localhost:21402/federationmetadata/2007-06/FederationMetadata.xml";
    options.Wtrealm = "https://localhost:7224/";
    options.RequireHttpsMetadata = false;
}).AddCookie();

I can get the user's id using:

[HttpGet("login"), Authorize(AuthenticationSchemes = "WsFederation")]
public IActionResult Login()
{
    var user = User as ClaimsPrincipal;
    var name = user.FindFirst(ClaimTypes.Name)?.Value;
    // Find user in database, generate token etc
    return Ok();
}

What I would like to do is authenticate the user with WS-federation, then get metadata like roles etc. from my database and create a JWT that can be used throughout my single page application configured with AddJwtBearer.

Do I have to use the AddWsFederation just for a single endpoint or can I simplify this?

0

There are 0 best solutions below