internal void CommitIfNotCaching() { if (justCreated) { return; // Do not write changes, beacuse the entry is just under construction until CommitChanges() is called. } if (useCache) { return; } if (!Bound) { return; } if (!writeGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Write, this.path); permission.Demand(); writeGranted = true; } adsObject.SetInfo(); }
/// <include file='doc\DirectoryEntry.uex' path='docs/doc[@for="DirectoryEntry.CommitChanges"]/*' /> /// <devdoc> /// <para> /// Saves any /// changes to the entry in the directory store. /// </para> /// </devdoc> public void CommitChanges() { if (justCreated) { // Note: Permissions Demand is not necessary here, because entry has already been created with appr. permissions. // Write changes regardless of Caching mode to finish construction of a new entry. adsObject.SetInfo(); justCreated = false; return; } if (!useCache) { // nothing to do return; } if (!Bound) { return; } if (!writeGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Write, this.path); permission.Demand(); writeGranted = true; } adsObject.SetInfo(); }
// Create new entry with the same data, but different IADs object, and grant it Browse Permission. internal DirectoryEntry CloneBrowsable() { if (!browseGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Browse, this.path); permission.Demand(); browseGranted = true; } DirectoryEntry newEntry = new DirectoryEntry(this.Path, this.UsePropertyCache, this.Username, this.Password, this.AuthenticationType); newEntry.browseGranted = true; return(newEntry); }
/// <include file='doc\DirectoryEntry.uex' path='docs/doc[@for="DirectoryEntry.DeleteTree"]/*' /> /// <devdoc> /// <para>Deletes this entry and its entire subtree from the /// Active Directory hierarchy.</para> /// </devdoc> public void DeleteTree() { if (!(AdsObject is UnsafeNativeMethods.IAdsDeleteOps)) { throw new InvalidOperationException(Res.GetString(Res.DSCannotDelete)); } if (!writeGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Write, this.path); permission.Demand(); writeGranted = true; } UnsafeNativeMethods.IAdsDeleteOps entry = (UnsafeNativeMethods.IAdsDeleteOps)AdsObject; entry.DeleteObject(0); }
/// <include file='doc\DirectoryEntry.uex' path='docs/doc[@for="DirectoryEntry.CopyTo1"]/*' /> /// <devdoc> /// <para> /// Creates a copy of this entry as a child of the given parent and /// gives it a new name. /// </para> /// </devdoc> public DirectoryEntry CopyTo(DirectoryEntry newParent, string newName) { if (!newParent.IsContainer) { throw new InvalidOperationException(Res.GetString(Res.DSNotAContainer, newParent.Path)); } if (!newParent.writeGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Write, newParent.path); permission.Demand(); newParent.writeGranted = true; } object copy = newParent.ContainerObject.CopyHere(Path, newName); return(new DirectoryEntry(copy, newParent.UsePropertyCache, Username, Password, AuthenticationType)); }
/// <include file='doc\DirectoryEntry.uex' path='docs/doc[@for="DirectoryEntry.Invoke"]/*' /> /// <devdoc> /// <para>Calls /// a method on the native Active Directory.</para> /// </devdoc> public object Invoke(string methodName, params object[] args) { if (!writeGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Write, this.path); permission.Demand(); writeGranted = true; } object target = this.NativeObject; Type type = target.GetType(); object result = type.InvokeMember(methodName, BindingFlags.InvokeMethod, null, target, args); if (result is UnsafeNativeMethods.IAds) { return(new DirectoryEntry(result, UsePropertyCache, Username, Password, AuthenticationType)); } else { return(result); } }
private void Bind(bool throwIfFail) { //Cannot rebind after the object has been disposed, since finalization has been suppressed. if (this.disposed) { throw new ObjectDisposedException(GetType().Name); } if (Path != null && Path.Length != 0) { //SECREVIEW: Need to demand permission event if adsObject is not null // this entry might be the result of a search, need to verify // if the user has permission to browse the object first. if (!browseGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Browse, Path); permission.Demand(); browseGranted = true; } } if (adsObject == null) { string pathToUse = Path; if (pathToUse == null || pathToUse.Length == 0) { // get the default naming context. This should be the default root for the search. DirectoryEntry rootDSE = new DirectoryEntry("LDAP://RootDSE"); //SECREVIEW: Looking at the root of the DS will demand browse permissions // on "*" or "LDAP://RootDSE". string defaultNamingContext = (string)rootDSE.Properties["defaultNamingContext"][0]; rootDSE.Dispose(); pathToUse = "LDAP://" + defaultNamingContext; if (!browseGranted) { DirectoryServicesPermission permission = new DirectoryServicesPermission(DirectoryServicesPermissionAccess.Browse, pathToUse); permission.Demand(); browseGranted = true; } } // Ensure we've got a thread model set, else CoInitialize() won't have been called. if (Thread.CurrentThread.ApartmentState == ApartmentState.Unknown) { Thread.CurrentThread.ApartmentState = ApartmentState.MTA; } Guid g = new Guid("00000000-0000-0000-c000-000000000046"); // IID_IUnknown object value = null; int hr = UnsafeNativeMethods.ADsOpenObject(pathToUse, Username, Password, (int)authenticationType, ref g, out value); if (hr != 0) { if (throwIfFail) { throw CreateFormattedComException(hr); } } else { adsObject = (UnsafeNativeMethods.IAds)value; } } }