在 Azure AAD 中创建架构扩展 Request_MultipleObjectsWithSameKeyValue

Create schema extensions in Azure AAD Request_MultipleObjectsWithSameKeyValue

我需要在 Azure AAD 中为我的应用程序创建自定义属性。 该代码基于 this 博客系列。

我的控制器上有 3 个操作,一个是获取应用程序(用于测试目的),另一个是创建扩展和列出扩展。

问题是当我尝试创建架构扩展时出现此错误:

{"odata.error":{"code":"Request_MultipleObjectsWithSameKeyValue","message":{"lang":"en","value":"An extension property exists with the name extension_33e037a7b1aa42ab96936c22d01ca338_CompInfo."}}} Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.Services.Client.DataServiceClientException: {"odata.error":{"code":"Request_MultipleObjectsWithSameKeyValue","message":{"lang":"en","value":"An extension property exists with the name extension_33e037a7b1aa42ab96936c22d01ca338_CompInfo."}}}

Source Error:

Line 72: Line 73: // Apply the change to Azure AD Line 74: app.GetContext().SaveChanges();

代码如下,这里你还可以看到一个叫做GetProperties的action,它应该获取新的属性,但是列表是EMPTY。

 public ActionResult CreateProperty()
        {
            // Create the extension property
            string extPropertyName = "CompInfo";
            ExtensionProperty extensionProperty = new ExtensionProperty()
            {
                Name = extPropertyName,
                DataType = "String",
                TargetObjects = { "User" }
            };
            Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);

            ActiveDirectoryClient adClient = new ActiveDirectoryClient(
             serviceRoot,
             async () => await GetAppTokenAsync());

            Application app =(Application)adClient.Applications.Where(
                a => a.AppId == clientId).ExecuteSingleAsync().Result;
            if (app == null)
            {
                throw new ApplicationException("Unable to get a reference to application in Azure AD.");
            }

            // Register the extension property
            app.ExtensionProperties.Add(extensionProperty);
            app.UpdateAsync();
            Task.WaitAll();

            // Apply the change to Azure AD
            app.GetContext().SaveChanges();
            ViewBag.Message = "Extension Created.";

            return View();
        }

        public ActionResult GetProperties()
        {
            Uri serviceRoot = new Uri(azureAdGraphApiEndPoint);

            ActiveDirectoryClient adClient = new ActiveDirectoryClient(
             serviceRoot,
             async () => await GetAppTokenAsync());

            Application app = (Application)adClient.Applications.Where(
                a => a.AppId == clientId).ExecuteSingleAsync().Result;
            if (app == null)
            {
                throw new ApplicationException("Unable to get a reference to application in Azure AD.");
            }

            IEnumerable<IExtensionProperty> appExtProperties = app.ExtensionProperties;
            appExtProperties.ToList();

            return View(appExtProperties);
        }

错误消息是正确的,也就是说,您的扩展 属性 确实存在于您的目录中。问题出在您的 GetProperties 方法中。

当我撰写博客时,Graph 客户端库出现问题,显然问题仍然存在,即 ExtensionProperties 属性 始终 returns 当您从 app 实例引用空列表时,该实例不同于您用于添加 属性 的应用程序实例。您 运行 遇到的情况与我 运行 遇到的问题完全相同。

我在博客中解决了这个问题并提供了解决方法。解决方法是使用 ActiveDirectoryClient 实例而不是应用程序 属性 查找扩展 属性。这需要了解 属性 如何存储在 Azure AD 中,但我也解决了这个问题。这是描述解决方法的博客部分。

顺便说一句,感谢您阅读博客! :)