api - Azure AD - 公开 API 以使用应用程序权限使用它
问题描述
我正在开发自定义 API 和 Windows 服务。我想使用 Azure AD 在 api 和 windows 服务之间进行身份验证和授权。
我的问题是:如何公开API,以便我可以将api权限添加到“应用程序权限”类型的win服务?
当我想将api权限添加到win服务时,我只能选择“委托权限”。但我需要“应用程序权限”,因为 win 服务在没有用户的情况下运行。
谢谢转发!
最好的问候马蒂亚斯
好的。现在我知道如何在应用注册中设置清单。我还获得了一个不记名令牌,并且在不记名令牌中我可以看到(如果我对其进行 bas64 解码)Web API 的客户端 ID 以及应用程序角色:“roles”:[“User.Sync2”] 所以我认为令牌是正确的。
在第二步中,我使用身份验证“Bearor”和令牌调用 Web 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。
错误/错误在哪里?
谢谢转发!
解决方案
您需要添加应用角色。请按照如何:在您的应用程序中添加应用程序角色并在令牌中接收它们。App 角色allowedMemberTypes应包括Application。
推荐阅读
- mql5 - 在 MetaTrader 5 中为策略测试定义动态限制
- javascript - Electron TypeError: $(...).jstree 在我尝试使用它时不是一个函数
- ruby-on-rails - (localhost:27017) 上的服务器报告线路版本 (0),但此版本的 Ruby 驱动程序至少需要 (2)。)
- python - Selenium youtube 评论机器人,找不到 youtube 评论框
- python - Python 每 8 次迭代加入列表
- javascript - 迭代状态变量
- typescript - 来自无类型对象的打字稿索引类型
- reactjs - 在 React Navigation 中更新另一个屏幕的状态
- android - 使用 rxJava 和 MVP 的 GoogleMaps
- postgresql - Postgres:更新数组中的特定json对象