public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer) { JObject obj = JObject.Load(reader); IDictionary <UserDomainKey, ResolvedPrincipal> dict = (IDictionary <UserDomainKey, ResolvedPrincipal>)existingValue ?? new ConcurrentDictionary <UserDomainKey, ResolvedPrincipal>(); foreach (var prop in obj.Properties()) { var key = new UserDomainKey(); var split = prop.Name.Split('\\'); key.AccountDomain = split[0]; key.AccountName = split[1]; dict.Add(key, prop.Value.ToObject <ResolvedPrincipal>()); } return(dict); }
/// <summary> /// Attempts to turn an account name into its corresponding SID as well as determine the type of the object /// </summary> /// <param name="accountName"></param> /// <param name="accountDomain"></param> /// <returns></returns> internal static async Task <(bool success, string sid, LdapTypeEnum type)> ResolveAccountNameToSidAndType(string accountName, string accountDomain) { var domain = Helpers.NormalizeDomainName(accountDomain); //If we have a space in the domain name, its most likely NT AUTHORITY or some other variation, and its not a valid name either way. Ignore it if (domain.Contains(" ")) { return(false, null, LdapTypeEnum.Unknown); } var key = new UserDomainKey { AccountDomain = domain, AccountName = accountName }; if (Cache.Instance.GetResolvedAccount(key, out var principal)) { return(principal.ObjectIdentifier != null, principal.ObjectIdentifier, principal.ObjectType); } var searcher = Helpers.GetDirectorySearcher(domain); var result = await searcher.GetOne($"(samaccountname={accountName})", ResolutionProps, SearchScope.Subtree); if (result == null) { Cache.Instance.Add(key, new ResolvedPrincipal { ObjectIdentifier = null, ObjectType = LdapTypeEnum.Unknown }); return(false, null, LdapTypeEnum.Unknown); } var sid = result.GetSid(); var type = result.GetLdapType(); Cache.Instance.Add(key, new ResolvedPrincipal { ObjectIdentifier = sid, ObjectType = type }); return(sid != null, sid, type); }
internal void Add(UserDomainKey key, ResolvedPrincipal value) { _resolvedAccountNameDictionary.TryAdd(key, value); }
internal bool GetResolvedAccount(UserDomainKey key, out ResolvedPrincipal value) { return(_resolvedAccountNameDictionary.TryGetValue(key, out value)); }