?
Solved

Active Directory

Posted on 2009-12-30
3
Medium Priority
?
354 Views
Last Modified: 2013-12-24
Hey.. i am trying to unlock and reset password in the Active Directory..

The code is working fine for some of the employees...

but it is giving errors for some employee ids..

it is giving below errors
1) General Access Denied ---> No idea why access is denied to unlock some empid's
2) Exception has been thrown by object of invocation --> Not sure
3) Object reference not set to instance of an object... ---> Empid may not exist in Active Directory
0
Comment
Question by:gautam_reddyc
  • 2
3 Comments
 
LVL 33

Expert Comment

by:Todd Gerbert
ID: 26145726
Can you describe your application some more, and post some code?

Is this a web application, web service, windows console or forms app?

What is an employee ID, a property of a user object?
0
 

Author Comment

by:gautam_reddyc
ID: 26146220
it is a web service...

empid is the user object.. Key in the AD

0
 
LVL 33

Accepted Solution

by:
Todd Gerbert earned 2000 total points
ID: 26146447
So employee ID is their Active Directory username?  Like "jsmith" for John Smith?

Is your web service running as a user that has necessary access rights to modify all users in your domain?

Can you post your code that's not running as you expect, and your web.config?


Here's some code I use in a web service to set passwords and enable/disable users...
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.DirectoryServices;

namespace ConsoleApplication1
{
	class Class1
	{
		const string SEARCH_ROOT_PATH = "LDAP://DC=yourdomain,DC=com";

		[Flags]
		public enum AdsUserFlags : int
		{
			AccountDisabled = 2,                 // 0x2
		}

		public static DirectoryEntry GetUserEntry(string UserName)
		{
			// This function will search the domain for a user by username
			// and return it's directory entry if found, or null if not found
			DirectorySearcher dsSearcher = new DirectorySearcher(
				new DirectoryEntry(SEARCH_ROOT_PATH),
				"(&(objectCategory=user)(sAMAccountName=" + UserName + "))",
				new string[] { },
				SearchScope.Subtree);

			SearchResult result = dsSearcher.FindOne();

			if (result == null)
				return null;
			else
				return result.GetDirectoryEntry();
		}

		public static bool UserEnabled(string UserName, bool Enabled)
		{
			// This function marks a user account enabled or disabled
			// It returns true if successful, or false otherwise

			int curAccountControl;
			DirectoryEntry user = GetUserEntry(UserName);

			if (user == null)
				return false; // Couldn't find user, return false

			curAccountControl = (int)user.Properties["userAccountControl"].Value;

			if (Enabled)
				user.Properties["userAccountControl"].Value = curAccountControl & (int)~AdsUserFlags.AccountDisabled;
			else
				user.Properties["userAccountControl"].Value = curAccountControl | (int)AdsUserFlags.AccountDisabled;

			try
			{
				user.CommitChanges();
				user.Close();
			}
			catch (Exception)
			{
				return false;
			}

			return true;
		}

		public static bool SetPassword(string UserName, string NewPassword)
		{
			// This function resets a user password
			// Returns true if successful, false otherwise

			DirectoryEntry user = GetUserEntry(UserName);

			if (user == null)
				return false; // Couldn't find user, return false

			try
			{
				user.Invoke("SetPassword", new object[] { NewPassword });
				user.CommitChanges();
			}
			catch (Exception)
			{
				return false;
			}
			finally
			{
				user.Close();
			}

			return true;
		}
	}
}

Open in new window

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This post looks at MongoDB and MySQL, and covers high-level MongoDB strengths, weaknesses, features, and uses from the perspective of an SQL user.
What we learned in Webroot's webinar on multi-vector protection.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

862 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question