-
Notifications
You must be signed in to change notification settings - Fork 785
Update Secure an ASP.NET Core Blazor Web App with OpenID Connect (OIDC) to demonstrate Access Token Management #570
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
using Duende.AccessTokenManagement.OpenIdConnect; | ||
using Microsoft.AspNetCore.Authentication.Cookies; | ||
|
||
namespace BlazorWebAppOidc; | ||
|
||
/// <summary> | ||
/// Automatically validates the token. | ||
/// See https://docs.duendesoftware.com/accesstokenmanagement/blazor-server/ for more information. | ||
/// </summary> | ||
public class CookieEvents : CookieAuthenticationEvents | ||
{ | ||
private readonly IUserTokenStore _store; | ||
|
||
public CookieEvents(IUserTokenStore store) => _store = store; | ||
|
||
public override async Task ValidatePrincipal(CookieValidatePrincipalContext context) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should use |
||
{ | ||
var token = await _store.GetTokenAsync(context.Principal!); | ||
if (!token.Succeeded) | ||
{ | ||
context.RejectPrincipal(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It appears that the sample no longer rejects the cookie if the token has expired and you cannot use the refresh token to get an unexpired token. Can we add that functionality back? Also, if the token is missing from the store, wouldn't it be better just to add it back from the store rather than reject the cookie. Since we're still using If we cannot do that, maybe we should log something here. I know we weren't before, but I could see this coming up a lot more often now that something like a server restart can cause it. |
||
} | ||
|
||
await base.ValidatePrincipal(context); | ||
} | ||
} |
This file was deleted.
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
using Duende.AccessTokenManagement; | ||
using Duende.AccessTokenManagement.OpenIdConnect; | ||
using Microsoft.AspNetCore.Authentication.OpenIdConnect; | ||
|
||
namespace BlazorWebAppOidc; | ||
|
||
/// <summary> | ||
/// Stores access token and refresh token in the server-side token store once a token is validated. | ||
/// See https://docs.duendesoftware.com/accesstokenmanagement/blazor-server/ for more information. | ||
/// </summary> | ||
public class OidcEvents : OpenIdConnectEvents | ||
{ | ||
private readonly IUserTokenStore _store; | ||
|
||
public OidcEvents(IUserTokenStore store) => _store = store; | ||
|
||
public override async Task TokenValidated(TokenValidatedContext context) | ||
{ | ||
var exp = DateTimeOffset.UtcNow.AddSeconds(double.Parse(context.TokenEndpointResponse!.ExpiresIn)); | ||
|
||
await _store.StoreTokenAsync(context.Principal!, new UserToken | ||
{ | ||
AccessToken = AccessToken.Parse(context.TokenEndpointResponse.AccessToken), | ||
AccessTokenType = AccessTokenType.Parse(context.TokenEndpointResponse.TokenType), | ||
RefreshToken = RefreshToken.Parse(context.TokenEndpointResponse.RefreshToken), | ||
Scope = !string.IsNullOrEmpty(context.TokenEndpointResponse.Scope) | ||
? Scope.Parse(context.TokenEndpointResponse.Scope) | ||
: Scope.Parse(string.Join(" ", context.Options.Scope)), | ||
ClientId = !string.IsNullOrEmpty(context.ProtocolMessage.ClientId) | ||
? ClientId.Parse(context.ProtocolMessage.ClientId) | ||
: ClientId.Parse(context.Options.ClientId!), | ||
IdentityToken = IdentityToken.Parse(context.TokenEndpointResponse.IdToken), | ||
Expiration = exp, | ||
}); | ||
|
||
await base.TokenValidated(context); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
RTM is around the corner and will be updated
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I asked offline about this in a separate message chain. Generally, they aren't keen on recommending preview packages in production, which is what our readers do with these samples. Will the 3.2.0 package not work for this? If not, when is "around the corner"? Is it like a week ... or month?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
End of August most probably, unless anything slips unexpectedly