gpt4 book ai didi

Azure AD Graph API 目录架构扩展

转载 作者:行者123 更新时间:2023-12-02 07:50:27 25 4
gpt4 key购买 nike

我目前正在尝试使用 Graph 客户端库版本 2.0 与我的 Azure Active Directory 进行交互。我想向用户添加新属性,因此我需要使用架构扩展。我按照这里的教程:http://justazure.com/azure-active-directory-part-6-schema-extensions ,示例代码如下:https://github.com/AzureADSamples/ConsoleApp-GraphAPI-DotNet .

我的代码:

public async Task setAccountNumber(string UserName, String AccountNumber)
{
////Register an Extension Property in Azure AD which is the account number
////Define the account number property
//ExtensionProperty accountNumber = new ExtensionProperty()
//{
// Name = "AccountNumber",
// DataType = "String",
// TargetObjects = { "User" }
//};

//// Get a reference to our application
//Microsoft.Azure.ActiveDirectory.GraphClient.Application app =
// (Microsoft.Azure.ActiveDirectory.GraphClient.Application)activeDirectoryClient.Applications.Where(
// a => a.AppId == Constants.ClientId).ExecuteSingleAsync().Result;

//// Register the extension property
//app.ExtensionProperties.Add(accountNumber);
//await app.UpdateAsync();


ExtensionProperty accountNumber = new ExtensionProperty()
{
Name = "AccountNumber",
DataType = "String",
TargetObjects = { "User" }
};

//Get the user
User user = (User)activeDirectoryClient.Users.Where(u => u.UserPrincipalName.Equals(
UserName, StringComparison.CurrentCultureIgnoreCase)).ExecuteSingleAsync().Result;

//Set the Account Number property
if (user != null)
{
user.SetExtendedProperty(accountNumber.Name, AccountNumber);
await user.UpdateAsync();
// Save the extended property value to Azure AD.
//user.GetContext().SaveChanges();
}

// Retrieve the extension property value for a user for test use only!
IReadOnlyDictionary<string, object> extendedProperties = user.GetExtendedProperties();
object extendedProperty = extendedProperties["AccountNumber"];
}

我使用注释行创建了一个新的扩展属性,即用户的帐号。我使用以下代码找到一个新用户并设置该用户的帐号。

但是,我收到了这个错误:

{"odata.error": {
"code":"Request_BadRequest",
"message": {
"lang":"en",
"value":"One or more extension property values specified are invalid."
},
"values":null}}

行:await user.UpdateAsync();

堆栈跟踪:

[DataServiceClientException: {"odata.error":{"code":"Request_BadRequest","message":{"lang":"en","value":"One or more extension property values specified are invalid."},"values":null}}]

[DataServiceRequestException: An error occurred while processing this request.]
System.Data.Services.Client.SaveResult.HandleResponse() +1038
System.Data.Services.Client.BaseSaveResult.EndRequest() +262
System.Data.Services.Client.DataServiceContext.EndSaveChanges(IAsyncResult asyncResult) +121
System.Threading.Tasks.TaskFactory`1.FromAsyncCoreLogic(IAsyncResult iar, Func`2 endFunction, Action`1 endAction, Task`1 promise, Boolean requiresSynchronization) +99
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144


System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
System.Runtime.CompilerServices.TaskAwaiter`1.GetResult() +49
Microsoft.Azure.ActiveDirectory.GraphClient.Extensions.<SaveChangesAsync>d__74.MoveNext() +1694
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
DataIngestionAPI.Managers.<setAccountNumber>d__c.MoveNext() in c:\Users\Jason\Source\Repos\UCDavis Water\UCDavisResearchPlatform\DataIngestionAPI\Managers\AzureADManager.cs:107
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
DataIngestionAPI.Controllers.<Registration>d__6.MoveNext() in c:\Users\Jason\Source\Repos\UCDavis Water\UCDavisResearchPlatform\DataIngestionAPI\Controllers\HomeController.cs:123
System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task) +144
System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task) +84
System.Threading.Tasks.TaskHelpersExtensions.ThrowIfFaulted(Task task) +89
System.Web.Mvc.Async.TaskAsyncActionDescriptor.EndExecute(IAsyncResult asyncResult) +110
System.Web.Mvc.Async.<>c__DisplayClass37.<BeginInvokeAsynchronousActionMethod>b__36(IAsyncResult asyncResult) +92
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +141
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethod(IAsyncResult asyncResult) +65
System.Web.Mvc.Async.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() +189
System.Web.Mvc.Async.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() +717
System.Web.Mvc.Async.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) +66
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +141
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeActionMethodWithFilters(IAsyncResult asyncResult) +65
System.Web.Mvc.Async.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() +103
System.Web.Mvc.Async.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) +330
System.Web.Mvc.Async.WrappedAsyncResult`1.CallEndDelegate(IAsyncResult asyncResult) +71
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncControllerActionInvoker.EndInvokeAction(IAsyncResult asyncResult) +58
System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +90
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +188
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +68
System.Web.Mvc.Controller.<BeginExecute>b__15(IAsyncResult asyncResult, Controller controller) +60
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +85
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
System.Web.Mvc.Controller.EndExecute(IAsyncResult asyncResult) +58
System.Web.Mvc.Controller.System.Web.Mvc.Async.IAsyncController.EndExecute(IAsyncResult asyncResult) +60
System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +93
System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +188
System.Web.Mvc.Async.WrappedAsyncResultBase`1.End() +196
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +73
System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +50
System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +58
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +59
System.Web.CallHandlerExecutionStep.OnAsyncHandlerCompletion(IAsyncResult ar) +282

感谢任何帮助。

最佳答案

所以我设法重现了这个问题。基本上,错误消息意味着该服务找不到您正在查找的扩展属性。您的代码的问题是您指定“AccountNumber”作为扩展名。但是,扩展程序的格式为“extension_appId_extensionName”,以确保扩展程序名称的唯一性。 注意:appId这里是您的应用程序的appId(又名clientId)GUID,但没有破折号 - 即 xxxx-xxx-xxx 变为 xxxxxxxxxx。

因此,最简单的方法是使用上面的语法构造扩展属性名称 - 在您的情况下,它将是“extension_appId_AccountName”,在此处替换您应用程序的 appId/clientId。

我认为这可能是因为您复制了我们的示例而发生的,其中完整的扩展名称位于内存中,因为在示例中我们刚刚创建了扩展,所以我们只是从那里使用它。我可能会更改示例以“构造”完整的扩展名(按照上面的语法)。

此外,有关目录架构扩展的更多信息,请参阅 https://msdn.microsoft.com/en-us/library/azure/dn720459.aspx

希望这有帮助,

关于Azure AD Graph API 目录架构扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31619774/

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