2010-09-15 185 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

相关问题