如何使用ASP.NET代码查看、修改AD用户信息
来源:互联网 发布:手机端js日期选择控件 编辑:程序博客网 时间:2024/06/12 23:32
以下代码是本人为公司编写的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());
}
//查询并显示帐户信息
protected void Button4_Click(object sender, EventArgs e)
{
if (Session["username"] != null)
{
using (DirectoryEntry de = new DirectoryEntry())
{
de.Path = this.Session["LDAP2"].ToString();
de.Username = @"" + this.Session["domain"].ToString().Trim() + "//" + this.Session["username"].ToString();
de.Password = this.Session["password"].ToString();
DirectorySearcher MySearch = new DirectorySearcher(de);
MySearch.Filter = "(sAMAccountName=" + this.usersearch.Text.Trim().ToLower() + ")";
try
{
SearchResultCollection results = MySearch.FindAll();
if (results.Count > 0)
{
if (results[0].Properties.Contains("company"))
company = results[0].Properties["company"][0].ToString();
if (results[0].Properties.Contains("department"))
department = results[0].Properties["department"][0].ToString();
if (results[0].Properties.Contains("description"))
description = results[0].Properties["description"][0].ToString();
if (results[0].Properties.Contains("displayName"))
displayName = results[0].Properties["displayName"][0].ToString();
if (results[0].Properties.Contains("telephoneNumber"))
telephoneNumber = results[0].Properties["telephoneNumber"][0].ToString();
if (results[0].Properties.Contains("title"))
title = results[0].Properties["title"][0].ToString();
if (results[0].Properties.Contains("distinguishedName"))
distinguishedName = results[0].Properties["distinguishedName"][0].ToString();
if (results[0].Properties.Contains("physicalDeliveryOfficeName"))
office = results[0].Properties["physicalDeliveryOfficeName"][0].ToString();
if (results[0].Properties.Contains("mail"))
email = results[0].Properties["mail"][0].ToString().Trim();
if (results[0].Properties.Contains("jpegPhoto"))
{
byte[] barrImage = (byte[])results[0].Properties["jpegphoto"][0];
//Response.BinaryWrite(barrImage);//读取并直接显示
viewjpegphoto(barrImage);
}
else
{
string noneimage = ("none.jpg"); // 服务器端水印图路径(图片)
this.Image3.ImageUrl = noneimage;
}
//string strPathx = Server.MapPath("temp/" + ii + ".jpg"); // 服务器端文件路径
this.Label1.Visible = false;
this.Label2.Visible = false;
this.Label1.Text = "";
this.Panel1.Visible = true;
this.Usernamex.Text = "姓名:" + displayName;
this.emailx.Text = "邮箱:" + email;
this.Titlex.Text = "职务:" + title;
this.Telx.Text = telephoneNumber;
this.DistinguishedNamex.Text = distinguishedName;
}
else
{
this.Label2.Visible = true;
this.Label2.Text = "帐户'" + this.usersearch.Text.Trim() + "'不存在!";
this.Panel1.Visible = false;
}
}
catch (Exception ex)
{
Response.Write(ex);
this.Label1.Visible = true;
this.Label1.Text = "帐户不存在或密码错误!1!!";
this.Usernamex.Text = "";
this.emailx.Text = "";
this.Titlex.Text = "";
this.Telx.Text = "";
this.DistinguishedNamex.Text = "";
this.Panel1.Visible = false;
}
}
}
else
{
this.Response.Redirect("default.aspx");
}
}
//修改并保存信息;
protected void Button3_Click(object sender, EventArgs e)
{
//连接到AD;
DirectoryEntry de = new DirectoryEntry();
de.Path = this.Session["LDAP1"].ToString() + this.DistinguishedNamex.Text;
de.Username = @"" + this.Session["domain"].ToString().Trim() + "//" + this.Session["username"].ToString();
de.Password = this.Session["password"].ToString();
if (this.Uploadfilex.PostedFile.FileName != "")
{
if (this.Uploadfilex.PostedFile.ContentType.ToLower().IndexOf("image") < 0)
{
this.Response.Write("<script language='javascript'>alert('图片格式不正确!')</script>");
return;
}
string name = Uploadfilex.PostedFile.FileName; // 客户端文件路径
//-----------------------------------------------------------------------------------
string getdatex = getdatevalue();
FileInfo file = new FileInfo(name);
string fileName = file.Name; // 文件名称
string ext = file.Extension; //扩展名
string filename_upload = getdatex + ext;
string webFilePath = Server.MapPath("temp/" + filename_upload); // 服务器端文件路径
if (!File.Exists(webFilePath))
{
//-----------------------------------------------------------------------------------
try
{
// 读取的图片文件,并写入数据库;
this.Uploadfilex.SaveAs(webFilePath); // 使用 SaveAs 方法保存文件
FileStream fs = new FileStream(webFilePath, FileMode.Open);
BinaryReader r = new BinaryReader(MakeThumbnail(fs, 130, 130, "H"));
r.BaseStream.Seek(0, SeekOrigin.Begin);
byte[] ba = new byte[r.BaseStream.Length];
ba = r.ReadBytes((int)r.BaseStream.Length);
//Response.BinaryWrite(ba);
de.Properties["jpegphoto"].Clear();
de.Properties["jpegphoto"].Insert(0, ba);
viewjpegphoto(ba);
r.Close();
fs.Close();
}
catch (Exception ex )
{
//Label1.Text = "提示:图片失败,失败原因:" + ex.Message;
this.Response.Write("<script language='javascript'>alert('" + ex.Message + "')</script>");
}
}
}
else
{
//this.Response.Write("<script>alert('提醒:下回记得上传个人照片!');</script>"); //允许
}
//---------------------------------------------------------------------------------------------
if (this.Telx.Text != telephoneNumber && this.Telx.Text != "")
{
de.Properties["telephoneNumber"].Value = this.Telx.Text.Trim();
}
else
{
//不允许保存空内容;
//this.Response.Write("<script language='javascript'>alert('提示:保持失败,电话号码不能为空!')</script>");
}
try
{
de.CommitChanges();
this.Response.Write("<script language='javascript'>alert('提示:提交成功!')</script>");
}
catch ( Exception )
{
this.Response.Write("<script language='javascript'>alert('提示:权限不足或写入失败!');history.go(-1);</script>");
}
}
static string getyestoday()
{
DateTime now = DateTime.Now;
//DateTime now = DateTime.Now.Date.AddDays(-1);
string jj = string.Concat(
now.Year,
now.Month.ToString().PadLeft(2, '0'),
now.Day.ToString().PadLeft(2, '0')
);
return jj;
}
static string getdatevalue()
{
//生产图片文件名,防止重复;
}
//上载后对图片进行缩小修改
public System.IO.Stream MakeThumbnail(Stream input,int width, int height, string mode)
{
System.Drawing.Image originalImage = System.Drawing.Image.FromStream(input);
System.IO.Stream output = new MemoryStream();
int towidth = width;
int toheight = height;
int x = 0;
int y = 0;
int ow = originalImage.Width;
int oh = originalImage.Height;
switch (mode)
{
case "HW"://指定高宽缩放(可能变形)
break;
case "W"://指定宽,高按比例
toheight = originalImage.Height * width / originalImage.Width;
break;
case "H"://指定高,宽按比例
towidth = originalImage.Width * height / originalImage.Height;
break;
case "Cut"://指定高宽裁减(不变形)
if ((double)originalImage.Width / (double)originalImage.Height > (double)towidth / (double)toheight)
{
oh = originalImage.Height;
ow = originalImage.Height * towidth / toheight;
y = 0;
x = (originalImage.Width - ow) / 2;
}
else
{
ow = originalImage.Width;
oh = originalImage.Width * height / towidth;
x = 0;
y = (originalImage.Height - oh) / 2;
}
break;
default:
break;
}
//新建一个bmp图片
System.Drawing.Image bitmap = new System.Drawing.Bitmap(towidth, toheight);
//新建一个画板
System.Drawing.Graphics g = System.Drawing.Graphics.FromImage(bitmap);
//设置高质量插值法
g.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.High;
//设置高质量,低速度呈现平滑程度
g.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.HighQuality;
//清空画布并以透明背景色填充
g.Clear(System.Drawing.Color.Transparent);
//在指定位置并且按指定大小绘制原图片的指定部分
g.DrawImage(originalImage, new System.Drawing.Rectangle(0, 0, towidth, toheight),
new System.Drawing.Rectangle(x, y, ow, oh),
System.Drawing.GraphicsUnit.Pixel);
try
{
//以jpg格式保存缩略图
bitmap.Save(output, System.Drawing.Imaging.ImageFormat.Jpeg);
}
catch (System.Exception e)
{
throw e;
}
finally
{
originalImage.Dispose();
bitmap.Dispose();
g.Dispose();
}
return output;
}
//显示图片
protected void viewjpegphoto(byte[] bardata)
{
string getdatey = getdatevalue();
//图片路径
string strPathx = "temp/" + getdatey + ".jpg";
//string strPathx = Server.MapPath("temp/" + ii + ".jpg"); // 服务器端文件路径
string strPhotoPathx = Server.MapPath(strPathx);
//Response.Write(strPathx);
//Response.Write("<br>");
//Response.Write(strPhotoPathx);
//保存图片文件
BinaryWriter bwx = new BinaryWriter(File.Open(strPhotoPathx, FileMode.OpenOrCreate));
bwx.Write(bardata);
bwx.Close();
//显示图片
this.Image3.ImageUrl = strPathx;
}
//private ICollection createDataSource()
//{
// DataTable dt = new DataTable();
// //define the columns of the table
// dt.Columns.Add("plantname", typeof(string));
// dt.Columns.Add("LDAP1", typeof(string));
// dt.Columns.Add("LDAP2", typeof(string));
// dt.Columns.Add("domain", typeof(string));
// //read the content of the XML file into a DataSet
// DataSet lanDS = new DataSet();
// lanDS.ReadXml(Server.MapPath("plant.xml"));
// if (lanDS.Tables.Count > 0)
// {
// foreach (DataRow copyRow in lanDS.Tables[0].Rows)
// {
// dt.ImportRow(copyRow);
// }
// }
// DataView dv = new DataView(dt);
// return dv;
//}
}
}
}
有点乱啊,呵! 看不明白可以联系我!
- 如何使用ASP.NET代码查看、修改AD用户信息
- ASP.net如何访问域用户信息
- 使用ASP.NET 2.0 Profile存储用户信息
- 使用ASP.NET 2.0 Profile存储用户信息
- 使用ASP.NET 2.0 Profile存储用户信息
- 使用ASP.NET 2.0 Profile存储用户信息
- 使用ASP.NET 2.0 Profile存储用户信息
- 使用ASP.NET 2.0 Profile存储用户信息
- java如何获取AD域用户信息?
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用asp.net 2.0 profile存储用户信息[翻译]
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 使用ASP.NET 2.0 Profile存储用户信息[翻译] Level 200
- 在ASP.NET Atlas中使用Profile Service保存用户信息
- 使用ASP.NET 2.0 Profile存储用户信息_1
- Eclipse常用快捷键
- 忘记 VSS Admin 密码 !
- JbpmSide介绍
- 学习windows网络命令之:route命令小档案
- Ext上传图片
- 如何使用ASP.NET代码查看、修改AD用户信息
- Oracle 函数大全(字符串函数,数学函数,日期函数,逻辑运算函数,其他函数)
- jsp乱码问题解决方法3
- What is the difference between an application server and a Web server?
- java获取lsit内对象类型的方法
- 关于DOCTYPE
- 英语学习的困惑!
- 【学习笔记】--Java过滤器,用户登陆验证过滤等
- asp.net运行超时