我目前正在尝试使用Graph Client Library Version 2.0与我的Azure Active Directory进行交互。我想为用户添加一个新属性,因此我需要使用Schema Extensions。我在此处遵循了教程: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
感谢任何帮助。
答案 0 :(得分:3)
所以我设法重现了这个问题。基本上,错误消息表示该服务找不到您要查找的扩展属性。您的代码的问题是您指定“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
希望这有帮助,