Comment obtenir le nom d’utilisateur et le mot de passe actuels à partir du système d’exploitation Windows.

Histoire: J’ai un CD de carte mère fourni par la carte mère d’origine d’Intel. Lorsque j’installe les pilotes, il demande le nom d’utilisateur et le mot de passe du compte administrateur.

Après l’installation de chaque pilote, le système sera redémarré et ne demandera pas le nom d’utilisateur et le mot de passe.

Mon idée est que Windows devrait avoir un moyen de valider et d’entrer le nom d’utilisateur et le mot de passe

Pourriez-vous s’il vous plaît laissez-moi savoir comment faire cela en C #, merci.

Je pense que cet article pourrait vous aider.

Faites-moi savoir si vous rencontrez un problème lors de la compréhension du code.

Edit 1: Je suis confus avec votre question.

mon idée: selon ce qui précède, Windows devrait fournir un moyen de valider et d’entrer un nom d’utilisateur et un mot de passe.

Voulez-vous valider un nom d’utilisateur et un mot de passe?


Ahh, désolé pour le retard. Voici le code c # converti

Ajoutez les espaces de noms suivants:

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

Et puis voici le code principal:

 namespace WindowsAccount { public partial class Form1 : Form { [DllImport("advapi32.dll", SetLastError = true)] public static extern bool LogonUser(ssortingng lpszUsername, ssortingng lpszDomain, ssortingng 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 Ssortingng lpBuffer, int nSize, ref IntPtr Arguments); [DllImport("kernel32.dll", SetLastError = true)] [return: MarshalAs(UnmanagedType.Bool)] static extern bool CloseHandle(IntPtr hObject); public static ssortingng GetErrorMessage(int errorCode) { int FORMAT_MESSAGE_ALLOCATE_BUFFER = 0x100; int FORMAT_MESSAGE_IGNORE_INSERTS = 0x200; int FORMAT_MESSAGE_FROM_SYSTEM = 0x1000; int msgSize = 255; ssortingng 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.ToSsortingng() + ". "); } return lpMsgBuf; } public Form1() { InitializeComponent(); } private void btnLogin_Click(object sender, EventArgs e) { IntPtr tokenHandle = new IntPtr(0); try { ssortingng UserName = null; ssortingng MachineName = null; ssortingng 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(); ssortingng 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); } } } } 

Faites-moi savoir si vous rencontrez un problème.

Vous pouvez utiliser la fonctionnalité de connexion Auto Admin, contrôlée par quelques clés de registre. Voir http://support.microsoft.com/kb/315231

Vous pouvez connaître votre nom d’utilisateur en accédant à une invite de commande et en tapant:

 C:\ >set USERNAME 

et il imprimera quelque chose comme

 USERNAME=Administrator 

c’est votre nom d’utilisateur connecté.

Je suis à peu près sûr que votre mot de passe est vide / vide, sinon vous y serez invité.