ADAL failed to return token in webAPI. Error:Invalid jwt token using userassersion

ADAL failed to return token in webAPI. Error:Invalid jwt token using userassersion

已创建调用 Web api.Two 应用程序的本机应用程序已在 azure.Here 中创建,是用于获取访问令牌的代码代码并且运行良好,我正在获取访问令牌:

UserCredential uc = new UserPasswordCredential(userName, password);
result = authContext.AcquireTokenAsync(todoListResourceId,clientId, 
uc).Result;

现在要在旧令牌(1 小时)到期后访问新令牌,我正在使用代码:

 AuthenticationContext authContext = new AuthenticationContext(authority);
        UserAssertion userAssertion = new UserAssertion(oldToken, "urn:ietf:params:oauth:grant-type:jwt-bearer", userName);
        AuthenticationResult result = authContext.AcquireTokenAsync(todoListResourceId, clientId, userAssertion).ConfigureAwait(false).GetAwaiter().GetResult();

但我得到的错误是:"Invalid JWT token. AADSTS50027: Invalid JWT token. Token format not valid"。 检查 JWT 令牌:格式正确,可以使用 jwt.io 解码。 注意:这两个代码片段使用的客户端 ID 是相同的 appId。

我知道这与问题 完全相同。我无法对那个问题发表评论,这就是我再次提问的原因。 谁能帮帮我?

如果有人能够提供帮助以其他方式在不使用用户密码的情况下获取令牌,那就太好了,因为我需要在内部生成新令牌而无需用户再次输入密码。

针对用户在原生应用上完成身份验证,而该原生应用需要调用web的场景API。 Azure AD 颁发 JWT 访问令牌来调用网络 API。如果网站API需要调用另一个下游网站API,它可以使用代理流程将用户的身份和身份验证委托给第二层网站API。

请参考this document for more details about On-Behalf-Of flow . You can also refer to code sample

守护进程(你的webapi)需要在没有用户身份的情况下调用webAPI的场景,你应该使用client credential flow to use its own credentials instead of impersonating a user, to authenticate when calling another web service. Code sample here供你参考。

请点击here查看以上两种情况的说明。您的代码正在使用 Resource Owner Password Credentials Grant,此流程具有多重限制,例如不支持 2FA,因此不推荐使用。

如果我误解了您的要求,请随时告诉我。