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;

相关内容

热门资讯

第5分钟辅助!小松宿松游戏辅助... 您好:这款小松宿松游戏辅助游戏是可以开挂的,确实是有挂的,很多玩家在这款小松宿松游戏辅助游戏中打牌都...
第8分钟辅助!皇豪互众辅助软件... 第8分钟辅助!皇豪互众辅助软件是干嘛的,微信小程序微乐辅助器免费下载(必备教程开挂辅助脚本);微信小...
第一分钟透视!科米台州麻将辅助... 科米台州麻将辅助 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1...
第6分钟辅助!玩吧辅助工具,火... 第6分钟辅助!玩吧辅助工具,火神辅助免费下载(玩家必看科普开挂辅助平台);1、这是跨平台的火神辅助免...
第三分钟辅助!广东雀神挂件怎么... 【福星临门,好运相随】;第三分钟辅助!广东雀神挂件怎么样,雀神广东麻将透视视频(透视新版开挂辅助插件...
9分钟开挂!河洛杠次辅助,边锋... 9分钟开挂!河洛杠次辅助,边锋老友修改器(揭秘真相开挂辅助插件) >>您好:软件加薇13670430...
两分钟辅助!wepoker私人... 两分钟辅助!wepoker私人局辅助挂,微信小程序辅助器防检测(总算了解开挂辅助平台);一、微信小程...
四分钟开挂!小程序河北微乐脚本... 皮皮衡阳字牌黑科技视频 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由...
第五分钟辅助!对战互娱辅助系统... 第五分钟辅助!对战互娱辅助系统,随意玩5元流量包(透视科技开挂辅助神器);对战互娱辅助系统AI智能教...
七分钟开挂!天天飞小鸡辅助,榕... >>您好:天天飞小鸡辅助确实是有挂的,很多玩家在这款天天飞小鸡辅助游戏中打牌都会发现很多用户的牌特别...