asp.net|C#域用户验证

  [DllImport("advapi32.dll")]
        private static extern bool LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        public bool ValidateUserAccount(string AstrDomainName, string AstrDomainAccount, string AstrDomainPassword)
        {

            const int LOGON32_LOGON_INTERACTIVE = 2; //通过网络验证账户合法性

            const int LOGON32_PROVIDER_DEFAULT = 0; //使用默认的Windows 2000/NT NTLM验证方

            IntPtr tokenHandle = new IntPtr(0);
            tokenHandle = IntPtr.Zero;

            string domainName = AstrDomainName; //域 如:officedomain
            string domainAccount = AstrDomainAccount; //域帐号 如:administrator
            string domainPassword = AstrDomainPassword;//密码
            bool checkok = LogonUser(domainAccount, domainName, domainPassword, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref tokenHandle);

            return checkok;
        }

 protected void btnSave_Click(object sender, EventArgs e)
        {
            TransFollowing.BLL.Account accountMananger = new TransFollowing.BLL.Account();
            string userName = txtUserName.Text.Trim();
            string passWord = txtPassWord.Text.Trim();

            if (ValidateUserAccount("域用户", txtUserName.Text.Trim(), txtPassWord.Text.Trim()))
            {
            //完全采用域用户来管理
            FormsAuthenticationTicket ticket = new FormsAuthenticationTicket(
            1,                              // version
            userName,          // user name
            DateTime.Now,                   // issue time
            DateTime.Now.AddHours(1),       // expires every hour
            false,                          // don't persist cookie
            ""                         // roles
            );
            FormsAuthentication.SetAuthCookie(userName, false);
            Context.Response.Redirect("~/TransManage/TranManage.aspx");
            }
            //else
            //{
            //    lbError.Visible = true;
            //}

           
        }

原文地址:https://www.cnblogs.com/Anders888/p/2153404.html