/// <summary> /// Unifies the name of the account. /// </summary> /// <param name="accessCredential">The access credential.</param> /// <param name="defaultDomain">The default domain.</param> /// <param name="omitException">The omit exception.</param> /// <param name="domainMapping">The domain mapping.</param> /// <returns>Beyova.AccessCredential.</returns> public static AccessCredential UnifyAccountName(this AccessCredential accessCredential, string defaultDomain = null, bool omitException = false, Dictionary <string, string> domainMapping = null) { try { accessCredential.CheckNullObject(nameof(accessCredential)); accessCredential.AccessIdentifier.CheckEmptyString(accessCredential.AccessIdentifier); string domain, accountName; if (accessCredential.AccessIdentifier.IsEmailAddress()) { accountName = accessCredential.AccessIdentifier.SubStringBeforeFirstMatch('@'); domain = accessCredential.AccessIdentifier.SubStringAfterFirstMatch('@').TrimEnd(".com", true); string mappedDomain; if (domainMapping != null && domainMapping.TryGetValue(domain, out mappedDomain)) { domain = mappedDomain; } } else if (accessCredential.AccessIdentifier.Contains('\\')) { accountName = accessCredential.AccessIdentifier.SubStringAfterFirstMatch('\\'); domain = accessCredential.AccessIdentifier.SubStringBeforeFirstMatch('\\'); } else { domain = accessCredential.Domain; accountName = accessCredential.AccessIdentifier; } accountName.CheckEmptyString(nameof(accountName)); domain = domain.SafeToString(defaultDomain); return(new AccessCredential { Domain = defaultDomain, AccessIdentifier = accountName, Token = accessCredential.Token }); } catch (Exception ex) { if (omitException) { return(null); } throw ex.Handle(new { accessCredential, defaultDomain }); } }
/// <summary> /// Gets the domain account. /// </summary> /// <param name="accessCredential">The access credential.</param> /// <param name="defaultDomain">The default domain.</param> /// <param name="omitException">The omit exception.</param> /// <param name="domainMapping">The domain mapping.</param> /// <returns>System.String.</returns> public static string GetDomainAccount(this AccessCredential accessCredential, string defaultDomain = null, bool omitException = false, Dictionary <string, string> domainMapping = null) { var unifiedAccount = UnifyAccountName(accessCredential, defaultDomain, omitException, domainMapping); return(unifiedAccount != null?string.Format("{0}\\{1}", unifiedAccount.Domain, unifiedAccount.AccessIdentifier) : null); }