gpt4 book ai didi

c# - 如何使用 MSAL 在控制台应用程序中代表用户获取访问 token 以调用 MS Graph?

转载 作者:太空宇宙 更新时间:2023-11-03 11:59:59 24 4
gpt4 key购买 nike

我有一个 SPA 应用程序,它使用 AAD v2 身份验证与我的后端 Web API 通信。现在,我正在开发一个控制台应用程序,以代表登录 SPA 应用程序的用户调用 Microsoft Graph。

我有一个有效的用户访问 token (用于调用后端 Web API)。我想使用此访问 token 请求一个新 token 来访问 MS Graph。

这是控制台应用程序的代码,用于使用 MSAL.NET 请求具有 MS Graph 范围的新访问 token :

string clientId = "<clientId>";
string clientSecret = "<clientSecret>";
string accessToken = "<validAccessTokenForWebApi>";
string assertionType = "urn:ietf:params:oauth:grant-type:jwt-bearer";
string[] scopes = new string[] { "User.Read", "Mail.Send" };
string graphAccessToken = null;

try
{
var app = ConfidentialClientApplicationBuilder
.Create(clientId).WithClientSecret(clientSecret).Build();

var userAssertion = new UserAssertion(accessToken, assertionType);

var result = app.AcquireTokenOnBehalfOf(scopes, userAssertion)
.ExecuteAsync().GetAwaiter().GetResult();

graphAccessToken = result.AccessToken;
}
catch (MsalServiceException ex)
{
throw;
}

但是当我调用 app.AcquireTokenOnBehalfOf() 时,我得到一个异常:

AADSTS50013: Assertion failed signature validation. [Reason - The provided signature value did not match the expected signature value., Thumbprint of key used by client: 'BB839F3453C7C04068B078EDADAB8E6D5F382E76', Found key 'Start=06/04/2019 00:00:00, End=06/04/2021 00:00:00']

这是什么原因?代表用户获取访问 token 的正确方法是什么?

更新 - 为什么我需要控制台应用程序?

我可以直接从我的后端 API 调用图谱 API,但某些操作可能会被用户延迟(例如,在 30 分钟后使用图谱 API 发送邮件)。这就是为什么我需要使用按计划运行的控制台应用程序来执行此操作。

最佳答案

如果你想使用OAuth 2.0 On-Behalf-Of flow ,我认为你不需要开发一个控制台应用程序来调用图形api。您可以直接使用后端 Web API 应用程序获取访问 token ,然后调用 Microsoft Graph。按照我的理解,你只要做这几步就可以了

  1. 在客户端应用程序中登录用户
  2. 获取 Web API (TodoListService) 的 token 并调用
  3. Web API 然后调用另一个下游 Web API(Microsoft图)。

enter image description here

更多详情请引用sample .

关于如何在控制台应用程序中使用代表流程获取访问 token ,详细步骤如下。

注册网络 API 应用

  1. Register APP
  2. 创建客户 secret
  3. Configure permissions to access Graph API
  4. Configure an application to expose web APIs (为 api 添加范围)

注册 SAP 应用

  1. Register APP
  2. 创建客户 secret
  3. Configure permissions to access web API

为 Web API 应用程序配置已知的客户端应用程序

  1. 在 Azure 门户中,导航到您的 Web API应用程序注册并单击 list 部分。

  2. 找到属性 knownClientApplications 并添加 SAP 应用程序的客户端 ID

获取访问 token 以调用 web api

   GET https://login.microsoftonline.com/common/oauth2/v2.0/authorize
?scope=<you web api scope> openid
&redirect_uri=<your sap app redirect url>
&nonce=test123
&client_id=<you sap app client id>
&response_type=id_token token

通过代表流获取访问 token

休息API

POST https://login.microsoftonline.com/common/oauth2/v2.0/token
Content-Type: application/x-www-form-urlencoded

grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer
&client_id=<you web api client id>
&assertion=<you acess token you get in above steps>
&client_secret=<you app secret>
&scope=https://graph.microsoft.com/user.read
&requested_token_use=on_behalf_of

MSAL.net 代码

string[] scopes = { "user.read" };
string accesstoken = "";

string appKey = "yor web api client secret";
string clientId = "your web api application id";

var app = ConfidentialClientApplicationBuilder.Create(clientId)
.WithClientSecret(appKey)
.Build();
UserAssertion userAssertion = new UserAssertion(accesstoken,
"urn:ietf:params:oauth:grant-type:jwt-bearer");
var result = app.AcquireTokenOnBehalfOf(scopes, userAssertion).ExecuteAsync().Result;
Console.WriteLine(result.AccessToken);

关于c# - 如何使用 MSAL 在控制台应用程序中代表用户获取访问 token 以调用 MS Graph?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57480324/

24 4 0
文章推荐: c# - 从对象构建表达式时为 lambda 声明提供的参数数量不正确
文章推荐: python - Tensorflow:__new__() 在对象检测 API 中得到了一个意外的关键字参数 'serialized_options'
文章推荐: php - 为什么这个 PHP 代码不正确?
文章推荐: c# - 从 Razor 页面发送 List 到 Update 的行?