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"以请求令牌响应中包含刷新令牌。

相关内容

热门资讯

wepoke辅助挂!德扑之星实... wepoke辅助挂!德扑之星实战,(WPk)原生真的有挂(详细透视攻略方法);wepoke辅助挂软件...
辅助透视(德州ai人工智能)w... 辅助透视(德州ai人工智能)wepoke有正规吗(详细辅助揭秘攻略)从来是真的有挂1、德州ai人工智...
WePoKe透明挂!德扑人工智... 自定义WePoKe透明挂系统规律,只需要输入自己想要的开挂功能,一键便可以生成出微扑克专用辅助器,不...
透视有挂(智星德州菠萝外挂)约... 透视有挂(智星德州菠萝外挂)约局吧是正规的吗(详细辅助揭秘教程)其实是真的有挂是一款可以让一直输的玩...
wepoke模拟器!gg扑克发... wepoke模拟器!gg扑克发牌好假,(Wpk)本来是有挂(详细辅助解说技巧);支持多人共享记分板与...
透视肯定(wepoke确实有挂... 透视肯定(wepoke确实有挂)fishpoker大菠萝外挂(详细辅助透明教程)原本有挂1)wepo...
微扑克ai机器人!wepoke... 微扑克ai机器人!wepoke软件透明挂,(wepOke)从来存在有挂(详细透视wpk教程)是一款可...
透视智能ai(wepokeai... 透视智能ai(wepokeai机器人)微扑克有挂(详细辅助扑克教程)都是是真的有挂,您好,wepok...
wepoke一定有挂!wepo... wepoke一定有挂!wepoke辅助机器人,(AAPoKER)一直有挂(详细透视辅助教程)科技教程...
透视好友房(wepoke真的有... 透视好友房(wepoke真的有挂)德扑ai智能机器人线上代打(详细辅助科技教程)好像是有挂是一款可以...