Azure AD-公开API以通过应用程序权限使用它

时间:2020-11-03 12:35:49

标签: api azure-active-directory

我正在开发自定义API和Windows服务。我想使用Azure AD在api和Windows服务之间进行身份验证和授权。

我的问题专家:如何公开API,以便可以将类型为“应用程序权限”的api权限添加到win服务?

当我要将api权限添加到win服务时,我只能选择“代理权限”。但是我需要“应用程序许可”,因为Win服务在没有用户的情况下运行。

谢谢你前进!

最好的问候 马赛厄斯


好的。现在,我知道了如何在应用程序注册中设置清单。我还获得了一个承载者令牌,在承载者令牌中,我可以看到(如果我对其进行bas64解码)Web API的客户端ID以及应用程序角色:“ roles”:[“ User.Sync2”] 所以我认为令牌是正确的。

在第二步中,我使用身份验证“载体”和令牌调用We​​b API(https:// localhost:44358 / api / AzureADB2C / Ping)。但是然后我收到401。(我没有在Web API的应用程序注册中注册任何平台,因此也没有重定向URI。但是我认为我不需要它吗?)

这是我的Web API项目的Startup.cs(使用Visual Studio标准生成):

using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Authentication.AzureAD.UI;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

namespace AzureADB2CConnect.API
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddAuthentication(AzureADDefaults.BearerAuthenticationScheme)
                .AddAzureADBearer(options => Configuration.Bind("AzureAd", options));

            services.AddControllers();
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseHttpsRedirection();

            app.UseRouting();

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}

这是我的API控制器:

using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace AzureADB2CConnect.API.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    public class AzureADB2CController : ControllerBase
    {
        [HttpGet("Ping")]
        [Authorize(Roles = "User.Sync2")]
        //[Authorize]
        public async void GetPing()
        {
            //foreach(Claim claim in ClaimsPrincipal.Current.Claims)
            //{

            //}
        }
    }
}

如果删除“授权”标签,则可以调用API。如果我只使用Authorize或Authorize(Roles =“ User.Sync2”),那我总是收到401。

错误/错误在哪里?

谢谢你前进!


这是解码的承载令牌: enter image description here


这就是我调用GET方法获取令牌的方式: enter image description here

2 个答案:

答案 0 :(得分:0)

您需要添加应用角色。请遵循How to: Add app roles in your application and receive them in the token。应用角色 allowedMemberTypes 应该包含 Application

答案 1 :(得分:0)

我可以重现您的问题。验证令牌时输入了错误的参数。您需要将Authorized更改为Authorization,将Bearor更改为Bearer

enter image description here

相关问题