我正试图通过Azure中的应用注册来控制授权。
现在,我已经设置了两个应用注册。
ApiApp使用默认设置进行设置,但我已将其添加到清单中:
"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"
},
...
]
在客户端应用注册中,我有所有默认值,但我添加了:
在我的客户端应用中,它使用此代码进行身份验证:
...
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"
});
}
...
}
但是,我得到的索赔不包括任何范围索赔。
答案 0 :(得分:1)
您必须将appRoles用于应用程序,并为代表用户的应用程序使用范围。
根据GianlucaBertelli的评论Azure AD, Scope-based authorization:
...在Service 2服务方案中,使用客户端凭据流您将无法获得SCP字段。因为您没有模仿任何用户,而是调用应用程序(您提供的是固定凭据集)。 在这种情况下,您需要使用AppRoles(因此应用程序权限,而不是委派权限)导致不同的声明。在这里查看精彩的操作方法和解释:https://joonasw.net/view/defining-permissions-and-roles-in-aad。
在他提供的链接中,它讨论了应用程序清单中的appRoles。
我之前看到的资源背后的意图是允许用户登录到客户端应用程序,然后客户端应用程序代表用户对API进行身份验证。这不是我试图使用的功能 - 只是为了使客户端应用程序能够进行身份验证并获得API的授权。
要实现这一点,您必须使用appRoles,它在应用程序清单中显示如下:
{
"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"
}
]
}
为客户端应用程序设置所需权限时,选择应用程序权限而不是委派权限。
在要求权限后,请务必单击“授予权限”按钮。要授予应用程序权限,它需要Azure Active Directory管理员。
完成此操作后,请求访问令牌作为客户端应用程序将在令牌中为您提供“角色”声明,该声明将是一个字符串值的集合,指示应用程序保留哪些角色。