我有一个用 c# 开发的网站。该站点将托管在我们组织的本地 Intranet 中。用户使用 Windows 身份验证从他们的 Windows ID 进行身份验证。当用户访问该站点时,我想在用户的活动目录中显示图片。
此代码使我能够获取用户图像。随着Response.BinaryWrite(bb);
Response.Flush();
图像显示在空白页中。相反,我想在同一页面的 div 中显示我的图片。我怎样才能做到这一点?
using System;
using System.DirectoryServices;
using System.Linq;
namespace thumbnailTest
{
public partial class Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
String myUser = User.Identity.Name.Split('\\')[1];
if (myUser == null)
{
Response.Redirect("app_graphics/user.jpg");
}
Response.ContentType = "image/jpeg";
Response.Clear();
Response.BufferOutput = true;
DirectoryEntry de = new DirectoryEntry();
de.Path = "LDAP://";
DirectorySearcher search = new DirectorySearcher();
search.SearchRoot = de;
search.Filter = "(&(objectClass=user)(objectCategory=person)(sAMAccountName=" + myUser + "))";
search.PropertiesToLoad.Add("samaccountname");
search.PropertiesToLoad.Add("thumbnailPhoto");
SearchResult user;
user = search.FindOne();
String userName;
if (user == null)
{
Response.Redirect("app_graphics/user.jpg");
}
else
userName = (String)user.Properties["sAMAccountName"][0];
try
{
byte[] bb = (byte[])user.Properties["thumbnailPhoto"][0];
Response.BinaryWrite(bb);
Response.Flush();
}
catch
{
Response.Redirect("app_graphics/user.jpg");
}
}
}
}