Answered Access denied (Exception from HRESULT: 0x80070005 (EACCESSDENIED)

  • Friday, May 04, 2012 11:35 PM
     
      Has Code

    Hi,

    I am working in a project with SP2010. I have to manage users in the Active Directory of Windows Server 2008 R2.

    When I try to change the password of the user, I have the error shown below in the picture.

    I tested this function in a console application and it's work nice. Also, I tested it in a web application and it's work. But when I try to do this operation with a sharpoint porject I have this error.

    PS: I grant the user all the provileges in the active directory.

    Here is the code of the function:

    public bool ChangePasswordADUser(string logonName, string newPassword)
            {
                try
                {
                    DirectoryEntry result = ReadADUser(logonName);
                    if (result != null)
                    {
                        object o = result.Invoke("SetPassword", new object[] { newPassword });
                        result.CommitChanges();
                        return true;
                    }
                    else
                    {
                        return false;
                    }
    
                }
                catch (System.Reflection.TargetInvocationException ex)
                {
                    return false;
                }
            }

    And there is the exception:

    exception

    Thank you in advance if you can help me.

All Replies

  • Monday, May 07, 2012 7:37 AM
     
     Answered Has Code

    Hi  grandi_hamza,

    I’ve tested your code in my test environment, but it works fine. In my personal opinion, you should check the privileges again, you should verify that the current identity is the user that you want or you can also use SPSecurity.RunWithElevatedPrivileges to run with Application Pool Identity.

    You can use following code to inspect the current identity:

    var identity = WindowsIdentity.GetCurrent();

    Thanks,


    Lambda Zhao

    TechNet Community Support

    • Marked As Answer by grandi_hamza Wednesday, May 09, 2012 8:44 AM
    •  
  • Monday, May 07, 2012 7:38 AM
     
     

    Please ask your question in the SharePoint 2010 forum and not in this pre-SharePoint 2010 forum.<o:p></o:p>

    SharePoint 2010 - General Questions and Answers
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010general/threads

    SharePoint 2010 - Setup, Upgrade,Administration and Operation
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010setup/threads

    SharePoint 2010 - Using SharePoint Designer, Infopath, and other customization
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010customization/threads

    SharePoint 2010 - Using Visual Studio with SharePoint and other programming
    http://social.technet.microsoft.com/Forums/en-US/sharepoint2010programming/threads



    Dirk Van den Berghe


  • Monday, May 07, 2012 7:40 AM
     
     
    By the way, when executing your code as a SharePoint solution, remember that the actual account doing the operation in Active Directory is the application pool account assigned to the web application in the IIS application pool assigned to your SharePoint web application. Are you sure you gave that account permissions in Active Directory for doing the modifications?

    Dirk Van den Berghe

  • Wednesday, May 09, 2012 8:45 AM
     
     Answered Has Code
            public bool ChangePasswordADUser(string logonName, string newPassword)
            {
    
                try
                {
                    DirectoryEntry result = ReadADUser(logonName);
                    if (result != null)
                    {
                        SPSecurity.RunWithElevatedPrivileges(delegate(){
                            System.Security.Principal.WindowsIdentity wi = System.Security.Principal.WindowsIdentity.GetCurrent();
    
                            object o = result.Invoke("SetPassword", new object[] { newPassword });
                            result.CommitChanges();
                        });
                        return true;
                    }
                    else
                    {
                        return false;
                    }
    
                }


    HG

    • Marked As Answer by grandi_hamza Wednesday, May 09, 2012 8:45 AM
    •