身份服务器:刷新当前经过身份验证的用户的令牌

问题描述 投票:0回答:1

认证配置:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
}).AddCookie(CookieAuthenticationDefaults.AuthenticationScheme)
.AddOpenIdConnect(OpenIdConnectDefaults.AuthenticationScheme, options =>
    {
        options.TokenValidationParameters = new TokenValidationParameters
        {
            NameClaimType = JwtClaimTypes.Name,
            RoleClaimType = JwtClaimTypes.Role,
        };
        
        options.SignedOutRedirectUri = "/Home";
        options.SignedOutCallbackPath = "/Account/SignOutCallback";

        options.Authority = Authority;
        options.ClientId = ClientId;
        options.ClientSecret = ClientSecret;
        options.ResponseType = "code";
        
        //Scopes is "Api.Scope", "offline_access"
        foreach (var scope in Scopes)
        {
            options.Scope.Add(scope);
        }

        options.SaveTokens = true;
    });

在控制器中使用

Autorize
属性时,此配置重定向到身份服务器登录

访问令牌:

var accessToken = await _contextAccessor.HttpContext.GetTokenAsync("access_token");

出于测试目的,客户端访问令牌生命周期配置为 120,因此令牌将在 2 分钟后过期。

客户端应用程序是一个 MVC Web 应用程序,我使用外部登录方法通过 AD 对用户进行身份验证。因为为了调用 API 我需要所有用户信息 (角色和主张)。 按照文档中的此方法: https://docs.duendesoftware.com/identityserver/v5/quickstarts/2_interactive/

现在的问题是如何在访问令牌过期时刷新它。

asp.net asp.net-core asp.net-identity identityserver4 duende-identity-server
1个回答
0
投票

这适用于身份服务器 4 代码流。
在 IdentityServer 配置中,将

AllowOfflineAccess=true,
IdentityServerConstants.StandardScopes.OfflineAccess
添加到允许的范围。
在客户端oidc认证中,添加
options.Scope.Add("offline_access");
后 然后你就可以通过

看到刷新令牌
<dl>
    @foreach (var prop in (await Context.AuthenticateAsync()).Properties.Items)
    {
        <dt>@prop.Key</dt>
        <dd>@prop.Value</dd>
    }
</dl>

© www.soinside.com 2019 - 2024. All rights reserved.