System.DirectoryServices Invoke error

sunny Liang 1 Reputation point
2022-07-13T03:05:16.797+00:00

异常:RPC 服务器不可用。 (异常来自 HRESULT:0x800706BA)
在 System.DirectoryServices.DirectoryEntry.Invoke(String methodName, Object[] args)

my code:
string pwd = "abcd.1234";
userDirectoryEntry.Invoke("SetPassword", new object[] { "" + pwd + "" });//设置初始化密码

                userDirectoryEntry.Properties["pwdLastSet"].Value = 0;  
                userDirectoryEntry.Properties["userAccountControl"].Value = 66048;  
                userDirectoryEntry.CommitChanges();
Developer technologies ASP.NET ASP.NET Core
Windows for business Windows Client for IT Pros Directory services Active Directory
Developer technologies C#
{count} votes

1 answer

Sort by: Most helpful
  1. Limitless Technology 39,916 Reputation points
    2022-07-18T07:41:44.08+00:00

    Hello SunnyLiang

    Check TCP/UDP 445 port is opened on your firewall. To connect to an AD server from outside a domain, you need the following ports to be opened : . TCP/UDP 389 (LDAP) . TCP 3268 (GC) . TCP/UDP 445 (SMB over IP)

    DirectoryEntry.Invoke() requires AuthenticationType.Secure. What this means is that it needs to be able to authenticate the request via Kerberos or NTLM.

    It attempts to use LDAPS (TCP 636) first, then falls back to CiFS (TCP445) if/when it times out or fails because of a missing or invalid certificate. If neither of these ports are open, it will fail with an "RPC Server unavailable" exception.

    Additionally: Are you (or could you upgrade to) .NET 3.5? The AD integration for users, groups, computers has been massively improved in .NET 3.5 - check out the MSDN article Managing Directory Security Principals ( https:// docs. microsoft. com/en-us/archive/msdn-magazine/2008/january/managing-directory-security-principals-in-the-net-framework-3-5 ) in the .NET Framework 3.5 for details.

    In your case, you could do something like:

    // establish context for local machine
    PrincipalContext ctx = new PrincipalContext(ContextType.Machine);

    // find the "Administrator" account
    UserPrincipal admin = UserPrincipal.FindByIdentity(ctx, "Administrator");

    // set the password to a new value
    admin.SetPassword("new-top-secret-password");
    admin.Save();

    ---------------------------------------------------------------------------------------------------------------------------------

    --If the reply is helpful, please Upvote and Accept as answer--

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.