【发布时间】: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