问题
I've followed Quickstart Hybrid Flow here but I need some help and advices about saving tokens after using refresh token.
If I say true, the option SaveTokens allows to save tokens in cookies.
Firstly, is it a good idea to store access and refresh tokens in a cookie (concerns about security) ?
Other question, I retrieve correctly refresh token via the code
var refreshToken = await HttpContext.GetTokenAsync("refresh_token");
but now, when I get the new access token, how can I store it (no SetTokenAsync
method) ?... because without that, I retrieve the old acces token when I call var refreshToken = await HttpContext.GetTokenAsync("access_token");
whereas I would like get the new.
Thanks
回答1:
From the documentation:
Interactive clients should use an authorization code-based flow. To protect against code substitution, either hybrid flow or PKCE should be used.
Thus the combination of PKCE and hybrid flow is not necessary and probably not useful.
If PKCE is available, this is the simpler solution to the problem.
PKCE is already the official recommendation for native applications and SPAs - and with the release of ASP.NET Core 3 also by default supported in the OpenID Connect handler as well.
So instead of using the hybrid flow, configure it as interactive ASP.NET Core MVC client.
new Client
{
ClientId = "mvc",
ClientSecrets = { new Secret("secret".Sha256()) },
AllowedGrantTypes = GrantTypes.Code,
RequireConsent = false,
RequirePkce = true,
// where to redirect to after login
RedirectUris = { "http://localhost:5002/signin-oidc" },
// where to redirect to after logout
PostLogoutRedirectUris = { "http://localhost:5002/signout-callback-oidc" },
AllowedScopes = new List<string>
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile
}
}
Where the mvc client has the expected configuration:
.AddOpenIdConnect("oidc", options =>
{
options.Authority = "http://localhost:5000";
options.RequireHttpsMetadata = false;
options.ClientId = "mvc";
options.ClientSecret = "secret";
options.ResponseType = "code";
options.SaveTokens = true;
});
I can also recommend this post from Brock Allen. This may answer your question about cookies. You can also check the post of Dominick Baier.
For information on how to use the refresh token please read my answer here.
来源:https://stackoverflow.com/questions/58434954/identityserver4-refresh-tokens-hybrid-flow-cookies-and-storage