c#-4.0 - 使用 C# 从事件目录检索并显示用户图像

标签 c#-4.0 active-directory

我有一个用 C# 开发的网站。 该网站将托管在我们组织的本地内联网中。 用户使用 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");
        }
    }
}
}

最佳答案

您应该将带有 src url 的 img 标记添加到您编写的处理程序中(可以说它应该是 Web 表单的 HttpHandler) 我想这个网址应该是这样的

<img src="http://myintranetsite/ADImageHandler alt="" />

关于c#-4.0 - 使用 C# 从事件目录检索并显示用户图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18457842/

相关文章:

linq - 在 C# 中从 XML 解析 CData

asp.net-mvc - 如何在Web表单和MVC2应用程序之间共享基于.net(C#)的身份验证 session ?

vbscript - Active Directory VBS搜索脚本的第1行中的错误 'Expected end of statement'

c# - 无法从远程服务器访问 Active Directory 域 Controller

c++ - 使用 C++ Builder 实现 Active Directory

c# - 自定义多因素 Active Directory 身份验证

c# - 与 oops 概念相关

security - 为 C# 评估器提供沙盒应用程序域的最佳证据

c# - 作为 AD 组发送电子邮件

c# - 具有自动生成列的可编辑 GridView