关于AD的操作 --Uitil类

namespace TB.ADBlock
{
    using System;
    using System.Collections.Generic;
    using System.DirectoryServices;
    using System.Globalization;
    using System.Text;


    public class Utils
    {
        public static char[] InvalidSAMAccountNameChars = new char[] { '/', '\\', '[', ']', ':', ';', '|', '=', ',', '+', '*', '?', '<', '>', '"' };


        internal static bool CanAsContainer(DirectoryEntry de)
        {
            string schemaClassName = de.SchemaClassName;
            if (!(schemaClassName == SchemaClass.organizationalUnit.ToString("F")) && !(schemaClassName == SchemaClass.builtinDomain.ToString("F")))
            {
                return (schemaClassName == SchemaClass.container.ToString("F"));
            }
            return true;
        }


        public static string ConvertDNToDomainName(string dn)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string dc in dn.Split(new char[] { ',' }))
            {
                sb.AppendFormat("{0}.", dc.Trim().Split(new char[] { '=' })[1]);
            }
            return sb.ToString(0, sb.Length - 1);
        }


        public static string ConvertDomainNameToDN(string domainName)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string dc in domainName.Split(new char[] { '.' }))
            {
                sb.AppendFormat("DC={0},", dc.ToUpper());
            }
            return sb.ToString(0, sb.Length - 1);
        }


        public static string ConvertGuidToNativeGuid(Guid guid)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in guid.ToByteArray())
            {
                sb.AppendFormat("{0:x2}", b);
            }
            return sb.ToString();
        }


        public static Guid ConvertNativeGuidToGuid(string nativeGuid)
        {
            new StringBuilder();
            byte[] byteArr = new byte[0x10];
            for (int i = 0; i < 0x10; i++)
            {
                char CS$0$0000 = nativeGuid[2 * i];
                char CS$0$0001 = nativeGuid[(2 * i) + 1];
                byteArr[i] = byte.Parse(CS$0$0000.ToString().ToUpper() + CS$0$0001.ToString().ToUpper(), NumberStyles.HexNumber);
            }
            return new Guid(byteArr);
        }


        public static string ConvertToOctetString(byte[] ids)
        {
            StringBuilder sb = new StringBuilder();
            foreach (byte b in ids)
            {
                sb.AppendFormat(@"\{0:x2}", b);
            }
            return sb.ToString();
        }


        public static string Escape4Query(string cn)
        {
            return cn.Replace("(", @"\28").Replace(")", @"\29").Replace(@"\", @"\5c");
        }


        internal static string EscapeDN(string odn)
        {
            return odn.Replace(@"\", @"\\").Replace("#", @"\#").Replace("+", @"\+").Replace("=", @"\=").Replace("\"", "\\\"").Replace(">", @"\>").Replace("<", @"\<").Replace(",", @"\,").Replace(";", @"\;").Replace("/", @"\/");
        }


        public static string EscapeDNBackslashedChar(string odn)
        {
            return odn.Replace("/", @"\/");
        }


        public static string GenerateADsPath(Guid guid)
        {
            return ("LDAP://<GUID=" + ConvertGuidToNativeGuid(guid) + ">");
        }


        public static string GenerateDN(string rdn, string parentDN)
        {
            if (!string.IsNullOrEmpty(parentDN))
            {
                return string.Format("{0},{1}", rdn, parentDN);
            }
            return rdn;
        }


        public static string GenerateRDNCN(string cn)
        {
            return string.Format("CN={0}", EscapeDN(cn));
        }


        public static string GenerateRDNOU(string ou)
        {
            return string.Format("OU={0}", EscapeDN(ou));
        }


        public static string GetParentDN(string dn)
        {
            int i = 0;
            while (i < dn.Length)
            {
                if ((dn[i] == ',') && (dn[i - 1] != '\\'))
                {
                    break;
                }
                i++;
            }
            if (i == dn.Length)
            {
                return null;
            }
            return dn.Substring(i + 1);
        }


        internal static string GetProperty(DirectoryEntry de, string propertyName)
        {
            if (de.Properties.Contains(propertyName))
            {
                return de.Properties[propertyName][0].ToString();
            }
            return null;
        }


        internal static string GetProperty(SearchResult result, string propertyName)
        {
            if (result.Properties.Contains(propertyName))
            {
                return result.Properties[propertyName][0].ToString();
            }
            return null;
        }


        public static string GetRDN(string dn)
        {
            return SplitDN(dn)[0];
        }


        public static string GetRDNValue(string dn)
        {
            string part = SplitDN(dn)[0];
            return part.Substring(part.IndexOf('=') + 1).Trim();
        }


        internal static string RemoveLDAPPre(string adsPath)
        {
            return adsPath.Substring(7);
        }


        internal static void SetProperty(DirectoryEntry de, string propertyName, int propertyValue)
        {
            if (de.Properties.Contains(propertyName))
            {
                de.Properties[propertyName][0] = propertyValue;
            }
            else
            {
                de.Properties[propertyName].Add(propertyValue);
            }
        }


        internal static void SetProperty(DirectoryEntry de, string propertyName, string propertyValue)
        {
            if (de.Properties.Contains(propertyName))
            {
                if (string.IsNullOrEmpty(propertyValue))
                {
                    de.Properties[propertyName].Value = null;
                }
                else
                {
                    de.Properties[propertyName][0] = propertyValue;
                }
            }
            else if (!string.IsNullOrEmpty(propertyValue))
            {
                de.Properties[propertyName].Add(propertyValue);
            }
        }


        public static string[] SplitDN(string dn)
        {
            List<string> parts = new List<string>();
            int l = 0;
            int i = 0;
            while (i < dn.Length)
            {
                if ((dn[i] == ',') && (dn[i - 1] != '\\'))
                {
                    parts.Add(dn.Substring(l, i - l));
                    l = i + 1;
                }
                i++;
            }
            if (l < (i - 1))
            {
                string last = dn.Substring(l, i - l);
                if (!string.IsNullOrEmpty(last.Trim()))
                {
                    parts.Add(last.Trim());
                }
            }
            return parts.ToArray();
        }


        internal static string UnEscapeDN(string dn)
        {
            return dn.Replace(@"\#", "#").Replace(@"\+", "+").Replace(@"\=", "=").Replace("\\\"", "\"").Replace(@"\>", ">").Replace(@"\<", "<").Replace(@"\,", ",").Replace(@"\;", ";").Replace(@"\/", "/").Replace(@"\\", @"\");
        }


        public static string UnEscapeDNBackslashedChar(string dn)
        {
            return dn.Replace(@"\/", "/");
        }
    }
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值