2010-09-15 75 views
3

故事:我有一張由英特爾原裝主板提供的主板CD。當我安裝驅動程序時,它會要求輸入管理員帳戶的用戶名和密碼。如何從Windows操作系統獲取當前用戶名和密碼。

每次安裝驅動程序後,系統將重新啓動並且不會詢問用戶名和密碼。

我的想法是,Windows應該有一個方法來驗證,並輸入用戶名和密碼

能否請你讓我知道如何在C#中做到這一點,謝謝。

回答

4

我認爲這article可能會幫助你。

讓我知道如果您在理解代碼時遇到任何問題。

編輯1:我很困惑你的問題。

我的想法:根據以上的Windows 768,16 是提供一些VAY驗證和 輸入用戶名和密碼

你想驗證輸入的用戶名和密碼?


啊,對不起,延誤了。這裏的轉換C#代碼

添加下面的命名空間:

using System.Security.Principal; 
using System.Security.Permissions; 
using System.Runtime.InteropServices; 

然後這裏有雲的主要代碼:

namespace WindowsAccount 
{ 
    public partial class Form1 : Form 
    { 

     [DllImport("advapi32.dll", SetLastError = true)] 
     public static extern bool LogonUser(string lpszUsername, 
      string lpszDomain, 
      string lpszPassword, 
      int dwLogonType, 
      int dwLogonProvider, 
      out IntPtr phToken 
      ); 

     [DllImport("kernel32.dll")] 
     public static extern int FormatMessage(int dwFlags, ref IntPtr lpSource, int dwMessageId, int dwLanguageId, ref String lpBuffer, int nSize, ref IntPtr Arguments); 

     [DllImport("kernel32.dll", SetLastError = true)] 
     [return: MarshalAs(UnmanagedType.Bool)] 
     static extern bool CloseHandle(IntPtr hObject); 


     public static string GetErrorMessage(int errorCode) 
     { 
      int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100; 
      int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200; 
      int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000; 

      int msgSize = 255; 
      string lpMsgBuf = null; 
      int dwFlags = FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | FORMAT_MESSAGE_IGNORE_INSERTS; 

      IntPtr lpSource = IntPtr.Zero; 
      IntPtr lpArguments = IntPtr.Zero; 
      int returnVal = FormatMessage(dwFlags, ref lpSource, errorCode, 0, ref lpMsgBuf, msgSize, ref lpArguments); 

      if (returnVal == 0) 
      { 
       throw new Exception("Failed to format message for error code " + errorCode.ToString() + ". "); 
      } 
      return lpMsgBuf; 

     } 


     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void btnLogin_Click(object sender, EventArgs e) 
     { 
      IntPtr tokenHandle = new IntPtr(0); 

      try 
      { 
       string UserName = null; 
       string MachineName = null; 
       string Pwd = null; 

       //The MachineName property gets the name of your computer. 
       MachineName = System.Environment.MachineName; 
       UserName = txtUser.Text; 
       Pwd = txtPass.Text; 

       const int LOGON32_PROVIDER_DEFAULT = 0; 
       const int LOGON32_LOGON_INTERACTIVE = 2; 
       tokenHandle = IntPtr.Zero; 

       //Call the LogonUser function to obtain a handle to an access token. 
       bool returnValue = LogonUser(UserName, MachineName, Pwd, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, out tokenHandle); 

       if (returnValue == false) 
       { 
        //This function returns the error code that the last unmanaged function returned. 
        int ret = Marshal.GetLastWin32Error(); 
        string errmsg = GetErrorMessage(ret); 
        MessageBox.Show(errmsg); 
       } 
       else 
       { 
        //Create the WindowsIdentity object for the Windows user account that is 
        //represented by the tokenHandle token. 

        WindowsIdentity newId = new WindowsIdentity(tokenHandle); 
        WindowsPrincipal userperm = new WindowsPrincipal(newId); 

        //Verify whether the Windows user has administrative credentials. 
        if (userperm.IsInRole(WindowsBuiltInRole.Administrator)) 
        { 
         MessageBox.Show("Access Granted. User is admin"); 
        } 
        else 
        { 
         MessageBox.Show("Access Granted. User is not admin"); 
        } 
       } 

       CloseHandle(tokenHandle); 
      } 
      catch (Exception ex) 
      { 
       MessageBox.Show("Exception occurred. " + ex.Message); 
      } 

     } 
    } 
} 

讓我知道,如果你面對任何問題。

+0

是的,先生,這是我的要求。但我無法理解VB.NET代碼(因爲我沒有很好的VB.NET知識)。請發送C#.net示例。 aeticle。 – Kumara 2010-09-16 11:46:38

+0

我沒有在我的機器上安裝.Net,我會幫助你,一旦我到達家中,或者你可以從vb.net轉換你的代碼到C#從http://www.developerfusion.com/tools/convert/vb -to-CSHARP /。 – Searock 2010-09-16 12:41:08

+0

非常感謝。 – Kumara 2010-09-19 01:36:22

0

您可以通過命令提示符,然後輸入找出你的用戶名:

C:\ >set USERNAME 

,它會打印出類似這樣

USERNAME=Administrator 

這是你的登錄用戶名。

我很確定你的密碼爲空/空白,否則會提示你。

+0

ECHO%USERNAME%在這種情況下會更合適。恕我直言 – abhilash 2010-09-15 05:36:19

相關問題