简体   繁体   English

如何从Active Directory中获取用户头像?

[英]How to get users avatar from Active Directory?

I haven't found any way to do that. 我还没有找到任何办法。

using (var ctx = new PrincipalContext(ContextType.Domain,
            _webApiServiceConfiguration.Domain,
            _webApiServiceConfiguration.DomainAccessUserName,
            _webApiServiceConfiguration.DomainAccessPassword))
{
    UserPrincipal userAd = UserPrincipal.FindByIdentity(ctx,user.USERID);
}

I'm using the UserPrincipal class to get user data from AD. 我正在使用UserPrincipal类从AD获取用户数据。 Is there a way to get his picture? 有没有办法得到他的照片? Client has such a requirement. 客户有这样的要求。

You cannot get the photo of the user from the UserPrincipal class - you'll have to go "one level deeper", eg get the underlying DirectoryEntry object, and then figure out which AD property is holding your picture. 您无法从UserPrincipal类中获取用户的照片-您必须“更深入”,例如获取基础DirectoryEntry对象,然后找出哪个AD属性正在保存您的图片。

using (var ctx = new PrincipalContext(ContextType.Domain,
            _webApiServiceConfiguration.Domain,
            _webApiServiceConfiguration.DomainAccessUserName,
            _webApiServiceConfiguration.DomainAccessPassword))
{
    UserPrincipal userAd = UserPrincipal.FindByIdentity(ctx,user.USERID);

    // if not null, get the underlying DirectoryEntry
    if (userAd != null) 
    {
        DirectoryEntry de = userAd.GetUnderlyingObject() as DirectoryEntry;

        // if de is not null
        if (de != null) 
        {
            // get the property in question - possibly the "photo" property
            if(de.Properties["photo"] != null)
            {
                 // unlike a database column, an AD property can contain *multiple* values....

            }
        }
}

Also: those "binary" properties are often encoded in AD so don't expect to find the exact binary bytes for the photo right in the property - you might need to do some conversions or something...... 另外:那些“二进制”属性通常是用AD编码的,因此不要期望在该属性中找到照片的确切二进制字节-您可能需要进行一些转换或其他操作……

You might want to search for "Getting photo from Active Directory" or something - there's a lot of posts on exactly how to do this. 您可能要搜索“从Active Directory获取照片”之类的东西-关于如何执行此操作的文章很多。 Eg this question & answer shows some code - and there seem to be a jpegPhoto and a thumbnailPhoto property on the DirectoryEntry object, too.... (so you'll have to check which one is really populated in your AD) 例如, 此问题和答案显示了一些代码-并且DirectoryEntry对象上似乎也有jpegPhotothumbnailPhoto属性。...(因此,您必须检查广告中确实填充了哪个)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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