Azure AD Graph API 目录架构扩展
Azure AD Graph API Directory Schema Extensions
我目前正在尝试使用 Graph Client Library 2.0 版与我的 Azure Active Directory 进行交互。我想为用户添加一个新属性,所以我需要使用架构扩展。我按照这里的教程进行操作:http://justazure.com/azure-active-directory-part-6-schema-extensions, and example code here: 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。
我认为这可能是因为您复制了我们的示例,而完整的扩展名在内存中,因为在示例中我们刚刚创建了扩展,所以我们只是从那里使用它。我可能会将示例更改为 "construct" 完整的扩展名(按照上面的语法)。
有关目录架构扩展的更多信息,请参阅https://msdn.microsoft.com/en-us/library/azure/dn720459.aspx
希望这对您有所帮助,
我目前正在尝试使用 Graph Client Library 2.0 版与我的 Azure Active Directory 进行交互。我想为用户添加一个新属性,所以我需要使用架构扩展。我按照这里的教程进行操作:http://justazure.com/azure-active-directory-part-6-schema-extensions, and example code here: 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。
我认为这可能是因为您复制了我们的示例,而完整的扩展名在内存中,因为在示例中我们刚刚创建了扩展,所以我们只是从那里使用它。我可能会将示例更改为 "construct" 完整的扩展名(按照上面的语法)。
有关目录架构扩展的更多信息,请参阅https://msdn.microsoft.com/en-us/library/azure/dn720459.aspx
希望这对您有所帮助,