Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="userPrincipalName"></param>
        /// <param name="preWindows2000"></param>
        /// <returns></returns>
        public string FormatUserNameForPrincipal(string userPrincipalName, bool preWindows2000)
        {
            // If this is a domain principal, use a domain name.
            if (this.ContextType == nameof(System.DirectoryServices.AccountManagement.ContextType.Domain))
            {
                string domainName = this.DomainName;

                if (string.IsNullOrWhiteSpace(domainName))
                {
                    domainName = Domain.GetComputerDomain().Name;

                    // For compatiblity with SQL Server and legacy apps, use main DC.
                    // As sometimes domain name will return DC1.DC2.DC3
                    // and we only need DC1
                    domainName = domainName.Split(".".ToCharArray()).First().ToUpper();
                }

                if (preWindows2000)
                {
                    string mainDomain     = domainName.Split(".".ToCharArray()).First().ToUpper();
                    string samAccountName = UtilsWindowsAccounts.SamAccountNameFromUserPrincipalName(userPrincipalName);
                    return($"{mainDomain}\\{samAccountName}");
                }
                else
                {
                    return($"{userPrincipalName}@{domainName}");
                }
            }

            return(UtilsWindowsAccounts.SamAccountNameFromUserPrincipalName(userPrincipalName));
        }
Пример #2
0
        /// <summary>
        /// Get an instance of FqdnNameParser
        /// </summary>
        /// <param name="name"></param>
        /// <param name="autoCalculateSamAccountName">Usernames used by CHEF al have an autocalculated sam account name</param>
        public FqdnNameParser(string name, bool autoCalculateSamAccountName = true)
        {
            // Backwards compatibility fix
            if (name.StartsWith("sid:"))
            {
                name = name.Replace("sid:", string.Empty).Trim();
            }

            if (name.Contains("@"))
            {
                this.DomainName = name.Split("@".ToCharArray()).Last();
                name            = name.Split("@".ToCharArray()).First();
            }

            if (name.Contains("\\"))
            {
                this.DomainName = name.Split("\\".ToCharArray()).First();
                name            = name.Split("\\".ToCharArray()).Last();
            }

            if (UtilsSystem.IsValidSid(name))
            {
                // Assume we are using the user principal name, we cannot determine the context here...
                this.Sid = new SecurityIdentifier(name);
            }
            else
            {
                this.UserPrincipalName = name;

                if (autoCalculateSamAccountName)
                {
                    this.SamAccountName = UtilsWindowsAccounts.SamAccountNameFromUserPrincipalName(this.UserPrincipalName);
                }
            }

            if (name.StartsWith("#"))
            {
                throw new Exception("Unsupported prefix # used in identity definition.");

                // Assume we are using the user principal name, we cannot determine the context here...
                // this.UserPrincipalName = name.Replace("#", string.Empty);
                // this.SamAccountName = name.Replace("#", string.Empty);
            }

            if (!string.IsNullOrWhiteSpace(this.DomainName))
            {
                this.ContextType = (Environment.MachineName == this.DomainName || this.DomainName.Equals("localhost", StringComparison.CurrentCultureIgnoreCase))
                    ? System.DirectoryServices.AccountManagement.ContextType.Machine
                    : System.DirectoryServices.AccountManagement.ContextType.Domain;
            }
        }