ASP.NETCore3.1OpenIDConnect,如何在用户不退出的情况下更新声明?
创始人
2024-09-17 09:30:37
0

可以使用令牌端点中的_refresh_token_来更新声明。在配置OpenID Connect时,需要在OnTokenValidated事件中手动请求并使用刷新令牌获取新的声明。

示例代码如下:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
    options.ForwardAuthenticate = OpenIdConnectDefaults.AuthenticationScheme;
    options.ForwardSignIn = OpenIdConnectDefaults.AuthenticationScheme;
})
.AddOpenIdConnect(options =>
{
    options.ClientId = Configuration["OpenIdConnect:ClientId"];
    options.ClientSecret = Configuration["OpenIdConnect:ClientSecret"];
    options.Authority = Configuration["OpenIdConnect:Authority"];
    options.CallbackPath = new PathString("/auth/signin-callback");

    // Request refresh token
    options.ResponseType = "code id_token token";
    options.Scope.Clear();
    options.Scope.Add("openid");
    options.Scope.Add("profile");
    options.Scope.Add("email");
    options.Scope.Add("offline_access");

    options.SaveTokens = true;
    options.GetClaimsFromUserInfoEndpoint = true;
    options.Events = new OpenIdConnectEvents
    {
        OnTokenValidated = async ctx =>
        {
            var refreshToken = ctx.TokenEndpointResponse.RefreshToken;
            if (refreshToken != null)
            {
                var newClaims = new List
                {
                    new Claim("newClaimType", "newClaimValue")
                };
                var appOptions = ctx.HttpContext.RequestServices.GetRequiredService>().Value;
                var tokenClient = new TokenClient(appOptions.TokenEndpoint, appOptions.ClientId, appOptions.ClientSecret);
                var tokenResult = await tokenClient.RequestRefreshTokenAsync(refreshToken);

                if (tokenResult.IsError)
                {
                    throw new Exception(tokenResult.Error);
                }

                foreach (var claim in tokenResult.Claims)
                {
                    newClaims.Add(new Claim(claim.Type, claim.Value));
                }

                var identity = ctx.Principal.Identity as ClaimsIdentity;
                identity?.AddClaims(newClaims);
            }
        }
    };
});

注:在请求刷新令牌时,需要在OpenID Connect配置中设置options.ResponseType = "code id_token token"以请求令牌响应中包含刷新令牌。

相关内容

热门资讯

记者揭秘!微信小程序微乐辅助器... 记者揭秘!微信小程序微乐辅助器破解版,wepoker能不能透视,第三方教程(有挂细节);运微信小程序...
终于清楚!佛手在线破解,wep... 您好:wepoker手机版透视脚本这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现...
重大消息!广东雀神智能插件辅助... 重大消息!广东雀神智能插件辅助脚本,We poker辅助器下载,wepoke教程(有挂方针)相信很多...
最新技巧!爱玩联盟辅助软件视频... 最新技巧!爱玩联盟辅助软件视频讲解,破解辅助插件wepoker,黑科技教程(有挂助手)相信很多朋友都...
玩家攻略!闲逸亲友圈app辅助... 玩家攻略!闲逸亲友圈app辅助,wpk透视工作室,专业教程(有挂技巧);最新版2026是一款经典耐玩...
重磅来袭!广西友乐app辅助工... 重磅来袭!广西友乐app辅助工具,hhpoker辅助器视频,攻略教程(了解有挂);广西友乐app辅助...
盘点几款!广东闲来有辅助软件,... 盘点几款!广东闲来有辅助软件,pokerrrr2辅助,技巧教程(有挂攻略),广东闲来有辅助软件是用手...
我来向大家传授!微信小程序多功... 我来向大家传授!微信小程序多功能辅助,德普辅助软件,透明教程(有挂讲解);超受欢迎的微信小程序多功能...
总算了解!蜜瓜大厅辅助免费下载... 总算了解!蜜瓜大厅辅助免费下载,wepoker私人定制透视,新版2025教程(真的有挂)是一款可以让...
实测交流!大玩家福建十三水辅助... 实测交流!大玩家福建十三水辅助,wpk有辅助器吗,力荐教程(有挂助手);1、许多玩家不知道大玩家福建...