Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
We have to check if an entity is a domain user or Domain user group in a WCF service running in a specific account. In order to do that we are using the following code:

important note: the service call is impersonated so that client identity is retrived
C#
// set up domain context
PrincipalContext ctx = newPrincipalContext(ContextType.Domain,domain);
 
// find is entity is a user
UserPrincipal user = UserPrincipal.FindByIdentity(ctx, entityname);

// find if the entity is a usergroupn
GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, entityname);

This code is working fine if the service and client is running in the same machine.

However if a client tries to connect the service from a remote machine in Findbyidentity we are getting the following exception

SystemException -> While resolving : 
000004DC: LdapErr: DSID-0C0907C2, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580

call stack :
at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
   at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
   at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)

Now we want to know how is this happening? and going one more step can we get the WindowsIdentity of an user/entity from its name/SID, as if we get this the resolving technique specified can be directly take from WindowsIdentity.

What I have tried:

In order to do that we are using the following code:

important note: the service call is impersonated so that client identity is retrived

// set up domain context

PrincipalContext ctx = newPrincipalContext(ContextType.Domain,domain);
 
// find is entity is a user

UserPrincipal user = UserPrincipal.FindByIdentity(ctx, entityname);

// find if the entity is a usergroupn

GroupPrincipal group = GroupPrincipal.FindByIdentity(ctx, entityname);

This code is working fine if the service and client is running in the same machine.

However if a client tries to connect the service from a remote machine in Findbyidentity we are getting the following exception

SystemException -> While resolving : 
000004DC: LdapErr: DSID-0C0907C2, comment: In order to perform this operation a successful bind must be completed on the connection., data 0, v2580 

call stack :

   at System.DirectoryServices.DirectoryEntry.Bind(Boolean throwIfFail)
   at System.DirectoryServices.DirectoryEntry.Bind()
   at System.DirectoryServices.DirectoryEntry.get_AdsObject()
   at System.DirectoryServices.PropertyValueCollection.PopulateList()
   at System.DirectoryServices.PropertyValueCollection..ctor(DirectoryEntry entry, String propertyName)
   at System.DirectoryServices.PropertyCollection.get_Item(String propertyName)
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoLDAPDirectoryInitNoContainer()
   at System.DirectoryServices.AccountManagement.PrincipalContext.DoDomainInit()
   at System.DirectoryServices.AccountManagement.PrincipalContext.Initialize()
   at System.DirectoryServices.AccountManagement.PrincipalContext.get_QueryCtx()
   at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithTypeHelper(PrincipalContext context, Type principalType, Nullable`1 identityType, String identityValue, DateTime refDate)
   at System.DirectoryServices.AccountManagement.Principal.FindByIdentityWithType(PrincipalContext context, Type principalType, String identityValue)
Posted
Updated 11-Jun-19 1:42am
v2

1 solution

You probably need to authenticate, see answers here: Solved: Does LDAP require a login account?[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900