如何从 Google OAUTH 在 ASP.Net Core 5 Identity 中获取刷新令牌?
How can I get a refresh-token from Google OAUTH in ASP.Net Core 5 Identity?
如何在 ASP.Net Core Identity 5 中从 Google 获取刷新令牌?
我能够获得访问令牌,但不能获得刷新令牌。
Startup.cs、配置服务
...
services.AddAuthentication()
.AddGoogle(options =>
{
IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
options.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
options.Scope.Add("https://www.googleapis.com/auth/calendar");
//this should enable a refresh-token, or so I believe
options.AccessType = "offline";
options.SaveTokens = true;
options.Events.OnCreatingTicket = ctx =>
{
List<AuthenticationToken> tokens = ctx.Properties.GetTokens().ToList();
tokens.Add(new AuthenticationToken()
{
Name = "TicketCreated",
Value = DateTime.UtcNow.ToString()
});
ctx.Properties.StoreTokens(tokens);
return Task.CompletedTask;
};
});
当我使用 google 帐户注册并且代码点击“OnCreatingTicket”时,我获得了访问令牌 - 但没有刷新令牌...:[=14=]
问题
我缺少什么来获得刷新令牌?
代码实际上工作得很好。
但是,您仅在第一次注册特定 OAuth 2.0 客户端 ID 的新帐户时从 Google 获得刷新令牌。我正在删除我的本地用户数据并重新注册 - 但这不会使 google 再次发送刷新令牌 - 仅访问令牌。
如果你想离线使用刷新令牌,你还需要自己将它存储在某个地方(比如在数据库中)——上面的代码不会发生这种情况。
我可能来晚了一点,但发现您可以将这一行放在选项中以强制刷新令牌。 (这有点让你介意)
options.AuthorizationEndpoint += "?prompt=consent";
如何在 ASP.Net Core Identity 5 中从 Google 获取刷新令牌?
我能够获得访问令牌,但不能获得刷新令牌。
Startup.cs、配置服务
...
services.AddAuthentication()
.AddGoogle(options =>
{
IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");
options.ClientId = googleAuthNSection["ClientId"];
options.ClientSecret = googleAuthNSection["ClientSecret"];
options.Scope.Add("https://www.googleapis.com/auth/userinfo.email");
options.Scope.Add("https://www.googleapis.com/auth/userinfo.profile");
options.Scope.Add("https://www.googleapis.com/auth/calendar");
//this should enable a refresh-token, or so I believe
options.AccessType = "offline";
options.SaveTokens = true;
options.Events.OnCreatingTicket = ctx =>
{
List<AuthenticationToken> tokens = ctx.Properties.GetTokens().ToList();
tokens.Add(new AuthenticationToken()
{
Name = "TicketCreated",
Value = DateTime.UtcNow.ToString()
});
ctx.Properties.StoreTokens(tokens);
return Task.CompletedTask;
};
});
当我使用 google 帐户注册并且代码点击“OnCreatingTicket”时,我获得了访问令牌 - 但没有刷新令牌...:[=14=]
问题
我缺少什么来获得刷新令牌?
代码实际上工作得很好。
但是,您仅在第一次注册特定 OAuth 2.0 客户端 ID 的新帐户时从 Google 获得刷新令牌。我正在删除我的本地用户数据并重新注册 - 但这不会使 google 再次发送刷新令牌 - 仅访问令牌。
如果你想离线使用刷新令牌,你还需要自己将它存储在某个地方(比如在数据库中)——上面的代码不会发生这种情况。
我可能来晚了一点,但发现您可以将这一行放在选项中以强制刷新令牌。 (这有点让你介意)
options.AuthorizationEndpoint += "?prompt=consent";