【问题标题】:Azure AD Graph API Directory Schema ExtensionsAzure AD Graph API 目录架构扩展
【发布时间】:2015-07-28 04:53:45
【问题描述】:

我目前正在尝试使用 Graph 客户端库版本 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

感谢任何帮助。

【问题讨论】:

  • 您使用的是哪个版本的 Graph API 客户端库?同样在“user.SetExtendedProperty(accountNumber.Name, AccountNumber);”行中什么是“AccountNumber” - 它是一个字符串吗?
  • 感谢您的回复,我正在使用 Microsoft Azure AD Graph API 的 NET 客户端库,版本 2.1.0。
  • 是的,AccountNumber 是一个字符串,我尝试了字符串和字符串。这行不通。
  • 谢谢。我无法重现。将跟进并回复您。

标签: azure azure-active-directory


【解决方案1】:

所以我设法重现了这个问题。基本上,错误消息意味着服务找不到您正在寻找的扩展属性。您的代码的问题是您将“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

希望对你有帮助,

【讨论】:

  • 谢谢!这样可行!还有一件事我要指出的是,需要删除客户端 ID 中的破折号。例如,如果您从管理门户复制的clientID是xxx-xxx-xxx,则需要将其更改为xxxxxxxxx。
  • 是的 - 很好 - 谢谢。将更新答案,并附上架构扩展文档的链接。
  • 我创建了一个自定义属性并将其映射到声明中的Signup or Sign-in policy。尝试使用此客户属性创建新用户时,我遇到了同样的错误。 {"extension_Role","Admin"} .. 即使这个自定义属性存在,我还要创建这个扩展吗?
  • Venky 可以为此提出一个新问题吗?看起来您也是在 B2C 租户中执行此操作,因此请添加 [azure-ad-b2c] 标签。
猜你喜欢
  • 2018-03-11
  • 2023-04-02
  • 1970-01-01
  • 1970-01-01
  • 2022-10-08
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多