Actually I think I spoke to soon about fixing the issue :(
Something very wierd is going on .... I have now tested several different user & group management functions, and some work and others don't. Whether they work or not is affected by 2 things: (i) whether the SAM store on the same server as your ASP.NET app or on a remote server, and (ii) whether the machine/host name or machine IP address is specified in the PrincipalContext.
Here are my findings. If anyone can explain this and suggest a solution I would be verrrry grateful.
FYI, all functions were run through a PrincipalContext set up as follows:
static PrincipalContext _PrincipalContext;
_PrincipalContext = new PrincipalContext(ContextType.Machine, _WindowsServerName, null, "LocalAdminUsername", "LocalAdminPassword");
The ASP.NET process was running under the regular (non-admin) NETWORK SERVICE acccount.
|
|
SAM store on the same server as your ASP.NET app |
SAM store on a remote server from your ASP.NET app |
|
Function |
machine name in the PrincipalContext |
machine IP address in the PrincipalContext |
host name in the PrincipalContext (name set up in Hosts file) |
machine IP address in the PrincipalContext |
|
Creating a user |
"General access denied” * error |
OK |
OK |
OK |
|
Checking if a user exists |
OK |
OK |
OK |
OK |
|
Checking if a group exists |
OK |
OK |
OK |
OK |
|
Checking if a user is in a group |
OK |
always returns false |
OK |
always returns false |
|
Adding a user to a group |
OK |
OK |
OK |
OK |
|
Removing a user from a group |
"General access denied” ** error |
does not error, but no change is made |
OK |
does not error, but no change is made |
|
Deleting a user |
"General access denied” *** error |
OK |
OK |
OK |
|
Disabling & re-enabling a user |
"General access denied” **** error |
OK |
OK |
OK |
|
Setting user password |
"Exception thrown” ***** error |
OK |
OK |
OK |
* General access denied error ---> System.UnauthorizedAccessException: General access denied error at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.SetInfo() at System.DirectoryServices.DirectoryEntry.CommitChanges() at System.DirectoryServices.AccountManagement.SDSUtils.ApplyChangesToDirectory(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes) at System.DirectoryServices.AccountManagement.SDSUtils.InsertPrincipal(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes, Boolean needToSetPassword) at System.DirectoryServices.AccountManagement.SAMStoreCtx.Insert(Principal p) at System.DirectoryServices.AccountManagement.Principal.Save()
** General access denied error ---> at System.DirectoryServices.AccountManagement.UnsafeNativeMethods.IADsGroup.Remove(String bstrItemToBeRemoved) at System.DirectoryServices.AccountManagement.SAMStoreCtx.UpdateGroupMembership(Principal group, DirectoryEntry de, NetCred credentials, AuthenticationTypes authTypes) at System.DirectoryServices.AccountManagement.SDSUtils.ApplyChangesToDirectory(Principal p, St
*** General access denied error ---> at System.DirectoryServices.Interop.UnsafeNativeMethods.IAdsContainer.Delete(String className, String relativeName) at System.DirectoryServices.DirectoryEntries.Remove(DirectoryEntry entry) at System.DirectoryServices.AccountManagement.SDSUtils.DeleteDirectoryEntry(DirectoryEntry deToDelete) at System.DirectoryServices.AccountManagement.SAMStoreCtx.Delete(Principal p)
**** General access denied error ---> at System.DirectoryServices.Interop.UnsafeNativeMethods.IAds.SetInfo() at System.DirectoryServices.DirectoryEntry.CommitChanges() at System.DirectoryServices.AccountManagement.SDSUtils.ApplyChangesToDirectory(Principal p, StoreCtx storeCtx, GroupMembershipUpdater updateGroupMembership, NetCred credentials, AuthenticationTypes authTypes)
***** Exception has been thrown by the target of an invocation. ---> at System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args) at System.DirectoryServices.AccountManagement.SDSUtils.SetPassword(DirectoryEntry de, String newPassword) at System.DirectoryServices.AccountManagement.SAMStoreCtx.SetPassword(AuthenticablePrincipal p, String newPassword)