【问题标题】:AAD API Role Based AuthenticationAAD API 基于角色的身份验证
【发布时间】:2019-10-14 10:24:43
【问题描述】:

我已成功创建了一个托管在 Azure 中并使用 AAD 持有者令牌身份验证进行保护的 Web API,以允许客户端应用程序(目前只是我构建的一个测试控制台应用程序)访问它。

要求最终客户端应用程序 (Sharepoint) 的用户将分为 2 个不同的组 - 其中一个将限制访问 API 的某些区域。

我的老板规定 API 应该处理所有身份验证,所以我需要换掉当前的 Azure Active Directory Bearer 身份验证中间件,并用(我认为)Open Id Connect 身份验证替换它。

我在制定解决方案时遇到了一些困难,因为我不太清楚这将如何/是否可行。我一直在查看the provided sample,但是我不知道如何使用它。在示例中,用户直接登录到站点,但在我的设置中他们没有登录到 API,他们登录到 Sharepoint,然后调用 - API 如何使用

[Authorize(Roles = "Admin")]

当它没有任何“登录用户”概念时的属性。

【问题讨论】:

    标签: azure active-directory azure-active-directory openid-connect azure-api-apps


    【解决方案1】:

    要调用 api,您需要提供包含权限的访问令牌。

    这里是代码 sn-p 供您参考。

    // Because we signed-in already in the WebApp, the userObjectId is know
                    string userObjectID = (User.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier"))?.Value;
    
                    // Using ADAL.Net, get a bearer token to access the TodoListService
                    AuthenticationContext authContext = new AuthenticationContext(AzureAdOptions.Settings.Authority, new NaiveSessionCache(userObjectID, HttpContext.Session));
                    ClientCredential credential = new ClientCredential(AzureAdOptions.Settings.ClientId, AzureAdOptions.Settings.ClientSecret);
                    result = await authContext.AcquireTokenSilentAsync(AzureAdOptions.Settings.TodoListResourceId, credential, new UserIdentifier(userObjectID, UserIdentifierType.UniqueId));
    
                    // Retrieve the user's To Do List.
                    HttpClient client = new HttpClient();
                    HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, AzureAdOptions.Settings.TodoListBaseAddress + "/api/todolist");
                    request.Headers.Authorization = new AuthenticationHeaderValue("Bearer", result.AccessToken);
                    HttpResponseMessage response = await client.SendAsync(request);
    

    参考:

    Call a web API in an ASP.NET Core web app using Azure AD

    How to: Add app roles in your application and receive them in the token

    Using groups vs using application roles for authorization in Azure AD apps

    【讨论】:

      猜你喜欢
      • 2016-01-03
      • 1970-01-01
      • 1970-01-01
      • 2021-11-22
      • 2019-10-10
      • 2017-08-03
      • 2016-03-07
      • 2014-04-04
      • 2013-11-07
      相关资源
      最近更新 更多