ASP.NetCore5Identity如何从GoogleOAUTH获取刷新令牌?
创始人
2024-09-17 11:30:24
0
  1. 在Google API控制台中创建一个OAuth客户端ID,并设置重定向URI。保存客户端ID和客户端机密。

  2. 安装'Google.Apis.Auth.AspNetCore”和'Google.Apis.Oauth2.v2”。

  3. 在'Startup.cs”文件中配置Google授权服务。在'ConfigureServices”方法中添加以下代码:

services
.AddAuthentication(options =>
{
  options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
  options.DefaultChallengeScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie()
.AddGoogleOpenIdConnect(options =>
{
  options.ClientId = Configuration["Google:ClientId"];
  options.ClientSecret = Configuration["Google:ClientSecret"];
  options.SaveTokens = true;
  options.Events.OnCreatingTicket = async context =>
  {
    // 获取刷新令牌并保存到身份验证令牌中。
    var refresh_token = context.Properties.GetTokenValue("refresh_token").ToString();
    context.Properties.StoreTokens(new[]
    {
      new AuthenticationToken { Name = "refresh_token", Value = refresh_token }
    });
  };
});

注意:应'ClientId”和'ClientSecret”替换为Google API控制台中创建的OAuth客户端ID和客户端机密。

  1. 在'Startup.cs”文件的'Configure”方法中使用Google授权服务:
app.UseAuthentication();
app.UseAuthorization();
  1. 在需要访问Google API的地方,通过以下代码获取刷新令牌:
string refresh_token = await HttpContext.GetTokenAsync("refresh_token");

注意:应该先通过上述步骤进行身份验证才能获取刷新令牌。

完整的'Startup.cs”文件代码示例:

using Google.Apis.Auth.AspNetCore;
using Google.Apis.Oauth2.v2;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace MyAppNamespace
{
  public class Startup
  {
    public Startup(IConfiguration configuration)
    {
      Configuration = configuration;
    }

    public IConfiguration Configuration { get; }

    public void ConfigureServices(IServiceCollection services)
    {
      services
      .AddAuthentication(options =>
      {
        options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        options.DefaultChallengeScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
      })
      .AddCookie()
      .AddGoogleOpenIdConnect(options =>
      {
        options.ClientId = Configuration["Google:ClientId"];
        options.ClientSecret = Configuration["Google:ClientSecret"];
        options.SaveTokens = true;

相关内容

热门资讯

一分钟了解!wepoker怎么... 一分钟了解!wepoker怎么破解游戏,拱趴大菠萝怎么开挂,总是是有挂(有挂技巧)透视方法中分为三种...
玩家攻略!竞技联盟透视插件,哈... 玩家攻略!竞技联盟透视插件,哈糖大菠萝可以开挂吗,确实真的是有挂(详细教程)1、不需要AI权限,帮助...
分享个大家!wepoker数据... 分享个大家!wepoker数据分析工具,pokerrrr2辅助,其实是有挂(真的有挂)1、辅助软件下...
三分钟了解!aapoker怎么... 三分钟了解!aapoker怎么控制牌,哈糖大菠萝开挂,原来有挂(竟然有挂)1、进入到是否有挂之后,能...
专业讨论!德州局hhpoker... 专业讨论!德州局hhpoker,wepoker辅助透视,竟然存在有挂(有挂教程)专业讨论!德州局hh...
科技揭秘!wepoker辅助透... 科技揭秘!wepoker辅助透视软件,菠萝德普辅助器免费版在哪里,本来真的是有挂(有挂教程)科技揭秘...
必备科技!德普之星透视软件免费... 必备科技!德普之星透视软件免费入口官网,wepoker网页版透视方法,其实是真的有挂(有挂方式)1、...
盘点十款!pokemmo辅助器... 盘点十款!pokemmo辅助器,德州透视是真的假的,都是真的有挂(真实有挂)1、起透看视 辅助软件价...
热点讨论!wpk刷入池率脚本,... 热点讨论!wpk刷入池率脚本,约局吧辅助器,一直是有挂(有挂功能)1、超多福利:超高返利,海量正版游...
最新研发!hhpoker有没有... 最新研发!hhpoker有没有作弊辅助,wepoker有没有插件,确实有挂(有挂秘诀)1)有没有挂:...