我在 Web API 控制器中有以下代码,其中一些用于调试目的:
public void Register([FromBody]AccountInfo accountInfo)
{
User user = new User();
user.accountEnabled = true;
user.displayName = accountInfo.UserName;
user.userPrincipalName = string.Format(CultureInfo.InvariantCulture, "{0}@{1}", user.displayName, ConfigurationManager.AppSettings["TenantDomainName"]);
user.mailNickname = accountInfo.UserName;
user.passwordProfile.password = accountInfo.Password;
user.passwordProfile.forceChangePasswordNextLogin = false;
DirectoryService.AddTousers(user);
DirectoryService.SaveChanges();
Group securityGroup = DirectoryService.groups.Where(u => u.displayName.Equals(accountInfo.SecurityGroup)).First();
User addedUser = DirectoryService.users.Where(u => u.objectId.Equals(user.objectId)).First();
DirectoryService.LoadProperty(addedUser, "memberOf");
DirectoryService.LoadProperty(securityGroup, "members");
DirectoryService.AddLink(securityGroup, "members", addedUser);
//securityGroup.members.Add(addedUser);
var users = securityGroup.members.ToList();
var links = DirectoryService.Links;
//DirectoryService.UpdateObject(securityGroup);
DirectoryService.SaveChanges();
}
上面的代码在 Windows Azure Active Directory 中添加了一个用户,然后尝试将该用户添加到一个组中。用户在没有任何故障的情况下被添加到 WAAD,但代码在最后一行失败并显示
“http://schemas.microsoft.com/ado/2007/08/dataservices/metadata\">Authorization_RequestDenied 权限不足,无法完成操作。”
错误信息。它不允许我将用户添加到组中。既然我的服务主体有足够的权限将用户添加到 AAD,为什么不能将用户添加到现有组?