1

我正在寻找使用 Azure AD 创建基于角色的授权 mvc 应用程序:

从 Azure 门户,我能够:

  • 创建用户和组。
  • 将用户分配到组。
  • 创建应用程序角色。
  • 创建应用程序角色(通过修改清单)
  • 将应用程序角色分配给用户。

我刚刚获得了一个免费的 Azure Active Directory 版本,并且我读到我们可以使用Microsoft Azure Active Directory来执行这些操作:

  • 为用户分配多个应用程序角色。
  • 将多个应用程序角色分配给组。

Microsoft 提供了很好的示例来查询 AAD,我已经开始使用它,但我不知道如何将应用程序分配给组。

这是我获取组的伪代码:

ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
var app = (await client.Applications.GetByObjectId("applicationObjectId").ExecuteAsync());
var servicePrincipal = await client.ServicePrincipals.GetByObjectId("servicePrincipalObjectId").ExecuteAsync();
var appRole = app.AppRoles.First(r => r.DisplayName == "my role");
var mygroup = (await client.Groups.ExecuteAsync()).CurrentPage.FirstOrDefault();           

我想做的是这样的:

mygroup .AppRoleAssignments.Add(new AppRoleAssignment()
{
    ResourceId = Guid.Parse(servicePrincipal.ObjectId),
    Id = appRole.Id,
    PrincipalType = "Group",
    PrincipalId = Guid.Parse(mygroup .ObjectId),
});
await group.UpdateAsync();

但是 AppRoleAssignments 的类型是IPagedCollection<IAppRoleAssignment>并且没有 Add 方法。

有谁知道我需要在我的代码中修改什么?

4

1 回答 1

2

事实上这很简单......我不得不将 IGroup 转换为Group

ActiveDirectoryClient client = AuthenticationHelper.GetActiveDirectoryClient();
var app = (await client.Applications.GetByObjectId("applicationObjectId").ExecuteAsync());
var servicePrincipal = await client.ServicePrincipals.GetByObjectId("servicePrincipalObjectId").ExecuteAsync();
var appRole = app.AppRoles.First(r => r.DisplayName == "my role");
var mygroup = (Group)(await client.Groups.ExecuteAsync()).CurrentPage.FirstOrDefault();  

它工作正常^^:

mygroup .AppRoleAssignments.Add(new AppRoleAssignment()
{
    ResourceId = Guid.Parse(servicePrincipal.ObjectId),
    Id = appRole.Id,
    PrincipalType = "Group",
    PrincipalId = Guid.Parse(mygroup .ObjectId),
});
await group.UpdateAsync();
于 2016-01-17T23:00:06.377 回答