c# - 如何在 ASP.NET MVC 中获取/设置自定义 Azure Active Directory B2C 用户属性?

标签 c# azure-active-directory microsoft-graph-api azure-ad-b2c

我添加了自定义 Organization字段作为我的 Azure Active Directory B2C 租户中的用户属性,如下所示:

enter image description here

我正在使用 Microsoft Graph .NET Client Library管理 Azure Active Directory B2C 中的用户,并想使用类似于以下代码的内容来设置用户的自定义 Organization字段和用户的内置 Email Addresses field 。

await graphClient.Users[user.Id].Request().UpdateAsync(new User()
{
    Email Addresses = new StringCollection("myemail@mydomain.com")
    Organization = "Microsoft"
});

两个问题:
  • 如何设置内置字段,例如 Email Addresses ?
  • 如何设置自定义字段,例如 Organization ?

  • 本文档显示 how to create a custom attribute但没有说明如何使用 Graph Client 访问或使用该属性。

    本文档展示了如何 create custom attributes and edit the Relying Party (RP) file .

    有更容易的方法吗?什么是graphClient然后获取/设置这些自定义用户属性的代码?

    最佳答案

    Microsoft Graph API 以及 Microsoft Graph 客户端是否支持向 Azure AD B2C 租户注册的扩展属性,这有点令人困惑。

    当我使用 Azure AD Graph API 查询用户对象时,会返回自定义属性(例如“CreatedTime”)。
    https://graph.windows.net/{tenant}/users/{objectId}
    返回:

    {
        "odata.metadata": "https://graph.windows.net/{tenant}/$metadata#directoryObjects/Microsoft.DirectoryServices.User/@Element",
        "odata.type": "Microsoft.DirectoryServices.User",
        "objectType": "User",
        ...
        "extension_917ef9adff534c858b0a683b6e6ec0f3_CreatedTime": 1518602039
    }
    

    当我使用 Microsoft Graph API 查询同一个对象时,不会返回自定义属性。
    https://graph.microsoft.com/v1.0/users/{id}/extensions
    返回:
    {
        "@odata.context": "https://graph.microsoft.com/v1.0/$metadata#users('{id}')/extensions",
        "value": []
    }
    

    除非您得到更好的答案,否则我建议您使用 Azure AD Graph API,并且可以选择 the Azure AD Graph Client , 获取和设置 Azure AD B2C 用户的扩展属性。

    获取和设置用户扩展属性的示例可以在 Announcing Azure AD Graph API Client Library 2.0 中找到。

    关于c# - 如何在 ASP.NET MVC 中获取/设置自定义 Azure Active Directory B2C 用户属性?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48769708/

    相关文章:

    当字符串太长时,C# +=(加等于)(加法赋值)工作很慢?

    asp.net-mvc - 在 MVC API 中使用 Microsoft Azure Active Directory 验证 OAuth 2.0 不记名 token 时出现 401

    git - VSTS - 连接到 Azure Active Directory 后无法进行 git push

    Azure AD 注销 URL 未重定向

    c# - 如何使用 Linq 或 Lambda 执行更新?(C#, Asp.net,Linq , Lambda)

    c# - 确定元素在 IQueryable 中的位置

    C# windows 应用程序 - 想将我的业务和数据层保留在单独的机器中并使用 WCF 访问它

    sharepoint - 从 SharePoint 列表项获取自定义字段

    microsoft-graph-api - 无法使用 GRAPH 从 Outlook 联系人获取某些数据类型的 SingleValueExtendedProperties

    c# - Microsoft Graph API 测试 - 代表用户获取访问权限