IdentityServer4 icon indicating copy to clipboard operation
IdentityServer4 copied to clipboard

Add option to allow credentials in CorsPolicyProvider

Open henkli opened this issue 8 years ago • 6 comments

To be able to re-authenticate on-the-fly, it would be great to have an option in CorsPolicyProvider to set AllowCredentials on the CorsPolicyBuilder. If this is not possible/wanted, another option would be to be able to override CorsPolicyProvider and provide an own implementation.

In short, in CorsPolicyProvider.cs instead of this:

var policyBuilder = new CorsPolicyBuilder()
   .WithOrigins(origin)
   .AllowAnyHeader()
   .AllowAnyMethod();

I want to do this:

var policyBuilder = new CorsPolicyBuilder()
   .WithOrigins(origin)
   .AllowAnyHeader()
   .AllowAnyMethod();

if (_options.Cors.AllowCredentials)
{
   policyBuilder.AllowCredentials();
}

henkli avatar Feb 07 '18 10:02 henkli

To be able to re-authenticate on-the-fly, it would be great to have an option in CorsPolicyProvider to set AllowCredentials on the CorsPolicyBuilder.

I don't know what this means. Can you elaborate?

brockallen avatar Feb 07 '18 14:02 brockallen

Basically, this is the setup: SPA app gets id token from idsrv using WS Federation. Id token expires, client does an AJAX request to app, which fails because id token has expired.

Instead of asking the user to reauthenticate, I do an inflight ajax request to idsrv to get a new id token, and resend the original ajax request.

If credentials are not passed to idsrv for the inflight re-auth request, the auth fails.

I realise there are other ways to solve this scenario, but would adding the option I suggested to idsrv be viable?

henkli avatar Feb 07 '18 16:02 henkli

SPA app gets id token from idsrv using WS Federation.

That doesn't make sense to me. You mean SPA uses OIDC to IdentityServer, and IdentityServer uses ws-fed to some upstream IdP?

Id token expires, client does an AJAX request to app, which fails because id token has expired.

This doesn't make sense to me either. JS apps don't use id_token to make Ajax calls. They use access tokens.

brockallen avatar Feb 07 '18 16:02 brockallen

You're correct in your assumptions, sorry for the haphazard explanation. I'll try to open more details as I discover them myself.

The endpoint I'm interested in allowing CORS credentials for, is the /wsfed endpoint, which is based on your IdentityServer4.WsFederation WsFederationController sample. Without CORS allowing credentials to be passed, the session cookie for WsFederationController isn't present, and as such the user isn't known to WsFederationController, leading to a manual reauthentication (302 to login page).

I tried adding the wsfed endpoint to IdentityServer CorsPaths, and was hoping to use its CORS policy. It would work if the credentials (the session cookie) were allowed to be passed to the IdP by the CORS policy.

What I'm doing now as a workaround is setting another CORS policy in startup for the wsfed endpoint instead. My hope was that I wouldn't need this, and could rely solely on the identity server CORS policy. If you don't think that's a good idea, this issue can be closed.

Thanks for taking the time to respond.

henkli avatar Feb 08 '18 10:02 henkli

The endpoint I'm interested in allowing CORS credentials for, is the /wsfed endpoint

Again, doesn't make sense to me. Why Ajax calls there? That's not how it's normally meant to be used. Of course, you can always do what you want in your own custom host, but I don't think we're going to add support for something we don't think is not a normal workflow.

brockallen avatar Feb 10 '18 02:02 brockallen

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

lock[bot] avatar Jan 13 '20 17:01 lock[bot]