0

私は、Azure ADをMVCアプリケーションの認証サービスに使用しています。グラフAPIを使用して、ユーザーアカウントを正常に管理しています。私はAppRoleAssignmentをユーザーに追加しようとしています。Azure AD Add AppRoleAssignment

string tenantID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/tenantid").Value; 
string userObjectID = ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier").Value; 

Uri servicePointUri = new Uri(graphResourceID); 
Uri serviceRoot = new Uri(servicePointUri, tenantID); 
ActiveDirectoryClient activeDirectoryClient = new ActiveDirectoryClient(serviceRoot, async() => await GetTokenForApplication()); 

IUser user = new User(); 
user.JobTitle = "Tester"; 
user.DisplayName = "Test Tester"; 
user.Surname = "Tester"; 
user.GivenName = "Test"; 
user.UserPrincipalName = "[email protected]"; 
user.AccountEnabled = true; 
user.MailNickname = "ttester"; 
user.PasswordProfile = new PasswordProfile 
{ 
    Password = "XXXXX", 
    ForceChangePasswordNextLogin = true 
}; 
await activeDirectoryClient.Users.AddUserAsync(user); 

var appRoleAssignment = new AppRoleAssignment 
{ 
    Id = Guid.Parse("XXXXX"), 
    ResourceId = Guid.Parse("XXXXX"), 
    PrincipalType = "User", 
    PrincipalId = Guid.Parse(user.ObjectId) 
}; 

user.AppRoleAssignments.Add(appRoleAssignment); 
await user.UpdateAsync(); 

AppRoleAssignmentは決して作られません。それがコンストラクタ変数であるかどうかはわかりません。

idロールのIDをアプリケーションマニフェストで作成します。 ResourceId私はアプリケーションのObjectIdを配置しています。アプリケーションはAADディレクトリの下に作成されます。

コードは実際にはエラーなしで完了しますが、ユーザにはAppRoleAssignmentsではないことが表示されます。

最後に、アプリケーションロールを使用してRBACを実装しようとしています。

ご協力いただきまして誠にありがとうございます。

答えて

2

ユーザーにアプリケーションロールを割り当てるには、あなたがIUserFetcherUserオブジェクトをキャストする必要があります。

await ((IUserFetcher)user) 
    .AppRoleAssignments.AddAppRoleAssignmentAsync(appRoleAssignment); 

私も、私は試してみるつもりですServicePrincipal.ObjectId

var servicePrincipal = (await 
      activeDirectoryClient.ServicePrincipals.Where(
       s => s.DisplayName == "MyApplicationName").ExecuteAsync()).CurrentPage 
      .First(); 

var appRoleAssignment = new AppRoleAssignment 
{ 
    Id = Guid.Parse("XXXXX"), 
    // Service principal id go here 
    ResourceId = Guid.Parse(servicePrincipal.ObjectId), 
    PrincipalType = "User", 
    PrincipalId = Guid.Parse(user.ObjectId) 
}; 
+0

にRESOURCEIDを設定する必要がありましたこれはアウト。興味深いことに、 'User'オブジェクトを取得したときに' AppRoles'プロパティが設定されています。私のコードでは、実際にAppRoleに割り当てています。しかし、その後の検索でもプロパティはまだnullです。 –