zoukankan      html  css  js  c++  java
  • ADHelper类

     纯属个人记录,不供大家学习。

    using System;

    using System.Collections.Generic;

    using System.Linq;

    using System.Text;

    using System.Configuration;

    using System.DirectoryServices;

     

    namespace DPForAD2BPMTel

    {

        public class ADHelper

        {

            public static String LDAPPath = ConfigurationManager.AppSettings["LDAPPath"];

            public static String Domain = ConfigurationManager.AppSettings["Domain"];

            public static String DomainUser = ConfigurationManager.AppSettings["DomainUser"];

            public static String DomainPass = ConfigurationManager.AppSettings["DomainPass"];

     

            /// <summary>

            /// 验证AD用户是否登陆成功

            /// </summary>

            /// <param name="domain">域名称</param>

            /// <param name="username">用户名</param>

            /// <param name="password">密码</param>

            /// <returns>返回登陆状态</returns>

            public static bool TryAuthenticate(string Account, string Password)

            {

                bool isLogin = false;

                try

                {

                    DirectoryEntry entry = new DirectoryEntry(string.Format(LDAPPath, Domain), Account, Password);

                    entry.RefreshCache();

                    isLogin = true;

                }

                catch

                {

                    isLogin = false;

                }

                return isLogin;

            }

     

            /// <summary>

            /// 设置指定的属性值

            /// </summary>

            /// <param name="de"></param>

            /// <param name="propertyName">属性名称?</param>

            /// <param name="propertyValue">属性值</param>

            public static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)

            {

                if (de.Properties.Contains(propertyName))

                {

                    if (String.IsNullOrEmpty(propertyValue))

                    {

                        de.Properties[propertyName].RemoveAt(0);

                    }

                    else

                    {

                        de.Properties[propertyName][0] = propertyValue;

                    }

                }

                else

                {

                    if (!String.IsNullOrEmpty(propertyValue))

                    {

                        de.Properties[propertyName].Add(propertyValue);

                    }

                }

            }

     

            /// <summary>

            /// 修改查询到的用户

            /// </summary>

            /// <param name="CommonName">通用名(displayName,系统中显示的中文字)</param>

            /// <param name="Account">帐户名(如Peter)</param>

            /// <param name="organizeName">组织单元名(资讯中心)</param>

            /// <param name="password">密码</param>

            public static bool ChangePassword(string Account, string NewPassword)

            {

                bool isLogin = false;

                try

                {

                    DirectoryEntry entry1 = new DirectoryEntry(LDAPPath, DomainUser, DomainPass, AuthenticationTypes.Secure);

                    Object obj = entry1.NativeObject;

                    DirectorySearcher search = new DirectorySearcher(entry1);

                    search.Filter = "(SAMAccountName=" + Account + ")";

                    search.PropertiesToLoad.Add("cn");

                    SearchResult result = search.FindOne();

                    DirectoryEntry user = result.GetDirectoryEntry();

     

                    SetProperty(user, "sAMAccountName", Account);

                    user.Invoke("SetPassword", new object[] { NewPassword });

                    user.CommitChanges();

                    isLogin = true;

                }

                catch

                {

                    isLogin = false;

                }

                return isLogin;

            }

     

            /// <summary>

            /// 根据员工ID获取对应AD域账号

            /// </summary>

            /// <param name="EmployeeID"></param>

            /// <returns></returns>

            public static string GetAccountByEmployeeID(string EmployeeID)

            {

                string Account = string.Empty;

                try

                {

                    DirectoryEntry entry = new DirectoryEntry(LDAPPath, DomainUser, DomainPass, AuthenticationTypes.Secure);

                    Object obj = entry.NativeObject;

                    DirectorySearcher search = new DirectorySearcher(entry);

                    search.Filter = "EmployeeID=" + EmployeeID;// "(SAMAccountName=qiu.fangbing)";

                    search.PropertiesToLoad.Add("cn");

                    SearchResult result = search.FindOne();

                    DirectoryEntry user = result.GetDirectoryEntry();

     

                    Account = Convert.ToString(user.Invoke("Get", new object[] { "SAMAccountName" }));

                    //string AD = user.Properties["SAMAccountName"].Value.ToString();

                    //string FullName = Convert.ToString(user.Invoke("Get", new object[] { "displayName" }));

                    //string Email = Convert.ToString(user.Invoke("Get", new object[] { "mail" }));               

                    //string Path = Convert.ToString(user.Invoke("Get", new object[] { "distinguishedName" }));

                }

                catch

                {

                    Account = string.Empty;

                }

                return Account;

            }

     

            /// <summary>

            /// 根据员工工号获取对应AD信息

            /// </summary>

            /// <param name="EmployeeID"></param>

            /// <returns></returns>

            public static ADInfo GetInfoByEmployeeID(string EmployeeID)

            {

                ADInfo info = new ADInfo();

                try

                {

                    DirectoryEntry entry = new DirectoryEntry(LDAPPath, DomainUser, DomainPass, AuthenticationTypes.Secure);

                    Object obj = entry.NativeObject;

                    DirectorySearcher search = new DirectorySearcher(entry);

                    search.Filter = "EmployeeID=" + EmployeeID;// "(SAMAccountName=qiu.fangbing)";

                    search.PropertiesToLoad.Add("cn");

                    SearchResult result = search.FindOne();

                    DirectoryEntry user = result.GetDirectoryEntry();

     

                    info.Account = Convert.ToString(user.Invoke("Get", new object[] { "SAMAccountName" }));

                    info.EmployeeID = Convert.ToString(user.Invoke("Get", new object[] { "EmployeeID" }));

                    info.Tel = Convert.ToString(user.Invoke("Get", new object[] { "telephoneNumber" }));

                    //info.DisplayName = Convert.ToString(user.Invoke("Get", new object[] { "displayName" }));

                    //info.Email = Convert.ToString(user.Invoke("Get", new object[] { "mail" }));

                    //info.Company = Convert.ToString(user.Invoke("Get", new object[] { "physicalDeliveryOfficeName" }));

                    //info.Center = Convert.ToString(user.Invoke("Get", new object[] { "company" }));

                    //info.Department = Convert.ToString(user.Invoke("Get", new object[] { "Department" }));

                    //info.Postion = Convert.ToString(user.Invoke("Get", new object[] { "title" }));

                    //info.Mobile = Convert.ToString(user.Invoke("Get", new object[] { "mobile" }));

                }

                catch

                {

                    info = null;

                }

                return info;

            }

        }

    }

  • 相关阅读:
    spark学习进度11(RDD分区和我shuffle以及缓存)
    spark学习进度10(阶段练习)
    gradle体验笔记
    git 进阶命令
    git 基础命令
    看日记学git--笔记
    git的objects目录
    macos中gitk报错
    第5章 迪米特法则(最少知知识原则)
    操作系统概念 第9版
  • 原文地址:https://www.cnblogs.com/qfb620/p/3117703.html
Copyright © 2011-2022 走看看