c# - 尝试激活图形 API 时无法解析类型 'Microsoft.Identity.Web.IT

.net core 2.2项目。我正在为我的 api 实现基于组的授权。我关注了https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/886f2ad2bf6add922c7998fb592e3d4088f9cf4e/5-WebApp-AuthZ/5-2-Groups来实现这一点。首先我添加了https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/886f2ad2bf6add922c7998fb592e3d4088f9cf4e/Microsoft.Identity.Web到我的项目。然后我开始在我的startup.cs中添加策略,如下所示。

services.AddAuthorization(options =>
            {
                options.AddPolicy("GroupsCheck", policy =>
                    policy.Requirements.Add(new GroupsCheckRequirement("2a39995a-8fd1-410e-99e2-11cf6046090d")));
            });

            services.AddScoped<IAuthorizationHandler, GroupsCheckHandler>();

然后我添加了GroupsCheckHandler.cs

public class GroupsCheckHandler : AuthorizationHandler<GroupsCheckRequirement>
    {
        private readonly ITokenAcquisition tokenAcquisition;
        private readonly IMSGraphService graphService;

        public GroupsCheckHandler(ITokenAcquisition tokenAcquisition, IMSGraphService MSGraphService)
        {
            this.tokenAcquisition = tokenAcquisition;
            this.graphService = MSGraphService;
        }
        protected override async Task HandleRequirementAsync(AuthorizationHandlerContext context,
                                                  GroupsCheckRequirement requirement)
        {
            string accessToken = await tokenAcquisition.GetAccessTokenOnBehalfOfUserAsync(new[] { Constants.ScopeUserRead, Constants.ScopeDirectoryReadAll });

            User me = await graphService.GetMeAsync(accessToken);

            IList<Group> groups = await graphService.GetMyMemberOfGroupsAsync(accessToken);

            var result = false;
            foreach (var group in groups)
            {
                if (requirement.groups.Equals(group.Id))
                {
                    result = true;
                }
            }

            if (result)
            {
                context.Succeed(requirement);
            }

        }
    }

然后我在 api 中添加了授权属性,如下所示。

[Authorize(Policy = "GroupsCheck")]
[Route("api/[controller]")]
[ApiController]

现在,每当我尝试访问任何 api 时,都会遇到异常。

System.InvalidOperationException: Unable to resolve service for type 'Microsoft.Identity.Web.ITokenAcquisition' while attempting to activate 'LocationServicesAPI.Services.GroupsRequirements.GroupsCheckHandler'.

有人可以帮我找出这个问题的根本原因吗,或者如果我做错了或者缺少任何配置,有人可以在这方面帮助我。任何帮助将不胜感激。谢谢

最佳答案

依赖项注入(inject)尝试实例化 ITokenAcquisition 实例但失败。这可能是由于 Startup.cs 中的配置存在问题。您需要确保在配置身份验证时调用 EnableTokenAcquisitionToCallDownstreamApiAddInMemoryTokenCaches

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddMicrosoftIdentityWebApi(Configuration, "AzureAd")
                .EnableTokenAcquisitionToCallDownstreamApi()
                .AddInMemoryTokenCaches();

关于c# - 尝试激活图形 API 时无法解析类型 'Microsoft.Identity.Web.ITokenAcquisition' 的服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59700833/

相关文章:

swiftui - 如何修复无法在 Swift 中找到文件 json

angular - 将 Angular 4 升级到 Angular 8

python - 为什么 randn 并不总是均值为 0 且方差为 1?

stripe-payments - 在 Stripe CLI 中使用 fixtures

xcode - SwiftUI:如何创建自定义 UIDatePicker?

c# - 如何在 ServiceStack 中返回不同的 Http Status Code

html - 容器中元素之间的空间

c# - C# 中输出参数的良好实践

python - 从 BigQuery 获取数据需要很长时间

django - 错误模块 'tablib.formats._xls' 没有属性 'title'