using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.DirectoryServices;
using System.Net;
using System.IO;
using System.Text;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
private string _user = "shixiang";
private string _password = "shixiang";
private string _Ads_Server = "192.168.0.10";
private string _dc_domain = "kerui";
private string _dc_com = "com";
protected void Page_Load(object sender, EventArgs e)
{
string adUsername = AdUserName();
if (adUsername == null)
{
// TODO:
Response.Write("没有登录");
return;
}
string[] k = adUsername.Split("".ToCharArray());
if(k.Length != 2)
{
// TODO:
Response.Write("没有通过AD认证");
return;
}
adUsername = k[1];
JopsUser ju = GetInfo(adUsername);
// TODO: 代码
Response.Write(ju.Email+"<br/>");
Response.Write(ju.Description + "<br/>");
Response.Write(ju.Groups + "<br/>");
Response.Write(ju.Name + "<br/>");
Response.Write(ju.EmployeeInfo + "<br/>");
/*
DirectoryEntry entry = new DirectoryEntry("GC://forestname");
IEnumerator ie = entry.Children.GetEnumerator();
ie.MoveNext();
entry = (DirectoryEntry)ie.Current;
DirectorySearcher search = new DirectorySearcher(entry);
*/
/*
string defaultNC = "DC=kerui.com";
Download();
DirectoryEntry de = new DirectoryEntry("LDAP://cn=users," + defaultNC);//new DirectoryEntry(@"LDAP://kerui.com", "liuyu", "liuyupower");
foreach (DirectoryEntry child in de.Children)
{
Response.Write(child.Name);
}
*/
//DirectoryEntry de = SystemFrameworks.Helper.ADHelper.GetDirectoryEntryByAccount("liuyu", "liuyupower");
//this.Label1.Text = de.Parent.Name;
}
public string AdUserName()
{
if (HttpContext.Current.User != null)
{
return HttpContext.Current.User.Identity.Name;
}
else
{
return "";
}
}
public string GenAdspath(string ou, string cn)
{
//string adspath = "LDAP:/ rver/";
string adspath = "LDAP://" + _Ads_Server + "/";
string adspath_dc = "DC=" + _dc_domain + ",DC=" + _dc_com;
if (!(null == cn || cn.Trim().Length == 0)) adspath += "CN=" + cn + ",";
if (!(null == ou || ou.Trim().Length == 0)) adspath += "OU=" + ou + ",";
adspath += adspath_dc;
return adspath;
}
private DirectoryEntry GetDirectoryObject(string adspath)
{
DirectoryEntry oDE;
oDE = new DirectoryEntry(adspath, _user, _password, AuthenticationTypes.Secure);
return oDE;
}
public JopsUser GetInfo(string username)
{
JopsUser jopsUser = new JopsUser();
string adspath = GenAdspath("", "");
//DirectoryEntry entry = new DirectoryEntry(adspath);
DirectoryEntry entry = GetDirectoryObject(adspath);
DirectorySearcher search = new DirectorySearcher(entry);
search.Filter = "(SAMAccountName=" + username + ")";
search.PropertiesToLoad.Add("cn");
search.PropertiesToLoad.Add("mail");
search.PropertiesToLoad.Add("givenName");
search.PropertiesToLoad.Add("sn");
search.PropertiesToLoad.Add("description");
search.PropertiesToLoad.Add("memberOf");
SearchResult result = search.FindOne();
//Common.writeLogFile("GetInfo3!");
string strHold;
jopsUser.Account = username;
//obtain Account
try
{
if (result.Properties["cn"].Count > 0)
{
strHold = (String)result.Properties["cn"][0];
jopsUser.Name = strHold;
}
else
{
jopsUser.Name = "";
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining mail. " + ex.Message);
}
//obtain Email
try
{
if (result.Properties["mail"].Count > 0)
{
strHold = (String)result.Properties["mail"][0];
jopsUser.Email = strHold;
}
else
{
jopsUser.Email = "";
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining mail. " + ex.Message);
}
//obtain FirstName
try
{
if (result.Properties["givenName"].Count > 0)
{
strHold = (String)result.Properties["givenName"][0];
jopsUser.FirstName = strHold;
}
else
{
jopsUser.FirstName = "";
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining givenName. " + ex.Message);
}
//obtain LastName
try
{
if (result.Properties["sn"].Count > 0)
{
strHold = (String)result.Properties["sn"][0];
jopsUser.LastName = strHold;
}
else
{
jopsUser.LastName = "";
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining sn. " + ex.Message);
}
//obtain Description
try
{
if (result.Properties["description"].Count > 0)
{
strHold = (String)result.Properties["description"][0];
jopsUser.Description = strHold;
}
else
{
jopsUser.Description = "";
}
}
catch (Exception ex)
{
throw new Exception("Error obtaining description. " + ex.Message);
}
//obtain Groups
try
{
StringBuilder groupNames = new StringBuilder();
int propertyCount = result.Properties["memberOf"].Count;
String dn;
int equalsIndex, commaIndex;
for (int propertyCounter = 0;
propertyCounter < propertyCount;
propertyCounter++)
{
dn = (String)result.Properties["memberOf"][propertyCounter];
equalsIndex = dn.IndexOf("=", 1);
commaIndex = dn.IndexOf(",", 1);
if (-1 == equalsIndex)
{
return null;
}
groupNames.Append(dn.Substring((equalsIndex + 1),
(commaIndex - equalsIndex) - 1));
groupNames.Append("|");
}
jopsUser.Groups = groupNames;
}
catch (Exception ex)
{
throw new Exception("Error obtaining group names. " + ex.Message);
}
return jopsUser;
}
}
public class JopsUser
{
public string Account = "";
public string Name = "";
public string FirstName = "";
public string LastName = "";
public string Email = "";
public string Description = "";
public string EmployeeInfo; //员工信息
public StringBuilder Groups = new StringBuilder();//AD 所在组
public DataTable RangsDt = new DataTable();//权限列表
public Hashtable SysParameter = new Hashtable();
public JopsUser()
{
//
// TODO: Add constructor logic here
//
}
public DataTable getRangs()
{
return RangsDt;
}
//从数据库读取扩展信息
public string getExtentInfo()
{
return EmployeeInfo;
}
public override string ToString()
{
//return base.ToString();
string userInfo = "Account:"
+ Account
+ "/nName:"
+ Name
+ "/nFirstName:"
+ FirstName
+ "/nLastName:"
+ LastName
+ "/nEmail:"
+ Email
+ "/nDescription"
+ Description
+ "/nGroups:"
+ Groups.ToString()
+ "/nEmployeeInfo:"
+ EmployeeInfo.ToString();
return userInfo;
}
}