您的位置:首页 > 编程语言 > C#

C# 使用API检查域用户名和密码是否正确

2017-02-14 12:17 531 查看
添加引用:

using System.Runtime.InteropServices;

public class VerifyUserByDomain
{
private static int LOGon32_LOGon_INTERACTIVE = 2;
private static int LOGon32_PROVIDER_DEFAULT = 0;
private static IntPtr tokenHandle = new IntPtr(0);

[DllImport("advapi32.dll")]
private static extern bool LogonUser(string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
ref IntPtr phToken);

public static bool verify(string userName, string pwd, string domain)
{
bool boolResult = false;
tokenHandle = IntPtr.Zero;
//使用域密码登录
boolResult = LogonUser(userName, domain, pwd, LOGon32_LOGon_INTERACTIVE, LOGon32_PROVIDER_DEFAULT, ref tokenHandle);
return boolResult;
}


在Windows应用程序中调用方式:

bool a = Comm.VerifyUserByDomain.verify(Environment.UserName, textBox2.Text.Trim(), Environment.UserDomainName);
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐