Mic*_*ala 5 c# asp.net-core-webapi asp.net-core-6.0
我正在尝试使用 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"
}
},
"AllowedHosts": "*"
}
Run Code Online (Sandbox Code Playgroud)
团队.cs
namespace GraphApiService.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class Teams : ControllerBase
{
private readonly GraphServiceClient _graphServiceClient;
public Teams(GraphServiceClient graphServiceClient)
{
_graphServiceClient = graphServiceClient;
}
// GET: api/<Teams>
[HttpGet]
public IEnumerable<string> Get()
{
//Throws an error here!
var teams = _graphServiceClient.Teams[<< team id >>].Request().GetAsync().Result;
return new string[] { "value1", "value2" };
}
}
}
Run Code Online (Sandbox Code Playgroud)
现在,如果我跳过 DI 部分并仅在构造函数中设置客户端,它就可以正常工作。
public Teams()
{
var scopes = new[] { "https://graph.microsoft.com/.default" };
var tenantId = " << tenand id >>";
var clientId = "<< client id >>";
var clientSecret = "<<client secret>>";
var options = new TokenCredentialOptions
{
AuthorityHost = AzureAuthorityHosts.AzurePublicCloud
};
var clientSecretCredential = new ClientSecretCredential(
tenantId, clientId, clientSecret, options);
_graphServiceClient = new GraphServiceClient(clientSecretCredential, scopes);
}
Run Code Online (Sandbox Code Playgroud)
任何帮助,将不胜感激。谢谢!
嗯,这种事总是会发生。花两天时间寻找答案。发布到论坛,几个小时后即可找到答案。我通过使用“WithAppOnly()”让它工作,如下所示:
var teams = _graphServiceClient.Teams[teamId].Request().WithAppOnly().GetAsync().Result;
Run Code Online (Sandbox Code Playgroud)
所有在线示例都使用委托的用户权限,因此很难找到有关如何处理应用程序权限的任何信息。
| 归档时间: |
|
| 查看次数: |
3796 次 |
| 最近记录: |