Если вы хотите реализовать Azure AD аутентификацию на основе роли приложения, выполните следующие действия:
Определите роли приложения
a. В колонке для вашего приложения на портале Azure нажмите Manifest .
b. Отредактируйте манифест, найдя параметр appRoles и добавив свои роли приложения. Определения ролей подобны следующим json.
"appRoles": [
{
"allowedMemberTypes": [
"User"
],
"description": "User readers can read basic profiles of all users in the directory",
"displayName": "UserReaders",
"id": "a816142a-2e8e-46c4-9997-f984faccb625",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "UserReaders"
},
{
"allowedMemberTypes": [
"User"
],
"description": "Directory viewers can view objects in the whole directory.",
"displayName": "DirectoryViewers",
"id": "72ff9f52-8011-49e0-a4f4-cc1bb26206fa",
"isEnabled": true,
"lang": null,
"origin": "Application",
"value": "DirectoryViewers"
}
],
c. Сохраните манифест.
назначьте роль приложения пользователю или группе
Кроме того, обратите внимание, что если вы хотите назначить роль приложения группе, вам необходимо иметь Azure лицензию AD Premium.
Код
a. Настройте приложение для получения заявки на роль, добавьте следующий код в startup.cs
public void ConfigureServices(IServiceCollection services)
{
// This flag ensures that the ClaimsIdentity claims collection will be built from the claims in the token
JwtSecurityTokenHandler.DefaultMapInboundClaims = false;
// The following lines code instruct the asp.net core middleware to use the data in the "roles" claim in the Authorize attribute and User.IsInrole()
services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
{
// The claim in the Jwt token where App roles are available.
options.TokenValidationParameters.RoleClaimType = "roles";
});
}
b. получить заявку на роль
[Authorize(Roles = <your role>")] // In controllers
// or
User.IsInRole("<your role>"); // In methods
Для получения более подробной информации см. документ и образец