Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set expiration date to client cookies?

I configured Identity Server:

public void Configuration(IAppBuilder app)
{
    var factory = new IdentityServerServiceFactory().UseInMemoryClients(new Client[] {
        new Client()
        {
            ClientName = "MyClient",
            ClientId = "MyClientId",
            Enabled = true,
            Flow = Flows.Implicit,
            RedirectUris = new List<string> { "MyClientServer/callback" },
        };
    });
}

and client server:

public void Configuration(IAppBuilder app)
{
    var cookieOptions = new CookieAuthenticationOptions();
    cookieOptions.AuthenticationType = "Cookies";
    app.UseCookieAuthentication(cookieOptions);

    var authenticationOptions = new OpenIdConnectAuthenticationOptions() {
        Authority = "https://MyIdentityServer/core",
        ClientId = "MyClientId",
        SignInAsAuthenticationType = "Cookies",
        UseTokenLifetime = true,
        RedirectUri = "MyClientServer/callback"
    });

    app.UseOpenIdConnectAuthentication(authenticationOptions);
}

When user login with "Remember Me" option Identity cookie has expired date:

idsvr.session    expires 04 October ...

But client cookie does not:

.AspNet.Cookies  at end of session

What should I do to set the same expiration date to client cookie?

UPDATE:

I can set any expiration date in client application:

authenticationOptions.Provider = new CookieAuthenticationProvider()
{
    OnResponseSignIn = (context) =>
    {
        var isPersistent = context.Properties.IsPersistent;
        if (isPersistent) // Always false
        {
            context.CookieOptions.Expires = DateTime.UtcNow.AddDays(30);
        }
    }
};

But I cannot determine when to set expiration date. It should be set only when user selects "Remember Me", but IsPersistent option always false on client side.

The problem exists on simple boilerplate project too: https://identityserver.github.io/Documentation/docsv2/overview/mvcGettingStarted.html

UPDATE2:

I need client cookie to be persistent because of bug in Safari - https://openradar.appspot.com/14408523

Maybe some workaround exists, so I can pass expiration date in callback from Identity to Client?

UPDATE3:

Actually, our Identity and Client servers have same parent domain like app.server.local and id.server.local. Maybe I can pass expiration date via additional cookie that belongs to parent domain (.server.local)? But I have no idea where it can be written on Identity, and where it can be applied on Client.

like image 234
Artem Avatar asked Oct 04 '17 16:10

Artem


1 Answers

A cookie issued by IdentityServer and a cookie issued by a client application are not linked in any way. IdentityServer does not have any control over cookies in a client application.

When you log in to IdentityServer, you are issued a cookie that tracks the authenticated user within IdentityServer. This saves the user from entering their credentials for every client application, facilitating single sign on.

By default this cookie lasts for that session (so it expires once the browser closes), otherwise if you set "remember me" it will last for a set number of days, across sessions.

A cookie in a client application would be issued upon successful verification of an identity token from IdentityServer. This cookie can have any expiration time, any policy, any name. It's completely controlled by the client application. In your case client cookie expiration can be set in the CookieAuthenticationOptions in your client application.

like image 100
Scott Brady Avatar answered Sep 21 '22 21:09

Scott Brady