C#实现windows用户用户管理(转)

using System;using System.DirectoryServices;// Be sure to set a reference to "System.DirectoryServices.dll"namespace AdTest// Change namespace for your project{// Structures for returning user informa
摘要由CSDN通过智能技术生成

using System;
using System.DirectoryServices;// Be sure to set a reference to "System.DirectoryServices.dll"

namespace AdTest// Change namespace for your project
{

// Structures for returning user information
public struct UserInfo
{
public string LoginName;
public string FirstName;
public string LastName;
}

public struct UserInfoEx
{
public string LoginName;
public string Password;
public string FirstName;
public string LastName;
public string EmailAddress;
public string Title;
public string Company;
public string Address;
public string City;
public string State;
public string PostalCode;
public string Phone;
public string Country;
}

// Static class containing all the supported user property names
public class UserProperty
{
public static string CommonName = "cn";
public static string Password = "homePhone";
public static string UserName = "sAMAccountName";
public static string Country = "co";
public static string Company = "company";
public static string Department = "department";
public static string Description = "description";
public static string DisplayName = "displayName";
public static string FirstName = "givenName";
public static string City = "l";
public static string Email = "mail";
public static string PostalCode = "postalCode";
public static string LastName = "sn";
public static string State = "st";
public static string Address = "streetAddress";
public static string Phone = "telephoneNumber";
public static string Title = "title";
}

// Active Directory Utility Class
public class ADUtil
{
public ADUtil()
{
//
// TODO: Add constructor logic here
//
}

#region Constants
// *** SECURE CONSTANTS ***
// Reality Check: In production, these would be stored in a secure are of the registry
// or another secure location. In production, instead of "Administrator", an account
// would be created which has ONLY the privileges it needs for the AD operations
// and no more.

// Domain Settings:
const string usersLdapPath = "LDAP://MYDOMAIN.local/CN=Users, DC=MYDOMAIN, DC=local";
const string adLoginName = "MYDOMAIN//Administrator";
const string adLoginPassword = "password";
#endregion

// GetUserCN - given the CMS user string, returns a friendly name for the user
static public string GetUserCN (string username)
{
DirectoryEntry usersDE =
new DirectoryEntry (usersLdapPath, adLoginName, adLoginPassword);
DirectorySearcher ds = new DirectorySearcher (usersDE);
ds.Filter = "(sAMAccountName=" + username + ")";
ds.PropertiesToLoad.Add (UserProperty.FirstName);
ds.PropertiesToLoad.Add (UserProperty.LastName);
SearchResult r = ds.FindOne();

return (r.Properties[UserProperty.FirstName][0].ToString()
    + " "
    + r.Properties[UserProperty.Last

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值