简体   繁体   English

尝试激活图形 API 时无法解析类型“Microsoft.Identity.Web.ITokenAcquisition”的服务

[英]Unable to resolve service for type 'Microsoft.Identity.Web.ITokenAcquisition' while attempting to activate graph api

net core 2.2 project. net core 2.2 项目。 I am implementing groups based authorization for my apis.我正在为我的 api 实施基于组的授权。 I followed https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/886f2ad2bf6add922c7998fb592e3d4088f9cf4e/5-WebApp-AuthZ/5-2-Groups to implement this.我按照https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/886f2ad2bf6add922c7998fb592e3d4088f9cf4e/5-WebApp-AuthZ/5-2-Groups来实现这一点。 First I added https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/886f2ad2bf6add922c7998fb592e3d4088f9cf4e/Microsoft.Identity.Web to my project.首先,我将https://github.com/Azure-Samples/active-directory-aspnetcore-webapp-openidconnect-v2/tree/886f2ad2bf6add922c7998fb592e3d4088f9cf4e/Microsoft.Identity.Web添加到我的项目中。 Then I started adding policy in my startup.cs as below.然后我开始在我的 startup.cs 中添加策略,如下所示。

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

            services.AddScoped<IAuthorizationHandler, GroupsCheckHandler>();

Then I added GroupsCheckHandler.cs然后我添加了 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);
            }

        }
    }

Then I added authorize attribute in api as below.然后我在 api 中添加了授权属性,如下所示。

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

Now when ever I try to access any api I am getting below exception.现在,每当我尝试访问任何 api 时,我都会遇到异常。

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

Can someone help me to figure out what might be the root cause of this issue or If I have done it wrongly or any configurations missing can someone help me in this regard.有人可以帮助我找出可能是此问题的根本原因,或者如果我做错了或缺少任何配置,有人可以在这方面帮助我。 Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks谢谢

The dependency injection is trying to instantiate an instance of ITokenAcquisition and failing.依赖注入正在尝试实例化 ITokenAcquisition 的实例并失败。 This could be because of an issue with the configuration in Startup.cs.这可能是因为 Startup.cs 中的配置存在问题。 You need to ensure you have the calls to EnableTokenAcquisitionToCallDownstreamApi and AddInMemoryTokenCaches while configuring Authentication.您需要确保在配置身份验证时调用EnableTokenAcquisitionToCallDownstreamApiAddInMemoryTokenCaches

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

You might have missed these lines on you Startup.cs :您可能错过了Startup.cs上的这些行:

services.AddSignIn(Configuration);

services.AddWebAppCallsProtectedWebApi(Configuration, new string[] { "YourApiScope" })
   .AddInMemoryTokenCaches(); // Or .AddDistributedTokenCaches(); depending on which token cache provider from Microsoft.Identity.Web you would like to use.

The DI registration for ITokenAcquisition happens inside AddWebAppCallsProtectedWebApi method, on Microsoft.Identity.Web. ITokenAcquisition的 DI 注册发生在 Microsoft.Identity.Web 上的AddWebAppCallsProtectedWebApi方法中。

我有一个几乎与使用 Blazor 类似的错误,并通过将 services.AddScoped() 更改为 services.AddHttpClient() 来解决它

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试激活“Management.Controllers.RoleController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'Management.Controllers.RoleController' 尝试激活“xxxxx.LoginModel”时无法解析“Microsoft.AspNetCore.Identity.SignInManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.SignInManager` while attempting to activate 'xxxxx.LoginModel' 尝试激活“AuthenticateController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthenticateController' 尝试激活“AuthController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager` while attempting to activate 'AuthController' 尝试激活时无法解析服务类型 - Unable to resolve service for type while attempting to activate 尝试激活“WebShop.Controllers.User.UserController”时无法解析“Microsoft.AspNetCore.Identity.UserManager”类型的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager' while attempting to activate 'WebShop.Controllers.User.UserController' 尝试激活“”时无法解析“”类型的服务 - Unable to resolve service for type '' while attempting to activate '' 尝试激活“ Controllers.AccountsController”时无法解析类型为“ Microsoft.AspNetCore.Identity.UserManager”的服务 - Unable to resolve service for type 'Microsoft.AspNetCore.Identity.UserManager hile attempting to activate 'Controllers.AccountsController' 尝试激活 Identity.IdentityUserManager 时无法解析 Identity.IdentityUserStore 类型的服务 - Unable to resolve service for type Identity.IdentityUserStore while attempting to activate Identity.IdentityUserManager 尝试激活“TheWorld.Startup”时无法解析“Microsoft.Extensions.Configuration.IConfigurationRoot”类型的服务 - Unable to resolve service for type 'Microsoft.Extensions.Configuration.IConfigurationRoot' while attempting to activate 'TheWorld.Startup'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM