简体   繁体   中英

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

I want to deserialize Json result into a model. I am using Azure Single sign on method. when I am login with new new created user in ad (new user in your organization) i am getting proper user info. but if i created new user in AzureAd with "User with an existing user account".I am able to log in and request is also authenticated. but i am not getting user profile. user profile is null. but "responseString" contains all values for user. can any one help me for that ?

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

User with an existing user account

{"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"}

You actually get different JSON's. I simplified them to show the problem. For new user JSON looks like that:

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

Deserialization will work with your model. But for existing user JSON is:

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

As you can see you have a value property that holds an array of users. So to be able to deserialize it you will need to create new model:

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

And then:

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

I'm not similar with Azure Active Directory but you should check why odata.metadata property is different for these responses.

I got the saluting :)

Add this block in your 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));
    }
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM