Skip to content

Commit b72bfd2

Browse files
committed
Update the samples to clarify the relationship between the expiration of the authentication results returned by OpenIddict and the lifetime of authentication cookies based on them
1 parent efdf8ce commit b72bfd2

File tree

4 files changed

+74
-4
lines changed

4 files changed

+74
-4
lines changed

sandbox/OpenIddict.Sandbox.AspNet.Client/Controllers/AuthenticationController.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -195,7 +195,25 @@ or Claims.Private.RegistrationId or Claims.Private.ProviderName
195195
OpenIddictClientOwinConstants.Tokens.BackchannelAccessToken or
196196
OpenIddictClientOwinConstants.Tokens.BackchannelIdentityToken or
197197
OpenIddictClientOwinConstants.Tokens.RefreshToken)
198-
.ToDictionary(pair => pair.Key, pair => pair.Value));
198+
.ToDictionary(pair => pair.Key, pair => pair.Value))
199+
{
200+
// Set the creation and expiration dates of the ticket to null to decorrelate the lifetime
201+
// of the resulting authentication cookie from the lifetime of the identity token returned by
202+
// the authorization server (if applicable). In this case, the expiration date time will be
203+
// automatically computed by the cookie handler using the lifetime configured in the options.
204+
//
205+
// Applications that prefer binding the lifetime of the ticket stored in the authentication cookie
206+
// to the identity token returned by the identity provider can remove or comment these two lines:
207+
IssuedUtc = null,
208+
ExpiresUtc = null,
209+
210+
// Note: this flag controls whether the authentication cookie that will be returned to the
211+
// browser will be treated as a session cookie (i.e destroyed when the browser is closed)
212+
// or as a persistent cookie. In both cases, the lifetime of the authentication ticket is
213+
// always stored as protected data, preventing malicious users from trying to use an
214+
// authentication cookie beyond the lifetime of the authentication ticket itself.
215+
IsPersistent = false
216+
};
199217

200218
context.Authentication.SignIn(properties, identity);
201219
return Redirect(properties.RedirectUri ?? "/");

sandbox/OpenIddict.Sandbox.AspNet.Server/Controllers/AuthenticationController.cs

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,25 @@ or Claims.Private.RegistrationId or Claims.Private.ProviderName
8686
// If needed, the tokens returned by the authorization server can be stored in the authentication cookie.
8787
OpenIddictClientOwinConstants.Tokens.BackchannelAccessToken or
8888
OpenIddictClientOwinConstants.Tokens.RefreshToken)
89-
.ToDictionary(pair => pair.Key, pair => pair.Value));
89+
.ToDictionary(pair => pair.Key, pair => pair.Value))
90+
{
91+
// Set the creation and expiration dates of the ticket to null to decorrelate the lifetime
92+
// of the resulting authentication cookie from the lifetime of the identity token returned by
93+
// the authorization server (if applicable). In this case, the expiration date time will be
94+
// automatically computed by the cookie handler using the lifetime configured in the options.
95+
//
96+
// Applications that prefer binding the lifetime of the ticket stored in the authentication cookie
97+
// to the identity token returned by the identity provider can remove or comment these two lines:
98+
IssuedUtc = null,
99+
ExpiresUtc = null,
100+
101+
// Note: this flag controls whether the authentication cookie that will be returned to the
102+
// browser will be treated as a session cookie (i.e destroyed when the browser is closed)
103+
// or as a persistent cookie. In both cases, the lifetime of the authentication ticket is
104+
// always stored as protected data, preventing malicious users from trying to use an
105+
// authentication cookie beyond the lifetime of the authentication ticket itself.
106+
IsPersistent = false
107+
};
90108

91109
context.Authentication.SignIn(properties, identity);
92110
return Redirect(properties.RedirectUri ?? "/");

sandbox/OpenIddict.Sandbox.AspNetCore.Client/Controllers/AuthenticationController.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,24 @@ public async Task<ActionResult> LogInCallback()
199199
// Build the authentication properties based on the properties that were added when the challenge was triggered.
200200
var properties = new AuthenticationProperties(result.Properties.Items)
201201
{
202-
RedirectUri = result.Properties.RedirectUri ?? "/"
202+
RedirectUri = result.Properties.RedirectUri ?? "/",
203+
204+
// Set the creation and expiration dates of the ticket to null to decorrelate the lifetime
205+
// of the resulting authentication cookie from the lifetime of the identity token returned by
206+
// the authorization server (if applicable). In this case, the expiration date time will be
207+
// automatically computed by the cookie handler using the lifetime configured in the options.
208+
//
209+
// Applications that prefer binding the lifetime of the ticket stored in the authentication cookie
210+
// to the identity token returned by the identity provider can remove or comment these two lines:
211+
IssuedUtc = null,
212+
ExpiresUtc = null,
213+
214+
// Note: this flag controls whether the authentication cookie that will be returned to the
215+
// browser will be treated as a session cookie (i.e destroyed when the browser is closed)
216+
// or as a persistent cookie. In both cases, the lifetime of the authentication ticket is
217+
// always stored as protected data, preventing malicious users from trying to use an
218+
// authentication cookie beyond the lifetime of the authentication ticket itself.
219+
IsPersistent = false
203220
};
204221

205222
// If needed, the tokens returned by the authorization server can be stored in the authentication cookie.

sandbox/OpenIddict.Sandbox.AspNetCore.Server/Controllers/AuthenticationController.cs

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,24 @@ public async Task<ActionResult> LogInCallback()
9090
// Build the authentication properties based on the properties that were added when the challenge was triggered.
9191
var properties = new AuthenticationProperties(result.Properties.Items)
9292
{
93-
RedirectUri = result.Properties.RedirectUri ?? "/"
93+
RedirectUri = result.Properties.RedirectUri ?? "/",
94+
95+
// Set the creation and expiration dates of the ticket to null to decorrelate the lifetime
96+
// of the resulting authentication cookie from the lifetime of the identity token returned by
97+
// the authorization server (if applicable). In this case, the expiration date time will be
98+
// automatically computed by the cookie handler using the lifetime configured in the options.
99+
//
100+
// Applications that prefer binding the lifetime of the ticket stored in the authentication cookie
101+
// to the identity token returned by the identity provider can remove or comment these two lines:
102+
IssuedUtc = null,
103+
ExpiresUtc = null,
104+
105+
// Note: this flag controls whether the authentication cookie that will be returned to the
106+
// browser will be treated as a session cookie (i.e destroyed when the browser is closed)
107+
// or as a persistent cookie. In both cases, the lifetime of the authentication ticket is
108+
// always stored as protected data, preventing malicious users from trying to use an
109+
// authentication cookie beyond the lifetime of the authentication ticket itself.
110+
IsPersistent = false
94111
};
95112

96113
// If needed, the tokens returned by the authorization server can be stored in the authentication cookie.

0 commit comments

Comments
 (0)