在任何地方使用Active Directory映像

介绍 (Introduction)

Active Directory has a neat feature that enables you to upload user images. Unfortunately not all systems support reading these user images natively and writing code involves manipulating a byte array and memory stream which not everybody is comfortable in doing.

Active Directory具有一项简洁的功能,使您可以上传用户图像。 不幸的是,并非所有系统都支持本地读取这些用户图像,并且编写代码涉及操纵并非所有人都习惯的字节数组和内存流。

This simple web application should suffice as both a solution for those that want to simply display images in their web applications, using an HTML <IMG> tag or as a starter project if you would like to enhance it.

对于那些只想使用HTML <IMG>标记在其Web应用程序中简单显示图像的解决方案,如果想对其进行增强,则此简单的Web应用程序就足够了。

演示版 (Demo)

演示地址

()

代码 (The Code)

GitHub repo address: https://github.com/svermaak/UserPhotos.git

GitHub仓库地址https : //github.com/svermaak/UserPhotos.git

using System;
using System.Configuration;
using System.DirectoryServices;
using System.IO;
using System.Web;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string sAMAccountName = Request.QueryString["SAMAccountName"];

Response.Buffer = true;
Response.Charset = "";
Response.Cache.SetCacheability(HttpCacheability.NoCache);
Response.ContentType = "image/jpeg";
Response.BinaryWrite(GetUserPicture(sAMAccountName));
Response.Flush();
Response.End();
}
private byte[] GetUserPicture(string sAMAccountName)
{
byte[] returnValue;
try
{
// Get configuration
string domainFQDN = ConfigurationManager.AppSettings["DomainFQDN"];
string domainDN = ConfigurationManager.AppSettings["DomainDN"];
string domainNetBIOS = ConfigurationManager.AppSettings["DomainNetBIOS"];
string domainUserName = ConfigurationManager.AppSettings["DomainUserName"];
string domainPassword = ConfigurationManager.AppSettings["DomainPassword"];

DirectoryEntry directoryEntry;
if ((!string.IsNullOrEmpty(domainNetBIOS)) && (!string.IsNullOrEmpty(domainUserName)) && (!string.IsNullOrEmpty(domainPassword)))
{
// Using credentials from Web.Config
directoryEntry = new DirectoryEntry($@"LDAP://{domainFQDN}/{domainDN}", $"{domainNetBIOS}\\{domainUserName}", $"{domainPassword}");
}
else
{
// Using Application Pool Identity
directoryEntry = new DirectoryEntry($@"LDAP://{domainFQDN}/{domainDN}");
}

DirectorySearcher directorySearcher = new DirectorySearcher(directoryEntry)
{
Filter = string.Format("(&(SAMAccountName={0}))", sAMAccountName)
};
SearchResult user = directorySearcher.FindOne();
returnValue = user.Properties["thumbnailPhoto"][0] as byte[];
}
catch
{
// Handles error and default image
System.Drawing.Image img = System.Drawing.Image.FromFile($@"{HttpRuntime.AppDomainAppPath}\DefaultImage.png");
img = img.GetThumbnailImage(370, 370, null, IntPtr.Zero);
using (MemoryStream ms = new MemoryStream())
{
img.Save(ms, System.Drawing.Imaging.ImageFormat.Jpeg);
returnValue = ms.ToArray();
}
}
return returnValue;
}
} 

部署 (deployment)

Simply open the solution in Visual Studio and publish it on an IIS server. Building a Visual Studio project and publishing of a site is outside of the scope of this article.

只需在Visual Studio中打开解决方案并将其发布在IIS服务器上即可。 构建Visual Studio项目和发布网站不在本文讨论范围之内。

After publishing, edit the web.config and change these values appropriately. If you do not specify a username and password, the Application Pool's identity will be used

发布后,编辑web.config并适当更改这些值。 如果您未指定用户名和密码,则将使用应用程序池的身份

<add key="DomainFQDN" value="ittelligence.com" />
<add key="DomainDN" value="DC=ITtelligence,DC=com" />
<add key="DomainNetBIOS" value="ITtelligence" />
<add key="DomainUserName" value="" />
<add key="DomainPassword" value="" /> 

结论 (Conclusion)

I hope you found this tutorial useful. You are encouraged to ask questions, report any bugs or make any other comments about it below.

希望本教程对您有所帮助。 鼓励您在下面提出问题,报告任何错误或对此作出任何其他评论。

Note: If you need further "Support" about this topic, please consider using the Ask a Question feature of Experts Exchange. I monitor questions asked and would be pleased to provide any additional support required in questions asked in this manner, along with other EE experts...  

注意 :如果您需要有关此主题的更多“支持”,请考虑使用Experts Exchange 的“提问”功能。 我会监督提出的问题,并很高兴与其他电子工程师一起提供以这种方式提出的问题所需的任何其他支持...

Please do not forget to press the "Thumbs Up" button if you think this article was helpful and valuable for EE members.

如果您认为本文对EE成员有用且有价值,请不要忘记按下“竖起大拇指”按钮。

It also provides me with positive feedback. Thank you!

它还为我提供了积极的反馈。 谢谢!

翻译自: https://www.experts-exchange.com/articles/33495/Use-Active-Directory-Images-Anywhere.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值