gpt4 book ai didi

c# - 在 Azure AAD 中创建架构扩展 Request_MultipleObjectsWithSameKeyValue

转载 作者:行者123 更新时间:2023-12-03 01:53:32 25 4
gpt4 key购买 nike

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

我的 Controller 上有 3 个操作,一个用于获取 APP(用于测试目的),另一个用于创建扩展和列出扩展。

问题是,当我尝试创建架构扩展时,我收到此错误:

{"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 的操作,它应该获取新属性,但列表是空的。

 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 属性引用它时,该属性始终返回一个空列表>app 实例与您用于添加属性的应用实例不同。您遇到的情况与我遇到的问题完全相同。

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

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

enter image description here

关于c# - 在 Azure AAD 中创建架构扩展 Request_MultipleObjectsWithSameKeyValue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30406756/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com