【问题标题】:Get data from Active Directory using react and ASP.NET Core使用 react 和 ASP.NET Core 从 Active Directory 获取数据
【发布时间】:2020-09-16 05:40:11
【问题描述】:

有人可以分享代码来检索用户详细信息以及根据SamAccountName 的输入显示的缩略图吗?

这必须使用 ASP.NET Core 和 react 来完成。我对这些技术很陌生,请帮忙。

我的试验如下

控制器代码:

public Image ourTeam()
    {
        var userDetails = db.users.Where(x=>x.saMAccountName == "someUsername").FirstOrDefault();

        Image image = GetImage(userDetails.CN); //I get image data
        //context.Response.ContentType = "image/jpeg";

        var stream = new System.IO.MemoryStream();

        if (image != null)
            image.Save(stream, ImageFormat.Jpeg);

        return image;
    }

组件代码:

import * as React from 'react';

import { RouteComponentProps } from 'react-router';

interface FetchImageDataState {
imageList: ImageData[];
loading: boolean;
}

export class OurTeam extends React.Component<RouteComponentProps<{}>, FetchImageDataState> {
constructor() {
    super();
    this.state = { imageList: [], loading: true }; //initialize to default

    fetch('api/Home/ourTeam')
        .then(response => response.json() as Promise<ImageData[]>)
        .then(data => {
            this.setState({ imageList: data, loading: false });
        });
}

    public render() {

    let contents = this.state.loading
        ? <p><em>Loading...</em></p>
        : this.renderImageTable(this.state.imageList);
    return <div>
        <h1>Team</h1>


        {contents}
    </div>;
}

private renderImageTable(imageList: ImageData[]) {
    return <div className='table'>{
        imageList.map(x =>
            <img src={x.byteData} alt={x.CN} />)
    }</div>;


}
}

export class ImageData {
CN: string = "";
byteData: string = "";
}

PFB 关于我如何验证然后检索图像的代码:

private Image GetImage(string cn)
    {
        string LDAP_PATH = "LDAP://" + "SOMEPATH.com";
        string user = "USERID";
        string password = "PASSWORD";
                    using (DirectoryEntry entry = new DirectoryEntry(LDAP_PATH, user, password))
        {
            {
                DirectorySearcher search = new DirectorySearcher(entry);
                search.PropertiesToLoad.Add("sAMAccountName");
                search.PropertiesToLoad.Add("distinguishedName");
                search.Filter = "(&(objectClass=user)(sAMAccountName=" + cn + "))";
                SearchResult result = search.FindOne();
                if (result == null)
                {
                    search.Filter = "(&(objectClass=user)(cn=" + cn + "))";
                    result = search.FindOne();
                }

                if (result != null)
                {
                    DirectoryEntry entryUser = result.GetDirectoryEntry();
                    string propertyName = "thumbnailPhoto";
                    if (entryUser.Properties.Contains(propertyName))
                    {
                        byte[] img = entryUser.Properties[propertyName].Value as byte[];
                        MemoryStream memStream = new MemoryStream(img);
                        return Image.FromStream(memStream);
                    }
                }
            }
        }

        // User not authenticated
        return null;
    }

【问题讨论】:

  • 使用谷歌可以找到很多关于how to read Active Directory from ASP.NET Core的资料
  • 感谢您的回复。我能够在 Active Directory 中连接和验证。我面临的问题是在反应中显示详细信息以及用户的图像。不确定是否要添加中间件或仅控制器可以工作。
  • 您如何进行身份验证?您是使用 Windows 身份验证,还是直接使用 LDAP?
  • 嗨 - 我已更新帖子以显示我的身份验证方式..
  • 震惊地看到没有人拥有这方面的专业知识......

标签: reactjs asp.net-core active-directory ldap asp.net-core-webapi


【解决方案1】:

我假设您想要返回图像,就像您只是在下载文件一样。

首先,从GetImage 返回一个byte[]。通过将其转换为 Image 对象,您不会在这里获得任何东西。如果您需要在代码中编辑图像,那将是有意义的。不过既然你不是,那也没关系。

所以改变这个:

byte[] img = entryUser.Properties[propertyName].Value as byte[];
MemoryStream memStream = new MemoryStream(img);
return Image.FromStream(memStream);

到这里:

return entryUser.Properties[propertyName].Value as byte[];

然后在你的控制器中,返回一个FileContentResult,像这样:

public ActionResult ourTeam()
{
    var userDetails = db.users.Where(x=>x.saMAccountName == "someUsername").FirstOrDefault();

    byte[] image = GetImage(userDetails.CN);

    return File(image, "image/jpeg");
}

【讨论】:

  • 我在控制器页面中进行了您建议的更改。运行应用程序后图像未加载,由于反应代码中的一些错误可能导致以下错误: Uncaught (in promise) SyntaxError: Unexpected token � in JSON at position 0
  • 您的代码期待 JSON 响应,但它正在获取文件。您必须更改代码才能获得图像文件。不过,我不是 React 开发人员,所以我认为我无法帮助您。
  • 这对我和我的问题来说都是一个悲伤的消息.. ?
猜你喜欢
  • 1970-01-01
  • 2011-02-20
  • 1970-01-01
  • 2021-10-28
  • 1970-01-01
  • 2017-10-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多