private static void EnsureOU(string dn, ADHelper adHelper)
        {
            if (string.IsNullOrEmpty(dn) == false)
            {
                string[] rdns = new RdnSequencePartEnumerator(dn).ToArray();

                for (int i = rdns.Length - 1; i >= 0; i--)
                {
                    if (rdns[i].StartsWith("OU=", StringComparison.Ordinal) == false)
                    {
                        throw new FormatException("DN格式错误。正确的格式为OU=ou1[,OU=ouBase][,...]");
                    }
                }

                string lastRoot = null;
                for (int i = rdns.Length - 1; i >= 0; i--)
                {
                    string root = rdns[i] + (lastRoot == null ? null : "," + lastRoot);
                    if (adHelper.EntryExists(root) == false)
                    {
                        using (DirectoryEntry entry = string.IsNullOrEmpty(lastRoot) ? adHelper.GetRootEntry() : adHelper.NewEntry(lastRoot))
                        {
                            using (DirectoryEntry here = entry.Children.Add(rdns[i], "organizationalUnit"))
                            {
                                here.CommitChanges();
                            }
                        }
                    }

                    lastRoot = root;
                }
            }
        }
Esempio n. 2
0
        private static StringBuilder GetEntryLongNameBuilder(DirectoryEntry entry, out string ldif)
        {
            string        dn      = entry.Properties["distinguishedName"].Value.ToString();
            StringBuilder builder = new StringBuilder(64);

            var de = new RdnSequencePartEnumerator(dn).GetEnumerator();

            ldif = null;

            if (de.MoveNext())
            {
                RdnAttribute attr = RdnAttribute.Parse(de.Current);
                builder.Append(attr.StringValue);
                builder.Append('$');
                ldif = attr.Ldif;
                while (de.MoveNext())
                {
                    attr = RdnAttribute.Parse(de.Current);
                    builder.Append(attr.StringValue);
                    builder.Append('$');
                }

                MakeTimePostfixToBuilderAndTrim(builder);

                return(builder);
            }
            else
            {
                throw new FormatException("DN格式不正确");
            }
        }
		private static StringBuilder GetEntryLongNameBuilder(DirectoryEntry entry, out string ldif)
		{
			string dn = entry.Properties["distinguishedName"].Value.ToString();
			StringBuilder builder = new StringBuilder(64);

			var de = new RdnSequencePartEnumerator(dn).GetEnumerator();
			ldif = null;

			if (de.MoveNext())
			{
				RdnAttribute attr = RdnAttribute.Parse(de.Current);
				builder.Append(attr.StringValue);
				builder.Append('$');
				ldif = attr.Ldif;
				while (de.MoveNext())
				{
					attr = RdnAttribute.Parse(de.Current);
					builder.Append(attr.StringValue);
					builder.Append('$');
				}

				MakeTimePostfixToBuilderAndTrim(builder);

				return builder;
			}
			else
				throw new FormatException("DN格式不正确");
		}
Esempio n. 4
0
 /// <summary>
 /// 提取当前RDN序列的上一级的RDN序列
 /// </summary>
 /// <param name="rdnSequence">当前级的RDN序列</param>
 /// <returns></returns>
 public static string GetParentRdnSequence(string rdnSequence)
 {
     var ie = new RdnSequencePartEnumerator(rdnSequence).GetEnumerator();
     if (ie.MoveNext())
     {
         int nextIndex = ie.Current.Length + 1;
         if (nextIndex < rdnSequence.Length)
         {
             return rdnSequence.Substring(nextIndex);
         }
         else
             return string.Empty;
     }
     else
     {
         throw new FormatException("无法提取上级路径");
     }
 }