0

I created a webpart where I am trying to set the password (if forgotten). Below is the code I am using: I get an access denied even though I am impersonating with an account that is in the Administrators group.

SafeTokenHandle safeTokenHandle;
string userName, domainName;
// Get the user token for the specified user, domain, and password using the 
// unmanaged LogonUser method. 
// The local machine name can be used for the domain name to impersonate a user on this machine.


const int LOGON32_PROVIDER_DEFAULT = 0;
//This parameter causes LogonUser to create a primary token. 
const int LOGON32_LOGON_INTERACTIVE = 2;

// Call LogonUser to obtain a handle to an access token. 
bool returnValue = LogonUser(username, domain, password,
LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out safeTokenHandle);

if (false == returnValue)
{
    int ret = Marshal.GetLastWin32Error();
}
using (safeTokenHandle)
{
using (WindowsImpersonationContext impersonatedUser = WindowsIdentity.Impersonate(safeTokenHandle.DangerousGetHandle()))
{
string x = WindowsIdentity.GetCurrent().Name;
PrincipalContext pc = new PrincipalContext(ContextType.Domain);
UserPrincipal up = UserPrincipal.FindByIdentity(pc, username);
up.SetPassword(txtNewChangedPassword.Text);
}
0

You can't change user password in sharepoint.

If you are trying to change the password of an fba user you can use the ChangePassword method (http://msdn.microsoft.com/en-us/library/system.web.security.membershipuser.changepassword.aspx) of the MembershipUser class.

By other way if you want to change it in AD, you have to invoke the ChangePassword method of a DirectoryEntry object of User type: user.Invoke("ChangePassword", new object[] { oldPassword, newPassword }); (check this post: http://glorix.blogspot.com/2007/10/ad-change-password-webpart.html)

greetings :)

  • I'm trying to Set the password in AD. – Kyle Johnson Jun 25 '15 at 13:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.