我正在尝试使用 ASP.Net Core 6 设置一个 Web api,以便用户可以到达我的端点,然后我使用特权帐户在幕后的 D365 中执行一些工作。我正在使用类型化的 HTTP 客户端,但我不确定如何插入承载身份验证,以便来自该客户端的所有请求都附加正确的授权标头。
程序.cs
builder.Services.AddHttpClient<D365Service>();
Run Code Online (Sandbox Code Playgroud)
D365Service.cs
private readonly HttpClient httpClient;
public D365Service(HttpClient httpClient)
{
this.httpClient = httpClient;
this.httpClient.DefaultRequestHeaders.Add("Accept", "*/*");
this.httpClient.DefaultRequestHeaders.Add("Accept-Encoding", "gzip, deflate, br");
// a whole bunch of other headers
// Is this where I can add in the bearer Authorization header? How do I generate that token?
}
Run Code Online (Sandbox Code Playgroud)
任何帮助表示赞赏。谢谢。
我正在尝试使用 ASP.Net Core 6 设置 Web api,以便用户可以到达我的端点,然后我使用特权帐户在 Teams 中执行一些工作。我认为我没有正确连接 DI 部分,因为在向 Teams 发出请求时出现以下错误:
MsalUiRequiredException: No account or login hint was passed to the AcquireTokenSilent call.
Run Code Online (Sandbox Code Playgroud)
一切设置方式如下:
程序.cs
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration, "AzureAd")
.EnableTokenAcquisitionToCallDownstreamApi()
.AddMicrosoftGraph(builder.Configuration.GetSection("Graph"))
.AddInMemoryTokenCaches();
Run Code Online (Sandbox Code Playgroud)
应用程序设置.json
{
"AzureAd": {
"Instance": "https://login.microsoftonline.com/",
"Domain": "<< domain >>",
"TenantId": " <<tenant id >>",
"ClientId": " << client id >>",
"ClientSecret": " << client secret >>"
},
"Graph": {
"BaseUrl": "https://graph.microsoft.com/v1.0",
"Scopes": "https://graph.microsoft.com/.default"
},
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft": "Warning",
"Microsoft.Hosting.Lifetime": "Information"
}
}, …Run Code Online (Sandbox Code Playgroud)