以下代码是本人为公司编写的AD帐户信息修改的程序代码,包括头像(二进制)的上传与修改;
设计过程大致如下:
1.选择对应AD服务器并验证、登陆; --因本公司有多个不同AD子域,需要各自管理对应AD域;
2.查询帐户;
3.修改信息并提交保存; --- 如邮件、 别名、部门、头像等;
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.DirectoryServices;
using System.IO;
using System.Net;
using System.Text.RegularExpressions;
using System.Drawing;
using System.Xml;
namespace ADHelp10
{
public partial class _Default : System.Web.UI.Page
{
public string company = "", department = "", description = "", displayName = "", telephoneNumber = "", title = "", distinguishedName = "", office = "", email = "", userpassword = "";
public string username_admin ="",password_admin="";
protected void Page_Load(object sender, EventArgs e)
{
//初始化菜单-- 供用户选择不同域,通XML获取;
}
public void IsAuthenticated_admin(string id)
{
//验证,请自行添加;
}
using (DirectoryEntry de0 = new DirectoryEntry())
{
de0.Path = LDAP2;
de0.Username = @"" + this.Session["domain"].ToString().Trim() + "//" + id;
de0.Password = password;
DirectorySearcher MySearch = new DirectorySearcher(de0);
MySearch.Filter = "(sAMAccountName=" + id + ")";
try
{
SearchResultCollection results = MySearch.FindAll();
if (results.Count > 0)
{
this.Panel2.Visible = false;
this.Panel3.Visible = true;
//this.usersearch.Text = id;
}
else
{
this.Response.Write("<script language='javascript'>alert('提示:AD授权验证失败!');history.go(-1);</script>");
}
}
catch (Exception)
{
//Response.Write(ex);
this.Response.Write("<script language='javascript'>alert('提示:服务器无法连接!');history.go(-1);</script>");
}
}
}
//登陆,并验证
protected void Button1_Click(object sender, EventArgs e)
{
this.Session["username"] = (this.Username.Text.Trim().ToLower());
this.Session["password"] = this.Password.Text.Trim();
this.Session["LDAP2"] = this.DropDownList1.SelectedValue.Trim();
//通过LDAP2值,确定其他session,LDAP1和domain;
DataRow row = Session[DropDownList1.SelectedIndex.ToString()] as DataRow;
this.Session["LDAP1"] = row["LDAP1"];
this.Session["domain"] = row["domain"];
//验证授权
IsAuthenticated_admin(this.Username.Text.Trim().ToLower());
IsAuthenticated(this.Username.Text.Trim().ToLower(), this.Password.Text.Trim(), this.DropDownList1.SelectedValue.Trim());
</