Azure Active Directory 用户(类型=具有现有用户帐户的用户)Json 列表模型给出空

Azure Active Directory Users (type=User with an existing user account) Json to List Model is giving null

我想将 Json 结果反序列化为模型。 我正在使用 Azure 单一登录方法。当我在广告中使用新创建的用户(您组织中的新用户)登录时,我得到了正确的用户信息。但是,如果我使用 "User with an existing user account" 在 AzureAd 中创建了新用户。我能够登录并且请求也经过身份验证。但我没有得到用户资料。用户配置文件为空。但是 "responseString" 包含用户的所有值。有人可以帮我吗?

UserProfile profile = JsonConvert.DeserializeObject<UserProfile>(responseString);

public class UserProfile
{
    public string DisplayName { get; set; }
    public string GivenName { get; set; }
    public string Surname { get; set; }
}

Json

拥有现有用户帐户的用户

{"odata.metadata":"https://graph.windows.net/780cdd84-48ba-4be3-8d66-b40b8bee6b0b/$metadata#directoryObjects/Microsoft.WindowsAzure.ActiveDirectory.User","value":[{"odata.type":"Microsoft.WindowsAzure.ActiveDirectory.User","objectType":"User","objectId":"****************","accountEnabled":true,"assignedLicenses":[],"assignedPlans":[],"city":null,"country":null,"department":null,"dirSyncEnabled":null,"displayName":"mahesh","facsimileTelephoneNumber":null,"givenName":"map","jobTitle":null,"lastDirSyncTime":null,"mail":null,"mailNickname":"devb_azureteam.com#EXT#","mobile":null,"otherMails":["devb@azureteam.com"],"passwordPolicies":"None","passwordProfile":null,"physicalDeliveryOfficeName":null,"postalCode":null,"preferredLanguage":null,"provisionedPlans":[],"provisioningErrors":[],"proxyAddresses":[],"state":null,"streetAddress":null,"surname":"map","telephoneNumber":null,"usageLocation":null,"userPrincipalName":"devb_azureteam.com#EXT#@AzureteamLoginTest.onmicrosoft.com"},

{"odata.type":"Microsoft.WindowsAzure.ActiveDirectory.User","objectType":"User","objectId":"****************","accountEnabled":true,"assignedLicenses":[],"assignedPlans":[],"city":null,"country":null,"department":null,"dirSyncEnabled":null,"displayName":"Education at AzureTeam","facsimileTelephoneNumber":null,"givenName":"Education","jobTitle":null,"lastDirSyncTime":null,"mail":null,"mailNickname":"education_azureteam.com#EXT#","mobile":null,"otherMails":["education@azureteam.com"],"passwordPolicies":null,"passwordProfile":null,"physicalDeliveryOfficeName":null,"postalCode":null,"preferredLanguage":null,"provisionedPlans":[],"provisioningErrors":[],"proxyAddresses":[],"state":null,"streetAddress":null,"surname":"at AzureTeam","telephoneNumber":null,"usageLocation":null,"userPrincipalName":"education_azureteam.com#EXT#@AzureteamLoginTest.onmicrosoft.com"},
{"odata.type":"Microsoft.WindowsAzure.ActiveDirectory.User","objectType":"User","objectId":"*****************","accountEnabled":true,"assignedLicenses":[],"assignedPlans":[],"city":null,"country":null,"department":null,"dirSyncEnabled":null,"displayName":"mahesh","facsimileTelephoneNumber":null,"givenName":"mahesh","jobTitle":null,"lastDirSyncTime":null,"mail":null,"mailNickname":"mahesh","mobile":null,"otherMails":["map@azureteam.com"],"passwordPolicies":"None","passwordProfile":null,"physicalDeliveryOfficeName":null,"postalCode":null,"preferredLanguage":null,"provisionedPlans":[],"provisioningErrors":[],"proxyAddresses":[],"state":null,"streetAddress":null,"surname":null,"telephoneNumber":null,"usageLocation":null,"userPrincipalName":"mahesh@AzureteamLoginTest.onmicrosoft.com"}]}

New user in organization

{"odata.metadata":"https://graph.windows.net/780cdd84-48ba-4be3-8d66-b40b8bee6b0b/$metadata#directoryObjects/Microsoft.WindowsAzure.ActiveDirectory.User/@Element","odata.type":"Microsoft.WindowsAzure.ActiveDirectory.User","objectType":"User","objectId":"************","accountEnabled":true,"assignedLicenses":[],"assignedPlans":[],"city":null,"country":null,"department":null,"dirSyncEnabled":null,"displayName":"mahesh","facsimileTelephoneNumber":null,"givenName":"mahesh","jobTitle":null,"lastDirSyncTime":null,"mail":null,"mailNickname":"mahesh","mobile":null,"otherMails":["map@azureteam.com"],"passwordPolicies":"None","passwordProfile":null,"physicalDeliveryOfficeName":null,"postalCode":null,"preferredLanguage":null,"provisionedPlans":[],"provisioningErrors":[],"proxyAddresses":[],"state":null,"streetAddress":null,"surname":null,"telephoneNumber":null,"usageLocation":null,"userPrincipalName":"mahesh@AzureteamLoginTest.onmicrosoft.com"}

你实际上得到了不同的JSON。我简化了它们以显示问题。对于新用户 JSON 看起来像这样:

{  
  "odata.metadata":"...Microsoft.WindowsAzure.ActiveDirectory.User/@Element",
  "odata.type":"Microsoft.WindowsAzure.ActiveDirectory.User", 
  "displayName":"mahesh", 
  "givenName":"mahesh", 
  "surname":null  
} 

反序列化将适用于您的模型。但对于现有用户 JSON 是:

{  
  "odata.metadata":"...Microsoft.WindowsAzure.ActiveDirectory.User",
  "value":[  
    {  
      "odata.type":"Microsoft.WindowsAzure.ActiveDirectory.User",     
      "displayName":"mahesh",      
      "givenName":"map",     
      "surname":"map"     
    },
    {  },
    {  }
  ]
}

如您所见,您有一个 value 属性 包含一组用户。因此,为了能够反序列化它,您需要创建新模型:

public class RootObject
{
    public List<UserProfile> Value { get; set; }
}

然后:

var obj = JsonConvert.DeserializeObject<RootObject>(json);

我与 Azure Active Directory 不相似,但您应该检查为什么 odata.metadata 属性 对于这些响应不同。

我收到了敬礼:)

将此块添加到您的 global.asax

protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
    ClaimsIdentity id = ((ClaimsIdentity)User.Identity);
    Claim claim = id.FindFirst(ClaimTypes.Email);
    if (claim != null)
    {
        string email = claim.Value;
        id.AddClaim(new Claim(ClaimTypes.Name, email));
    }
}