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;

相关内容

热门资讯

总算了解!德州poker有外挂... 总算了解!德州poker有外挂,广东雀神小程序辅助器最新版,中至鹰潭麻将有没有猫腻(有挂细节)1、完...
实操分享!德扑分析软件,微信雀... 实操分享!德扑分析软件,微信雀神麻将软挂神器,小甘麻将助赢神器(有挂方法)1、很好的工具软件,可以解...
发现一款!wpk数据清零,雀神... 发现一款!wpk数据清零,雀神辅助器插件,福建天天开心辅助工具下载(有挂工具);1、游戏颠覆性的策略...
热点推荐!微扑克wpk辅助软件... 热点推荐!微扑克wpk辅助软件,雀神广东麻雀外挂怎么用,28圈有没有外挂(有挂工具)热点推荐!微扑克...
必备攻略!微扑克ai辅助工具,... 必备攻略!微扑克ai辅助工具,雀神小程序有挂的吗,雀魂有没有辅助器(有挂揭秘);1、下载好雀魂有没有...
热门推荐!fishpoker正... 您好,浙江游戏大厅辅助器随意选牌这款游戏可以开挂的,确实是有挂的,需要了解加微【757446909】...
今日头条!德扑之星发牌,雀神辅... 今日头条!德扑之星发牌,雀神辅助器免费下载,桂林字牌十打九赢的方法(有挂辅助)一、桂林字牌十打九赢的...
重大推荐!wepoke软件透明... 重大推荐!wepoke软件透明功能实现方法,广东雀神麻雀辅助器,老友麻将有挂的吗(有挂黑科技)1、老...
重大科普!德扑胜率计算的软件,... 重大科普!德扑胜率计算的软件,微信雀神广东麻雀小程序辅助免费,悠闲麻将川南版有假吗(有挂解密);1、...
免费测试版!wpk透视辅助可测... 免费测试版!wpk透视辅助可测试真的,广东雀神辅助器微信插件,闲逸斗地主app外 挂(有挂辅助)1、...