/// <summary>
        /// To ease management, we need a one-to-one transformation from userPrincipalName to SamAccountName
        /// </summary>
        /// <param name="userPrincipalName">User principal name, must contain a prefix: prefix_accoutnname</param>
        /// <returns></returns>
        public static string SamAccountNameFromUserPrincipalName(string userPrincipalName)
        {
            if (!userPrincipalName.Contains("_"))
            {
                throw new Exception($"User name '{userPrincipalName}' does not start with a prefix and an underscore: prefix_username");
            }

            string samAccountName = userPrincipalName;
            string prefix         = userPrincipalName.Split("_".ToCharArray()).First() + "_";

            if (prefix.Length > 5)
            {
                throw new Exception($"The length of the prefix '{prefix}' exceeds the 4 character limit.");
            }

            string cleanName = string.Join("_", userPrincipalName.Split("_".ToCharArray()).Skip(1));

            // The maximum user length depends on if this is a local account, or a domain account.
            // https://serverfault.com/questions/105142/windows-server-2008-r2-change-the-maximum-username-length
            int maxUserNameLength = 20;

            if (samAccountName.Length > maxUserNameLength)
            {
                samAccountName = prefix + UtilsEncryption
                                 .GetMD5(cleanName)
                                 .Substring(0, maxUserNameLength - prefix.Length);
            }

            return(samAccountName);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Get the target destionation path
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        protected string GetDestinationPath(string key)
        {
            var invalids     = Path.GetInvalidFileNameChars();
            var sanitizedKey = string.Join("_", key.Split(invalids, StringSplitOptions.RemoveEmptyEntries)).TrimEnd('.');

            string fileName = this.UseKeyAsFileName ? sanitizedKey : UtilsEncryption.GetShortHash(key, 12);

            return(Path.Combine(this.StorageDir, fileName + ".json"));
        }