gpt4 book ai didi

asp.net-web-api - 通过 Azure 应用程序注册的 WebAPI 范围授权

转载 作者:行者123 更新时间:2023-12-02 02:54:40 24 4
gpt4 key购买 nike

我正在尝试通过 Azure 中的应用注册来控制授权。

现在,我设置了两个应用程序注册。

  • 应用程序
  • 客户端应用

ApiApp 是使用默认设置设置的,但我已将其添加到 list 中:

"oauth2Permissions": [
{
"adminConsentDescription": "Allow admin access to ApiApp",
"adminConsentDisplayName": "Admin",
"id": "<guid>",
"isEnabled": true,
"type": "User",
"userConsentDescription": "Allow admin access to ApiApp",
"userConsentDisplayName": "Admin",
"value": "Admin"
},
...
]

在客户端应用程序注册中,我有所有默认值,但我添加了:

  1. 在 key 中,用于针对 AD 验证应用程序的密码
  2. 在所需权限中,我添加了 ApiApp 并需要委派权限“管理员”。我保存了它,单击完成,然后单击“授予权限”以确保权限具有强制更新。

在我的客户端应用程序中,它使用此代码进行身份验证:

...
var context = new AuthenticationContext(authority);
var clientCredentials = new ClientCredential(<clientId>, <clientSecret>);
var result = await context.AcquireTokenAsync(<apiAppUri>, clientCredentials);
var client = new HttpClient();
client.DefaultRequestHeaders.Authorization = new System.Net.Http.Headers.AuthenticationHeaderValue("Bearer", result.AccessToken);
var webResult = await client.GetAsync(<api uri>);

如果您在创建 Web API 项目时选择工作或学校帐户,我的 ApiApp 仅使用内置授权:

public void ConfigureAuth(IAppBuilder app)
{
app.UseWindowsAzureActiveDirectoryBearerAuthentication(
new WindowsAzureActiveDirectoryBearerAuthenticationOptions
{
Tenant = ConfigurationManager.AppSettings["ida:Tenant"],
TokenValidationParameters = new TokenValidationParameters {
ValidAudience = ConfigurationManager.AppSettings["ida:Audience"]
},
});
}

这个有效:

[Authorize]
public class ValuesController : ApiController

这些不起作用:

[Authorize(Users = "Admin")]
public class ValuesController : ApiController

[Authorize(Roles= "Admin")]
public class ValuesController : ApiController

根据我正在阅读的内容,我相信除了 ApiApp 项目本身之外,我已经正确设置了所有内容。我认为我需要以不同方式或使用额外信息设置授权,以允许 oauth2Permission 范围正确用于 WebAPI。

我缺少哪些步骤来允许 WebAPI 中的特定范围而不仅仅是 [Authorize] 属性?

我用了Integrating applications with Azure Active Directory帮助我设置应用程序注册,以及 Service to service calls using client credentials ,但我似乎无法准确找到在 Web API 部分中实现代码所需的内容。

更新

我找到了这个资源:Azure AD .NET Web API getting started

它表明您可以使用此代码来检查范围声明:

public IEnumerable<TodoItem> Get()
{
// user_impersonation is the default permission exposed by applications in Azure AD
if (ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/scope")
.Value != "user_impersonation")
{
throw new HttpResponseException(new HttpResponseMessage {
StatusCode = HttpStatusCode.Unauthorized,
ReasonPhrase = "The Scope claim does not contain 'user_impersonation' or scope claim not found"
});
}
...
}

但是,我得到的声明不包括任何范围声明。

最佳答案

您必须为应用程序使用 appRoles,为代表用户的应用程序使用范围。

根据 GianlucaBertelli 在 Azure AD, Scope-based authorization 的评论中:

...in the Service 2 Service scenario, using the client credentials flow you won’t get the SCP field. As you are not impersonating any user, but the calling App (you are providing a fixed credential set). In this case you need to use AppRoles (so Application permissions, not delegated) that results in a different claim. Check a great how-to and explanation here: https://joonasw.net/view/defining-permissions-and-roles-in-aad.

在他提供的链接中,它讨论了应用程序 list 中的 appRoles。

我之前查看的资源背后的意图是允许用户登录到客户端应用程序,然后客户端应用程序代表用户对 API 进行身份验证。这不是我尝试使用的功能——只是为了让客户端应用程序能够对 API 进行身份验证和授权。

为此,您必须使用 appRoles,它在应用程序 list 中如下所示:

{
"appRoles": [
{
"allowedMemberTypes": [
"Application"
],
"displayName": "Read all todo items",
"id": "f8d39977-e31e-460b-b92c-9bef51d14f98",
"isEnabled": true,
"description": "Allow the application to read all todo items as itself.",
"value": "Todo.Read.All"
}
]
}

当您为客户端应用程序设置所需的权限时,您选择的是应用程序权限而不是委托(delegate)权限。

请求权限后,确保单击“授予权限”按钮。要授予应用程序权限,需要 Azure Active Directory 管理员。

完成此操作后,请求访问 token 作为客户端应用程序将为您提供 token 中的“角色”声明,这将是一个字符串值集合,指示应用程序拥有哪些角色。

关于asp.net-web-api - 通过 Azure 应用程序注册的 WebAPI 范围授权,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50122817/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com