Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cookie configuration mystery using AddCookie and ConfigureApplicationCookie #45788

Closed
qui8t opened this issue Dec 28, 2022 · 12 comments
Closed
Labels
area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer area-identity Includes: Identity and providers ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Milestone

Comments

@qui8t
Copy link

qui8t commented Dec 28, 2022

I am confused how to set the cookie options, it seems some options are effective only if set using AddCookie and some others if set using ConfigureApplicationCookie. For example,

  1. LoginPath would be effective only if set in AddCookie:
services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
    options.LoginPath = "signin";
});

services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = "signin"; 
    // No effect; if the setting in `AddCookie` is removed, this will fall back to the default `Account/Login` option.
});
  1. ExpireTimeSpan would be effective only if set in ConfigureApplicationCookie:
services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromSeconds(30);
    // No effect; if the setting in `ConfigureApplicationCookie` is removed, this will fall back to the default timespan.
});

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromSeconds(30);
});
@javiercn javiercn added the area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer label Dec 28, 2022
@blowdart
Copy link
Contributor

blowdart commented Jan 3, 2023

You are mixing and matching things for generic cookie authentication (AddCookie) and things meant for asp.net identity (ConfigureApplicationCookie).

Which auth are you trying to use?

@blowdart blowdart added the Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. label Jan 3, 2023
@ghost
Copy link

ghost commented Jan 3, 2023

Hi @qui8t. We have added the "Needs: Author Feedback" label to this issue, which indicates that we have an open question for you before we can take further action. This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@blowdart blowdart added the area-identity Includes: Identity and providers label Jan 3, 2023
@qui8t
Copy link
Author

qui8t commented Jan 4, 2023

I am trying to use Google authNZ with built-in asp.net identity, without leveraging any external library, so my config reads as the following

services
    .AddIdentity<AppUser, IdentityRole>()
    .AddEntityFrameworkStores<AccountDbContext>();

services.AddIdentityCore<AppUser>();

services
    .AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddJwtBearer()
    .AddCookie(options =>
    {
        options.LoginPath = "/account/login";
        options.Cookie.SameSite = SameSiteMode.Lax;
        options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
    })
    .AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
    {
        options.ClientId = _clientId;
        options.ClientSecret = _clientSecret;
        options.UsePkce = true;
        options.ClaimActions.MapJsonKey(GoogleProfilePictureClaim, "picture", "url");
    });

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(15);
    options.SlidingExpiration = true;
});

And add the following for the middleware setup.

app.UseAuthentication();
app.UseAuthorization();

app.UseCookiePolicy(
    new CookiePolicyOptions
    {
        MinimumSameSitePolicy = SameSiteMode.Lax,
        Secure = CookieSecurePolicy.Always
    });

@ghost ghost added Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. and removed Needs: Author Feedback The author of this issue needs to respond in order for us to continue investigating this issue. labels Jan 4, 2023
@blowdart
Copy link
Contributor

blowdart commented Jan 4, 2023

Ah got it. @HaoK how does this all hang together?

@HaoK
Copy link
Member

HaoK commented Jan 12, 2023

@qui8t Underneath the covers all ConfigureApplicationCookie is doing is configuring the named cookie that identity sets as the default authentication scheme. Which is the "Identity.Application" cookie scheme. Your app is specifying your own cookie to use by default. Generally if you are not using the application cookie as the default scheme, you don't even to add it.

Basically you should be calling EITHER AddIdentity, or AddIdentityCore. If you call the former, that adds all of the authentication cookies for identity as well. If you call AddIdentityCore, you are responsible for wiring up the authentication part of things, which by default does this:

        services.AddAuthentication(o =>
        {
            o.DefaultScheme = IdentityConstants.ApplicationScheme;
            o.DefaultSignInScheme = IdentityConstants.ExternalScheme;
        })
        .AddIdentityCookies(o => { });

@HaoK HaoK added question and removed Needs: Attention 👋 This issue needs the attention of a contributor, typically because the OP has provided an update. labels Jan 12, 2023
@HaoK HaoK added this to the Discussions milestone Jan 12, 2023
@qui8t
Copy link
Author

qui8t commented Jan 13, 2023

Thank you @HaoK for the elaboration. I removed services.AddIdentityCore<AppUser>();. If I am understanding it correct, I can setup the cookie in ConfigureApplicationCookie, so I changed my code as the following.

services
    .AddAuthentication(options =>
    {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    })
    .AddJwtBearer()
    .AddCookie( /* config moved to ConfigureApplicationCookie */)
    .AddGoogle(GoogleDefaults.AuthenticationScheme, options =>
    {
        options.ClientId = _clientId;
        options.ClientSecret = _clientSecret;
        options.UsePkce = true;
        options.ClaimActions.MapJsonKey(GoogleProfilePictureClaim, "picture", "url");
    });

services.ConfigureApplicationCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromDays(15);
    options.SlidingExpiration = true;

    // moved here from AddCookie()
    options.LoginPath = "/account/login";
    options.Cookie.SameSite = SameSiteMode.Lax;
    options.Cookie.SecurePolicy = CookieSecurePolicy.Always;
});

With this setup, at least options.LoginPath is not configured properly.

@HaoK
Copy link
Member

HaoK commented Jan 13, 2023

What do you mean loginpath is not configured properly? Its not /account/login as you expect? This is likely due to AddIdentity stomping on it. If you want to configure the cookies yourself, a better alternative might be to remove the call to AddIdentity and keep AddIdentityCore, and don't use any of the built in identity cookies, then you have full control over the authentication/cookies and you won't have to deal with stomping on cookie settings.

@qui8t
Copy link
Author

qui8t commented Jan 13, 2023

I want the user to be redirected to the "/account/login" page if the cookie has expired or they requested a protected resource while they are not authenticated yet. With the setup in my previous post, this does not happen. With the setup in my first post, this happens.

@HaoK
Copy link
Member

HaoK commented Jan 18, 2023

I suggest not using the identity application cookies and configuring the cookie completely yourself rather than mixing and matching your own cookies with the identity cookies

@qui8t
Copy link
Author

qui8t commented Jan 18, 2023

I'd love to avoid mixing, though I am trying to remark upon the fact that I can not implement the use-case I mentioned without mixing and matching them. It seems some functionality is available in one and some in the other. Please refer to my first post, and I am happy to elaborate if needed.

@chuannguyen1208
Copy link

services
  

As my understanding, you should remove .AddIdentity<AppUser, IdentityRole>() and replace with .AddIdentityCore instead, then try your login path again.

@HaoK HaoK removed their assignment Feb 21, 2023
@HaoK HaoK added the ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. label Feb 21, 2023
@ghost ghost added the Status: Resolved label Feb 21, 2023
@ghost
Copy link

ghost commented Feb 22, 2023

This issue has been resolved and has not had any activity for 1 day. It will be closed for housekeeping purposes.

See our Issue Management Policies for more information.

@ghost ghost closed this as completed Feb 22, 2023
@ghost ghost locked as resolved and limited conversation to collaborators Mar 24, 2023
This issue was closed.
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-auth Includes: Authn, Authz, OAuth, OIDC, Bearer area-identity Includes: Identity and providers ✔️ Resolution: Answered Resolved because the question asked by the original author has been answered. question Status: Resolved
Projects
None yet
Development

No branches or pull requests

6 participants
@blowdart @HaoK @javiercn @chuannguyen1208 @qui8t and others