获取当前计算机用户密码,c# – 如何从Windows操作系统获取当前用户名和密码.

我认为这

article可能会对你有所帮助.

如果您在理解代码时遇到任何问题,请告诉我.

编辑1:我对你的问题感到困惑.

my idea: According above Windows shoud

be provide some vay to validate and

enter username and password

您想验证输入的用户名和密码吗?

啊,抱歉延误了.这是转换后的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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值