UnauthorizedAccessException when updating a directory service entry from ASP.NET

Tech-Archive recommends: Fix windows errors by optimizing your registry



Hi

When I run this code from a ASP.NET website I get an
"System.UnauthorizedAccessException: General access denied error
" message when the Invoke method is called. The WindowsIdentity.GetCurrent()
is in the local administrator group. I tried running this with the <identity
impersonate="true"/> set i nthe web.config file but get the same error. The
OS is Windows Server 2008 x64, IIS 7.


var identity = WindowsIdentity.GetCurrent();
if (identity != null) Debug.WriteLine(identity.Name);

if (identity != null)
{
WindowsImpersonationContext impersonationContext =
identity.Impersonate();

//Insert your code that runs under the security context of the
authenticating user here.
const string name = "test.sp";
const string oldPassword = "oldpassword";
const string newPassword = "newpassword";

string username = string.Format(@"{0}\{1}",
Environment.MachineName, name);

var entry = new DirectoryEntry(string.Format("WinNT://{0}/{1},
user", Environment.MachineName, name),
username, oldPassword,
AuthenticationTypes.Secure);

entry.Invoke("ChangePassword", new object[] { oldPassword,
newPassword });

impersonationContext.Undo();
}

I want to run this code in a SharePoint webpart, so logged in users to
Sharepoint can change their own password essentially. Also I want to be able
to do the same thing with Active Directories.

Can you tell me why this doesn't work and what my options are?
Many thanks
Andrew


.