0

I'm trying to do a couple of things. First off this c# program verifies against Active Directory user credentials using:

var ADentry = new DirectoryEntry("LDAP://domain", uname, pword);

But obviously you need to pass in the username and password somehow. Is there a way that you can retrieve it automatically when the user signs in on the network from Active Directory and use that in the fields without having the username type in the username and password.

If not, I made it so the user can type in their credentials in the console. But if it doesn't work it ends up hanging forever. What type of code can I use to timeout after say 1 minute if this keeps hanging otherwise it hangs forever? thanks

Sirus
  • 382
  • 1
  • 8
  • 35
  • You have posted two questions in one - splitting them into two would be better. Your second question is answered here: http://stackoverflow.com/questions/57615/how-to-add-a-timeout-to-console-readline – ChrisWue Nov 04 '11 at 22:26

1 Answers1

0

I was trying to do an LDAP query and the following will do it for you. Most of it are methods but you may be interested in this line of code: PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.Negotiate);

The full code is here:

IsUserGroupMember("username", "group name the user is in");

public string sDomain = "your.domainName.com"
public string sDefaultOU = OU=**,OU=**,DC=**,DC=**,DC=**

        public void IsUserGroupMember(string sUserName, string sGroupName)
        {
            try
            {
                UserPrincipal oUserPrincipal = GetUser(sUserName);
                GroupPrincipal oGroupPrincipal = GetGroup(sGroupName);

                if (oUserPrincipal != null && oGroupPrincipal != null)
                {
                    //do something
                }
                else
                {
                    //nothing
                }
            }
            catch (PrincipalServerDownException)
            {
                throw;

            }
            catch (Exception)
            {
                throw;
            }
        }


public UserPrincipal GetUser(String sUserName)
        {
            PrincipalContext oPrincipalContext = GetPrincipalContext();

            UserPrincipal oUserPrincipal = UserPrincipal.FindByIdentity(oPrincipalContext, sUserName);
            return oUserPrincipal;


        }

        public GroupPrincipal GetGroup(string sGroupName)
        {
            PrincipalContext oPrincipalContext = GetPrincipalContext();

            GroupPrincipal oGroupPrincipal = GroupPrincipal.FindByIdentity(oPrincipalContext, sGroupName);
            return oGroupPrincipal;
        }

        public PrincipalContext GetPrincipalContext()
        {
            PrincipalContext oPrincipalContext = new PrincipalContext(ContextType.Domain, sDomain, sDefaultOU, ContextOptions.Negotiate);
            return oPrincipalContext;
        }

I hope this code will be of use for you.

Dan Ergis
  • 375
  • 1
  • 5
  • 15