【问题标题】:retrieve and display user image from active directory with c#使用 c# 从活动目录中检索和显示用户图像
【发布时间】:2013-08-29 18:03:22
【问题描述】:

我有一个用 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");
        }
    }
}
}

【问题讨论】:

标签: c#-4.0 active-directory


【解决方案1】:

您应该在您编写的处理程序中添加一个带有 src url 的 img 标记(可以说是 Web 表单的 HttpHandler) 我想网址看起来像

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    相关资源
    最近更新 更多