I've created an ASP.NET Core 3.1 application that uses 2 authentication types - cookie and JWT bearer.
I've setup a scheme that redirects users to the proper scheme based on the path requested:
.AddAuthentication(sharedOptions =>
{
sharedOptions.DefaultScheme = "smart";
sharedOptions.DefaultChallengeScheme = "smart";
})
.AddPolicyScheme("smart", "Bearer Authorization or Cookie", options =>
{
options.ForwardDefaultSelector = context =>
{
var requestPath = context.Request.Path;
if (CookiePolicyPathRegex.IsMatch(requestPath))
{
return CookieAuthenticationDefaults.AuthenticationScheme;
}
return JwtBearerDefaults.AuthenticationScheme;
};
})
.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOAuthServiceScheme(Configuration); // Custom handler for JWT
I setup the authorization policies like so:
options.AddPolicy(ApiPolicies.CookiePolicy, policy =>
{
// policy.AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
policy.RequireRole(Roles.Access);
});
options.AddPolicy(ApiPolicies.JwtPolicy, policy =>
{
policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme);
policy.RequireAuthenticatedUser();
});
This works fine, the proper policies are being triggered, but I have one problem. In my integration tests I use a middleware that adds the ClaimsIdentity for the cookie authentication:
public async Task Invoke(HttpContext context)
{
// Removed for brevity
var claimsIdentity = new ClaimsIdentity(claims, CookieAuthenticationDefaults.AuthenticationScheme);
context.User = new ClaimsPrincipal(claimsIdentity);
await _next(context);
}
The middleware is setup to run before the Auth middlewares
ConfigureAdditionalMiddleware(app);
app.UseAuthentication();
app.UseAuthorization();
If I uncomment the // policy.AddAuthenticationSchemes(CookieAuthenticationDefaults.AuthenticationScheme); part from the cookie policy, the the authorization part doesn't see the Identity created in the middleware. If I leave it commented, the Identity is there, with claims, authentication type and everything. If I look in the PolicyScheme that forwards to the two auth schemes, the Identity is there.
My question is, why does adding CookieAuthenticationDefaults.AuthenticationScheme somehow hide the User Identity that was created with the same authentication type?
UseAuthenticationmiddleware so identity isn't set yet? - AlexanderUseAuthenticationand inUseAuthorization, see this post to learn more about what exactlyUseAuthenticationdoes stackoverflow.com/questions/48836688/… - Michael Shterenberg