问题
Our company is on Office 365. So when users sign in to my asp.net mvc website, I'd like to display their photo, like Outlook does.
Where does outlook get the user's photo? And how I can get to it programmatically?
I've tried hitting up the ActiveDirectory and fetching the thumbnailPhoto, but it's blank for most users. Thus Outlook must be getting it elsewhere. Where and how?
I've also seen some examples where Microsoft.Office.Interop.Outlook assembly is used (assuming it requires Outlook installed to function). However, this website runs on a server that doesn't have Outlook installed.
回答1:
I've tried hitting up the ActiveDirectory and fetching the thumbnailPhoto, but it's blank for most users. Thus Outlook must be getting it elsewhere. Where and how?
The user photo is not available in your ADon-premise, because your users most likely haven't stored it there in the first place unless you have provided them with an app which writes it to the AD.
The user photo is available in your ADAzure, because your users most likely are applying their photos in Outlook (or online).
I am assuming that your on-premise AD is being synced with Azure AD via Dirsync or ADConnect utilities. If yes, then actually the photo
or thumbnailphoto
attributes are synced only one-way (from on-premise to Azure) and are not written back. Reference here for DirSync and here for ADConnect.
This is why you are not able to get it out of your AD.
How to get user's exchange/outlook/office365 photo?
There are quite a few ways to get the photo from Azure AD:
Exchange Web Services (EWS): by using the GetUserPhoto operation. Basically sending a SOAP envelope with the email address and size. the XML response will get you the base64-encoded photo contained in the PictureData element
Exchange Web Services (EWS Managed API and/or REST API): Basically using the AutoDiscover service to the URL
https://outlook.office365.com/ews/exchange.asmx
and then connect viaEWS.Service.ConnectToService
method on theMicrosoft.Exchange.WebServices.Data
namespace for which you need a reference to the library (a DLL that you can download from here). The method remains the sameGetUserPhoto
and the image is contained in the response in binary.Outlook REST API: Basically sending a
GET
request to the Outlook API (with the bearer access token) at the URLhttps://outlook.office.com/api/{version}/me/photo
for your own, and at the URLhttps://outlook.office.com/api/{version}/users/email@example.com/photo
. You get the photo stream in binary format as the response. Don't forget to append$value
to get the blob otherwise you'll get the metadata. More details referenced here.Office 365 Unified API (aka Graph): Basically nearly the same as the older API. Send a
GET
request (with bearer token) to the URLhttps://graph.microsoft.com/{version}/me/photo/
for your own photo andhttps://graph.microsoft.com/{version}/users/UPN/photo
for another user. More or less the same. More details referenced here. There is also a sandbox here for you to explore the samples and API references. You can also try it out yourself online interactively here (Don't forget to sign-in before firing your get requests)
Hope that helps.
Some more references: here, here and here.
回答2:
Try this method,It works for me It will fetch the user profile picture from account
https://outlook.office365.com/owa/service.svc/s/GetPersonaPhoto?email=plambhure@sanjeevgroup.com&UA=0&size=HR64x64&sc=1468233338850
来源:https://stackoverflow.com/questions/34069979/how-to-get-users-exchange-outlook-office365-photo