ServiceStack v6 JWTAuthProvider 没有 return 承载和刷新令牌

ServiceStack v6 JWTAuthProvider doesn't return bearer and resfresh tokens

我从 ServiceStack 网站下载了 .NET6 项目模板,摆弄并使用 CredentialAuthProvider 成功设置了身份验证。但是,当添加 JwtAuthProvider 时,它不会 return 预期的令牌(承载和刷新) ~ 都在 PostMan 和 ServiceStack 中测试API 探险家。它始终 return 与 CredentialAuthProvider 的响应相同,如下所示:

{
    "userId": "1",
    "sessionId": "MLheS29QdaaynocpNYLN",
    "userName": "admin@email.com",
    "displayName": "Admin User",
    "profileUrl": "...",
    "roles": ["Admin"],
    "permissions": []
}

这是我的 AuthFeature 设置

var privateKey = RsaUtils.CreatePrivateKeyParams(RsaKeyLengths.Bit2048);
appHost.Plugins.Add(new AuthFeature(() => new CustomUserSession(),
    new IAuthProvider[] {
        new JwtAuthProvider(appSettings)
        {
            HashAlgorithm = "RS256",
            PrivateKeyXml = privateKey.ToPrivateKeyXml(),
            RequireSecureConnection = false,
            SetBearerTokenOnAuthenticateResponse = true, 
        },
        new CredentialsAuthProvider(appSettings),     /* Sign In with Username / Password credentials */
}));

为了测试令牌数量,我使用下面的 DummyAuthProvider 并将其添加到 IAuthProvider 数组:

public class DummyAuthProvider : AuthProvider, IAuthResponseFilter
{
    public DummyAuthProvider() => Provider = "dummy";

    public Task ExecuteAsync(AuthFilterContext authContext)
    {
        //throw new NotImplementedException();
        var jwt = (JwtAuthProvider)AuthenticateService.GetJwtAuthProvider();

        var session = authContext.Session;
        var authService = authContext.AuthService;
        
        var shouldReturnTokens = authContext.DidAuthenticate;
        if (shouldReturnTokens && jwt.SetBearerTokenOnAuthenticateResponse && session.IsAuthenticated)
        {
            if (!jwt.RequireSecureConnection || authService.Request.IsSecureConnection)
            {
                //... will populate jwt tokens
            }
        }
       
        //.. wont populate jwt tokens
    
        return Task.CompletedTask;
    }
}

结果是它实际填充了 jwt 令牌。但是 API 响应 不包括填充的标记

非常感谢任何对此的见解。

请阅读 JWT Changes in latest ServiceStack v6 Release 默认情况下,JWT 在 HttpOnly Secure ss-tok Cookie 中返回。

您可以恢复为 JWT 返回 Bearer/Refresh 响应主体中的令牌:

new JwtAuthProvider(AppSettings) {
    UseTokenCookie = false
},

但是我建议使用默认 cookie,因为它能够提供改进的透明 JWT 令牌处理,经过身份验证的客户端会自动重新发送每个请求的 JWT 令牌 cookie。它像普通会话 Cookie 一样工作,它允许您的服务器切换到使用无状态 JWT Cookie,而无需更新现有的客户端身份验证。