欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

c# 修改windows中賬戶的用戶名和密碼

 更新時(shí)間:2020年11月24日 09:22:43   作者:沙奇碼丶  
這篇文章主要介紹了c# 改變windows中賬戶的用戶名和密碼,幫助大家更好的理解和學(xué)習(xí)C#,感興趣的朋友可以了解下

在 C# 中,我們可以使用 WMI 類中的 Win32_Service 或者 Win32 API 中的函數(shù) ChangeServiceConfig 來(lái)修改本地或遠(yuǎn)程計(jì)算機(jī) Windows 服務(wù)登錄身份 (賬戶) 的用戶名和密碼。

1、使用 Win32 API 修改服務(wù)登錄身份信息:

使用 Win32 API 中的函數(shù) ChangeServiceConfig 更改的是服務(wù)控制管理器數(shù)據(jù)庫(kù)中指定服務(wù)的配置信息。

private const int SC_MANAGER_ALL_ACCESS = 0x000F003F;
private const uint SERVICE_NO_CHANGE = 0xffffffff; //這個(gè)值可以在 winsvc.h 中找到
private const uint SERVICE_QUERY_CONFIG = 0x00000001;
private const uint SERVICE_CHANGE_CONFIG = 0x00000002;
 
[DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]
public static extern Boolean ChangeServiceConfig(IntPtr hService, UInt32 nServiceType, 
  UInt32 nStartType,UInt32 nErrorControl,String lpBinaryPathName,String lpLoadOrderGroup,
  IntPtr lpdwTagId, [In] char[] lpDependencies, String lpServiceStartName, 
  String lpPassword, String lpDisplayName);
 
[DllImport("advapi32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern IntPtr OpenService(IntPtr hSCManager, string lpServiceName, uint dwDesiredAccess);
 
[DllImport("advapi32.dll", EntryPoint = "OpenSCManagerW", ExactSpelling = true, 
  CharSet = CharSet.Unicode, SetLastError = true)]
public static extern IntPtr OpenSCManager(string machineName, string databaseName, uint dwAccess);
 
public static bool ChangeServiceAccountInfo(string serviceName, string username,string password)
{
  try
  {
    IntPtr scm_Handle = OpenSCManager(null, null, SC_MANAGER_ALL_ACCESS);
    if (scm_Handle == IntPtr.Zero)
     throw new System.Runtime.InteropServices.ExternalException("打開服務(wù)管理器錯(cuò)誤");
 
    IntPtr service_Handle = OpenService(scm_Handle, serviceName,SERVICE_QUERY_CONFIG | SERVICE_CHANGE_CONFIG);
    if (service_Handle == IntPtr.Zero)
     throw new System.Runtime.InteropServices.ExternalException("打開服務(wù)錯(cuò)誤");
    //修改服務(wù)的賬戶用戶名和密碼
    if (!ChangeServiceConfig(service_Handle, SERVICE_NO_CHANGE, SERVICE_NO_CHANGE, 
       SERVICE_NO_CHANGE, null, null, IntPtr.Zero, null, username, password, null))
    {
      int nError = Marshal.GetLastWin32Error();
      Win32Exception win32Exception = new Win32Exception(nError);
      throw new System.Runtime.InteropServices.ExternalException("無(wú)法修改服務(wù)登錄身份的用戶名和密碼:" + win32Exception.Message);
    }
    Console.WriteLine("服務(wù)登錄身份信息修改成功!");
    return true;
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
    return false;
  }
}

2、使用 C# 中 WMI 修改服務(wù)登錄身份信息:

使用 WMI 服務(wù),我們需要添加 System.Management 的引用。

注意:如果您的遠(yuǎn)程計(jì)算機(jī)連接的是 Active Directory 域,那么使用完全限定的用戶名(例如 TestDomainMorgan)而不是簡(jiǎn)單的用戶名(Morgan)。

using System.Management;

public static void ChangeServiceAccountInfobyWMI(string serviceName, string username, string password)
{
  string mgmntPath = string.Format("Win32_Service.Name='{0}'", serviceName);
  using (ManagementObject service = new ManagementObject(new ManagementPath(mgmntPath)))
  {
    object[] accountParams = new object[11];
    accountParams[6] = username;
    accountParams[7] = password;
    uint returnCode = (uint)service.InvokeMethod("Change", accountParams);
    if (returnCode == 0)
    {
       Console.WriteLine("服務(wù)登錄身份信息修改成功!");
    }
    else
    {
       Console.WriteLine("服務(wù)登錄身份信息修改失敗");
       Console.WriteLine("錯(cuò)誤代碼:" + returnCode);
       // 此微軟官方支持鏈接,可以查看相應(yīng)的返回代碼的消息:
       // https://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx
    }
  }
}

3、使用 C#中的 WMI 修改遠(yuǎn)程計(jì)算機(jī)服務(wù)的登錄身份信息:

使用 WMI 服務(wù),我們需要添加 System.Management 的引用,并且在修改遠(yuǎn)程計(jì)算機(jī)中的服務(wù)信息時(shí),請(qǐng)使用管理員憑據(jù)。

注意:如果您的遠(yuǎn)程計(jì)算機(jī)連接的是 Active Directory 域,那么使用完全限定的用戶名(例如 TestDomainMorgan)而不是簡(jiǎn)單的用戶名(Morgan)。

using System.Management;
static void ChangeRemoteServiceAccountInfo(string remoteComputer, string serviceName, string username, string password)
{
  try
  {
    ConnectionOptions connectionOptions = new ConnectionOptions();
    // 如需要,請(qǐng)使用證書
    //connectionOptions.Username = "Administrator";
    //connectionOptions.Password = "AdminPassword";
    //connectionOptions.Impersonation = ImpersonationLevel.Impersonate;
    ManagementScope scope = new ManagementScope("\" + remoteComputer + "rootCIMV2", connectionOptions);
    scope.Connect();
    string mgmntPath = string.Format("Win32_Service.Name='{0}'", serviceName);
    using (ManagementObject service = new ManagementObject(scope, new ManagementPath(mgmntPath),new ObjectGetOptions()))
    {
      object[] accountParams = new object[11];
      accountParams[6] = username;
      accountParams[7] = password;
      uint returnCode = (uint)service.InvokeMethod("Change", accountParams);
      if (returnCode == 0)
      {
        Console.WriteLine("服務(wù)登錄身份信息修改成功!");
      }
      else
      {
        Console.WriteLine("服務(wù)登錄身份信息修改失敗");
        Console.WriteLine("錯(cuò)誤代碼:" + returnCode);
        // 此微軟官方支持鏈接,可以查看相應(yīng)的返回代碼信息:
        // https://msdn.microsoft.com/en-us/library/aa393660(v=vs.85).aspx
      }
    }
  }
  catch (Exception ex)
  {
    Console.WriteLine(ex.ToString());
  }
}

以上就是c# 改變windows中賬戶的用戶名和密碼的詳細(xì)內(nèi)容,更多關(guān)于c# 更改用戶名和密碼的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

最新評(píng)論