您的位置:首页 > 编程语言 > C#

AD帐户操作C#示例代码(二)——检查密码将过期的用户

2014-04-15 22:26 1101 查看
本文接着和大家分享AD帐户操作,这次开发一个简单的检查密码将过期用户的小工具。

using (SmtpClient smtpClient = new SmtpClient())
{
if (!string.IsNullOrEmpty(mailMessageInfo.Host))
{
smtpClient.Host = mailMessageInfo.Host;
}
if (!string.IsNullOrEmpty(mailMessageInfo.Port))
{
smtpClient.Port = int.Parse(mailMessageInfo.Port);
}
smtpClient.Credentials = new System.Net.NetworkCredential();
if (!string.IsNullOrEmpty(mailMessageInfo.UserName))
{
NetworkCredential networkCredential = new NetworkCredential { UserName = mailMessageInfo.UserName };
if (!string.IsNullOrEmpty(mailMessageInfo.PassWord))
{
networkCredential.Password = mailMessageInfo.PassWord;
}
smtpClient.Credentials = networkCredential;
}
MailMessage mailMessage = new MailMessage();
if (!string.IsNullOrEmpty(mailMessageInfo.From))
{
mailMessage.From = new MailAddress(mailMessageInfo.From);
}
foreach (string to in mailMessageInfo.ToList)
{
if (!string.IsNullOrWhiteSpace(to))
{
mailMessage.To.Add(to);
}
}
if (mailMessageInfo.CcList != null && mailMessageInfo.CcList.Length > 0)
{
foreach (string cc in mailMessageInfo.CcList)
{
if (!string.IsNullOrWhiteSpace(cc))
{
mailMessage.To.Add(cc);
}
}
}
mailMessage.IsBodyHtml = true;
string path = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "templates", mailMessageInfo.TemplateFileName);
string body = File.ReadAllText(path);
Regex regexImg = new Regex(@"<img\s[^>]*>", RegexOptions.IgnoreCase);
Regex regexSrc = new Regex(
@"src=(?:(['""])(?<src>(?:(?!\1).)*)\1|(?<src>[^\s>]+))",
RegexOptions.IgnoreCase | RegexOptions.Singleline);
MatchCollection matchCollection = regexImg.Matches(body);
Dictionary<string, string> contentIds = new Dictionary<string, string>();
foreach (Match matchImg in matchCollection)
{
if (regexSrc.IsMatch(matchImg.Groups[0].Value))
{
Match matchSrc = regexSrc.Match(matchImg.Groups[0].Value);
string srcValue = matchSrc.Groups["src"].Value;
if (!srcValue.StartsWith("http:", System.StringComparison.OrdinalIgnoreCase)
&& !srcValue.StartsWith("file:", System.StringComparison.OrdinalIgnoreCase))
{
if (srcValue.IndexOf("/") == 0)
{
srcValue = srcValue.Substring(1);
}
string attachmentContentId = Path.GetFileName(srcValue).Replace(".", string.Empty);
body = body.Replace(matchSrc.Groups["src"].Value, "cid:" + attachmentContentId);
if (!contentIds.ContainsKey(attachmentContentId))
{
string inlinePath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "templates", srcValue.Replace(@"/", @"\"));
Attachment inline = new Attachment(inlinePath);
inline.ContentDisposition.Inline = true;
inline.ContentDisposition.DispositionType = DispositionTypeNames.Inline;
inline.ContentId = attachmentContentId;
if (srcValue.EndsWith("gif", StringComparison.OrdinalIgnoreCase))
{
inline.ContentType.MediaType = MediaTypeNames.Image.Gif;
}
else
{
inline.ContentType.MediaType = MediaTypeNames.Image.Jpeg;
}
inline.ContentType.Name = Path.GetFileName(inlinePath);
mailMessage.Attachments.Add(inline);
contentIds.Add(attachmentContentId, null);
}
}
}
}
mailMessage.Body = body; ;
mailMessage.BodyEncoding = Encoding.UTF8;
mailMessage.Subject = mailMessageInfo.Subject;
mailMessage.SubjectEncoding = Encoding.UTF8;
smtpClient.Send(mailMessage);
}


View Code

最后,将这些代码整合起来,就是检查用户密码过期的小工具了!由于笔者水平有限,文中难免会有些疏漏和错误,代码也有待不断优化,欢迎各位高手提出宝贵的建议!

参考资料:

DirectoryEntry 类使用 http://msdn.microsoft.com/zh-cn/library/z9cddzaa(v=vs.110).aspx

DirectorySearcher 类使用 http://msdn.microsoft.com/zh-cn/library/System.DirectoryServices.DirectorySearcher(v=vs.90).aspx

轻量目录访问协议 (LDAP) http://msdn.microsoft.com/en-us/library/aa367008(VS.85).aspx

检查密码将过期用户小工具下载地址:http://files.cnblogs.com/CSharpDevelopers/UserPasswordSetChecker.zip
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: