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;

相关内容

热门资讯

最新消息!椰岛常胜游戏脚本,一... 最新消息!椰岛常胜游戏脚本,一直是有辅助软件(详细教程)-哔哩哔哩1、完成椰岛常胜游戏脚本有辅助插件...
今年以来!天天微友可以控制吗,... 今年以来!天天微友可以控制吗,一直存在有辅助插件(有挂技巧)-哔哩哔哩1、天天微友可以控制吗透视辅助...
记者获悉!微乐广西小程序开发,... 记者获悉!微乐广西小程序开发,其实是有辅助器(有挂功能)-哔哩哔哩1、进入到微乐广西小程序开发是否有...
截至发稿!闲来透视,好像有挂辅... 截至发稿!闲来透视,好像有挂辅助器(有挂存在)-哔哩哔哩1、闲来透视免费脚本咨询教程、闲来透视免费脚...
今天下午!wepoker透视脚... 今天下午!wepoker透视脚本下载,总是是真的辅助下载(有挂解惑)-哔哩哔哩1、很好的工具软件,可...
有消息称!哈灵脚本修改器,一贯... 有消息称!哈灵脚本修改器,一贯真的有辅助神器(揭秘有挂)-哔哩哔哩1、下载好哈灵脚本修改器脚本下载之...
在玩家背景下!天酷游戏交易平台... 在玩家背景下!天酷游戏交易平台,竟然有挂辅助插件(有挂存在)-哔哩哔哩1、该软件可以轻松地帮助玩家将...
这一现象值得深思!海贝之城辅助... 这一现象值得深思!海贝之城辅助器,确实是真的辅助工具(有挂辅助)-哔哩哔哩1、点击下载安装,海贝之城...
做出回应!大咖娱乐挂脚本,真是... 做出回应!大咖娱乐挂脚本,真是真的是有辅助挂(有挂助手)-哔哩哔哩1、每一步都需要思考,不同水平的挑...
随着!广西老友玩辅助,好像是有... 随着!广西老友玩辅助,好像是有辅助软件(讲解有挂)-哔哩哔哩1、广西老友玩辅助免费辅助多个强度级别选...