public override bool Equals(object o) { if (!(o is ADStoreKey)) { return(false); } ADStoreKey that = (ADStoreKey)o; if (_wellKnownSid != that._wellKnownSid) { return(false); } if (!_wellKnownSid) { if (_objectGuid == that._objectGuid) { return(true); } } else { if ((string.Equals(_domainName, that._domainName, StringComparison.OrdinalIgnoreCase)) && (Utils.AreBytesEqual(_sid, that._sid))) { return(true); } } return(false); }
public override bool Equals(object o) { if (o as ADStoreKey != null) { ADStoreKey aDStoreKey = (ADStoreKey)o; if (this.wellKnownSid == aDStoreKey.wellKnownSid) { if (this.wellKnownSid) { if (string.Compare(this.domainName, aDStoreKey.domainName, StringComparison.OrdinalIgnoreCase) == 0 && Utils.AreBytesEqual(this.sid, aDStoreKey.sid)) { return(true); } } else { if (this.objectGuid == aDStoreKey.objectGuid) { return(true); } } return(false); } else { return(false); } } else { return(false); } }
static public void TestADStoreKeys() { // GUID-based ADStoreKeys Guid adGuid1 = Guid.NewGuid(); Guid adGuid2 = Guid.NewGuid(); ADStoreKey key1a = new ADStoreKey(adGuid1); ADStoreKey key1b = new ADStoreKey(adGuid1); ADStoreKey key2 = new ADStoreKey(adGuid2); Debug.Assert(key1a.Equals(key1b)); Debug.Assert(!key1a.Equals(key2)); Debug.Assert(key1a.GetHashCode() == key1b.GetHashCode()); Debug.Assert(key1a.GetHashCode() != key2.GetHashCode()); // SID-based ADStoreKeys string mach1 = "machine1"; string mach1a = "machine1"; string mach2 = "machine2"; byte[] sid1a = new byte[] { 0x1, 0x2, 0x3 }; byte[] sid1b = new byte[] { 0x1, 0x2, 0x3 }; byte[] sid2 = new byte[] { 0x10, 0x20, 0x30 }; ADStoreKey key3a = new ADStoreKey(mach1, sid1a); ADStoreKey key3b = new ADStoreKey(mach1, sid1a); ADStoreKey key3c = new ADStoreKey(mach1a, sid1b); ADStoreKey key4 = new ADStoreKey(mach2, sid2); ADStoreKey key5 = new ADStoreKey(mach1, sid2); Debug.Assert(key3a.Equals(key3b)); Debug.Assert(key3a.Equals(key3c)); Debug.Assert(!key3a.Equals(key4)); Debug.Assert(!key3a.Equals(key5)); Debug.Assert(!key3a.Equals(key1a)); Debug.Assert(!key1a.Equals(key3a)); Debug.Assert(key3a.GetHashCode() != key4.GetHashCode()); // Shouldn't matter, since SAMStoreKey should make a copy of the byte[] sid sid1b[1] = 0xf; Debug.Assert(key3a.Equals(key3b)); Debug.Assert(key3a.Equals(key3c)); }
// Given a underlying store object (e.g., DirectoryEntry), further narrowed down a discriminant // (if applicable for the StoreCtx type), returns a fresh instance of a Principal // object based on it. The WinFX Principal API follows ADSI-style semantics, where you get multiple // in-memory objects all referring to the same store pricipal, rather than WinFS semantics, where // multiple searches all return references to the same in-memory object. // Used to implement the reverse wormhole. Also, used internally by FindResultEnumerator // to construct Principals from the store objects returned by a store query. // // The Principal object produced by this method does not have all the properties // loaded. The Principal object will call the Load method on demand to load its properties // from its Principal.UnderlyingObject. // // // This method works for native objects from the store corresponding to _this_ StoreCtx. // Each StoreCtx will also have its own internal algorithms used for dealing with cross-store objects, e.g., // for use when iterating over group membership. These routines are exposed as // ResolveCrossStoreRefToPrincipal, and will be called by the StoreCtx's associated ResultSet // classes when iterating over a representation of a "foreign" principal. // This method will either be passed a DirectoryEntry or SearchResult object if this is the result of a search. // We need to determine the type and then use the appropriate object. internal override Principal GetAsPrincipal(object storeObject, object discriminant) { Debug.Assert(storeObject != null); DirectoryEntry de = null; SearchResult sr = null; string path; string distinguishedName; if (storeObject is DirectoryEntry) { de = (DirectoryEntry)storeObject; path = de.Path; distinguishedName = (string)de.Properties["distinguishedName"].Value; } else { Debug.Assert(storeObject is SearchResult); sr = (SearchResult)storeObject; path = sr.Path; distinguishedName = (string)sr.Properties["distinguishedName"][0]; } GlobalDebug.WriteLineIf(GlobalDebug.Info, "ADStoreCtx", "GetAsPrincipal: using path={0}", (null != de ? de.Path : "searchResult")); // Construct a appropriate Principal object. bool targetIsFromGC = SDSUtils.IsObjectFromGC(path); try { Principal p; DirectoryEntry dcEntry = null; PrincipalContext constructedContext = null; // if the object was obtained from a GC, we have to construct a new context, and build a new DirectoryEntry // if the object is not from a GC but belongs to another domain, we just have to construct a new context. We can still use the storeObject or searchresult's DirectoryEntry. // if our context is not a domain (that is, it's ADLDS) we don't build a new context unless the object was obtained from a GC. if (targetIsFromGC || OwningContext.ContextType == ContextType.Domain) { string dnsDomainName = SDSUtils.ConstructDnsDomainNameFromDn(distinguishedName); if (targetIsFromGC || (!String.IsNullOrEmpty(this.domainDnsName) && String.Compare(this.DnsDomainName, dnsDomainName, StringComparison.OrdinalIgnoreCase) != 0)) { constructedContext = SDSCache.Domain.GetContext(dnsDomainName, this.Credentials, this.OwningContext.Options); } if (targetIsFromGC) { dcEntry = SDSUtils.BuildDirectoryEntry("LDAP://" + dnsDomainName + "/" + GetEscapedDN(distinguishedName), this.Credentials, this.authTypes); this.InitializeNewDirectoryOptions(dcEntry); } } if (de != null) { //NOTE: If target is GC then we do NOT use variable "de". So, need to dispose this at the end. p = SDSUtils.DirectoryEntryToPrincipal((targetIsFromGC ? dcEntry : de), constructedContext ?? this.OwningContext, (Type)discriminant); } else { //NOTE: If input storeObject is searchResult then variable "de" gets used in all cases. p = SDSUtils.SearchResultToPrincipal(sr, constructedContext ?? this.OwningContext, (Type)discriminant); p.UnderlyingObject = (targetIsFromGC ? dcEntry : sr.GetDirectoryEntry()); } Debug.Assert(p != null); Guid g; if (de != null) g = de.Guid; else { byte[] guid = (byte[])sr.Properties["objectGuid"][0]; g = new Guid(guid); } ADStoreKey key = new ADStoreKey(g); p.Key = key; return p; } catch (System.Runtime.InteropServices.COMException e) { throw ExceptionHelper.GetExceptionFromCOMException(e); } finally { if (targetIsFromGC && de != null) { //Since target is GC, variable "de" is not getting used. Disposing it. de.Dispose(); } } }