C# 使用API檢查域用戶名和密碼是否正確

添加引用:api

using System.Runtime.InteropServices;dom

    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應用程序中調用方式:ide

bool a = Comm.VerifyUserByDomain.verify(Environment.UserName, textBox2.Text.Trim(), Environment.UserDomainName);spa

相關文章
相關標籤/搜索