I need to write via C# the user profile pictures into the Active Directory. First I tried using the DirectoryServices
setting the two properties thumbnailPhoto
and jpegPhoto
to the DirectoryEntry
object.
Because on some users I got Access Denied
I tried using Microsoft Graph SDK API
as followings:
byte[] bytes = File.ReadAllBytes(path);
using (MemoryStream stream = new MemoryStream(bytes))
{
await _client.Users[user.Id].Photo.Content.PutAsync(stream);
}
where the loaded stream is byte[1743117].
Using this approach it works fine, but getting the image as following, returns a reduced version, 100x100, in other words, the thumbnail:
await _client.Users[user.Email].Photo.Content.GetAsync()
Then I noticed that pics are automatically synched to SharePoint Online, Exchange, but not to Teams, so I am wondering how all the magic works behind the curtains.
Furthermore, if I query the Active Dirctory again as followings:
using (DirectorySearcher dsSearcher = new DirectorySearcher())
{
dsSearcher.Filter = "(&(objectClass=user) (mail=aMail))";
SearchResult? result = dsSearcher.FindOne();
if (result != null)
{
using (DirectoryEntry user = new DirectoryEntry(result.Path))
{
user.Properties["jpegPhoto"];
user.Properties["thumbnailPhoto"];
}
}
}
jpegPhoto
is byte[507251] and thumbnailPhoto
is byte[4994]
In the end what I need is:
As mentioned by @Gabriel Luci, Ms Graph resizes the picture.
The problem accessing the active directory properties was due to permissions. In fact, the permissions write all properties
is required.