C#如何在WEB开发中通过LDAP获取当前登录域用户信息,具体属性信息需结合域的配置,具体见以下代码:
前端页面适当位置显示用户名信息:
<span class="h3">
<span>@LoginSession.LoginUser.DisplayName</span>
</span>
LoginSession.cs文件:
using System.Web;
using SPC.MVC.Models;
using SPC.MVC.Util;
namespace SPC.MVC
{
public class LoginSession
{
private const string SessionKey = "Jaxxx.WUX.LoginUser";
public static UserInfo LoginUser
{
get
{
var item = HttpContext.Current.Session[SessionKey] as UserInfo;
if(item == null)
{
UserInfo currentUser = Common.GetADUserEntity(Common.GetCurrentNTID());
HttpContext.Current.Session[SessionKey] = currentUser;
item = currentUser;
}
return item;
}
}
public static void Logout()
{
//退出登录则需要清除相应的Session
HttpContext.Current.Session.Remove(SessionKey);
}
public static bool IsKicked()
{
var item = HttpContext.Current.Session[SessionKey] as UserInfo;
if (item == null)
{
return true;
}
return false;
}
}
}
Common.cs文件:
using System;
using System.Configuration;
using System.DirectoryServices;
using System.IO;
using System.Net.Mail;
using System.Web;
using System.Web.UI.WebControls;
using System.Web.Hosting;
using SPC.MVC.Models;
namespace SPC.MVC.Util
{
public static class Common
{
private readonly static string LDAP_PATH = ConfigurationManager.AppSettings["LDAP"];
/// <summary>
/// Get user id from HttpContext
/// </summary>
/// <returns></returns>
public static string GetCurrentNTID()
{
string identityName = HttpContext.Current.User.Identity.Name;
int splitIndex = identityName.IndexOf('\\');
return splitIndex > -1 ? identityName.Substring(splitIndex + 1) : identityName;
}
/// <summary>
/// Get User information from AD
/// </summary>
/// <param name="ntid">AD用户名</param>
/// <returns>用户实例</returns>
public static UserInfo GetADUserEntity(string ntid)
{
if (string.IsNullOrEmpty(ntid))
{
throw new Exception("Searched user id cannot be null.");
}
using (HostingEnvironment.Impersonate())
{
DirectoryEntry entry = new DirectoryEntry(LDAP_PATH);
DirectorySearcher searcher = new DirectorySearcher(entry);
searcher.SearchScope = SearchScope.Subtree;
searcher.Filter = "(&(objectClass=user)(sAMAccountName=" + ntid + "))";
SearchResult searchResult = searcher.FindOne();
if (searchResult != null)
{
UserInfo user = new UserInfo();
user.UID = ntid;
user.Department = GetADProperty(searchResult, "department"); //具体属性信息需结合域的配置
user.DisplayName = GetADProperty(searchResult, "displayName"); //具体属性信息需结合域的配置
user.Email = GetADProperty(searchResult, "mail"); //具体属性信息需结合域的配置
//user.Site.SiteName = GetSiteCodeFromUserOUPath(searchResult.Path); //具体属性信息需结合域的配置
return user;
}
}
return null;
}
/// <summary>
/// 根据属性名,在搜索结果中查找属性值
/// </summary>
/// <param name="searchResult">DirectorySearcher返回的搜索结果</param>
/// <param name="propertyName">属性名</param>
/// <returns>属性值</returns>
private static string GetADProperty(SearchResult searchResult, string propertyName)
{
if (searchResult.Properties.Contains(propertyName))
{
return searchResult.Properties[propertyName][0].ToString();
}
else
{
return string.Empty;
}
}
/// <summary>
/// 从OU中获取SITE信息
/// </summary>
/// <example>
/// LDAP://corp.jaxxx.org/CN=Ming Liu,OU=Users,OU=Wuxi,OU=RegionAsia,DC=corp,DC=JAXXX,DC=ORG
/// Site: OU=Wuxi
/// </example>
/// <param name="searchResult"></param>
/// <returns></returns>
//private static string GetSiteCodeFromUserOUPath(string ouPath)
//{
// string[] ouFolders = ouPath.Split(new char[] { ',' });
// if (ouFolders.Length > 5)
// {
// if (ouPath.IndexOf("OU=Wuhan") > -1)
// {
// return "WUH";
// }
// string site = ouFolders[ouFolders.Length - 5].Substring(3);
// switch (site)
// {
// case "GPSuzhou": return "SLF";
// case "Huangpu": return "HUA";
// case "Shanghai": return "SHA";
// case "Singapore": return "SIN";
// case "Wuxi": return "WUX";
// }
// }
// return string.Empty;
//}
}
}