问题描述
故事:我有一张英特尔原装主板提供的主板光盘.当我安装驱动程序时,它要求输入管理员帐户的用户名和密码.
Story: I have a motherboard CD provided by Intel's original motherboard. When I install drivers it asks for username and password of the administrator account.
每次驱动安装后,系统都会重新启动,不会要求输入用户名和密码.
After every driver installs, the system will be restart and doesn't ask for the username and password.
我的想法是 Windows 应该有一种方法来验证和输入用户名和密码
My idea is that Windows should have a way to validate and enter username and password
能否请您告诉我如何在 C# 中执行此操作,谢谢.
Could you please let me know how to do this in C#, thanks.
推荐答案
我认为这篇文章可能会帮助你.
I think this article might help you.
如果您在理解代码时遇到任何问题,请告诉我.
Let me know if you face any problem while understanding the code.
编辑 1 :我对你的问题感到困惑.
Edit 1 : I am confused with your question.
我的想法:根据上面的 Windows 应该提供一些验证和输入用户名和密码
是否要验证输入的用户名和密码?
Do you want to validate a entered username and password?
啊,抱歉耽搁了.这是转换后的 c# 代码
Ahh, sorry for the delay. Here's the converted c# code
添加以下命名空间:
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);
}
}
}
}
如果您遇到任何问题,请告诉我.
Let me know if you face any problem.
这篇关于如何从 Windows 操作系统获取当前用户名和密码.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!