c#實現WINDOWS登入

來源:互聯網
上載者:User

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Runtime.InteropServices;
using System.Security.Principal;

namespace yutest
{
    public partial class _Default : System.Web.UI.Page
    {

        [DllImport("advapi32.dll", CharSet = CharSet.Auto)]
        public static extern bool LogonUser(string lpszUsername,string lpszDomain,string lpszPassword,int dwLogonType,int dwLogonProvider,out int phToken);

 

        protected void Page_Load(object sender, EventArgs e)
        {
            string aaa = System.Threading.Thread.CurrentPrincipal.Identity.Name;
            //string bbb = System.Threading.Thread.CurrentPrincipal.Identity.n;

            //System.Environment.UserDomainName  
            //System.Environment.UserName
           

        }

        protected void Button1_Click(object sender, System.EventArgs e)
        {
            //驗證使用者的輸入是否為空白
            if (tDomain.Text.Trim().Length > 0 && tUserName.Text.Trim().Length > 0&& tPassword.Text.Trim().Length > 0)
            {   //調用函數Login(string UserName, string Password, string Domain)
                //實現Windows登入
                if (Login(tUserName.Text.Trim(), tPassword.Text.Trim(),tDomain.Text.Trim()) == true)
                {   //顯示登入成功資訊
                    LoginMsg.Text = "登入成功!!!";
                    LoginMsg.Visible = true;
                    return;
                }
                else
                {   //顯示登入失敗資訊
                    LoginMsg.Text = "登入失敗,請重新輸入使用者名稱稱、密碼及其系統網域名稱!!!";
                    LoginMsg.Visible = true;
                }
            }

        }

        private bool Login(string UserName, string Password, string Domain)
        {       //擷取使用者名稱稱和系統網域名稱
            string text1 = Domain.Trim();
            string text2 = UserName.Trim();
            text2 = text2.Replace("/", @"\");      //處理符號“/”
            int num1 = text2.IndexOf('\\');        //擷取符號“\”的索引
            if (num1 != -1)
            {   //格式化使用者名稱稱和系統網域名稱
                text1 = text2.Substring(0, num1);
                text2 = text2.Substring(num1 + 1);
            }
            else
            {   //格式化使用者名稱稱和系統網域名稱
                num1 = text2.IndexOf('@');
                if (num1 != -1)
                {
                    text1 = text2.Substring(num1 + 1);
                    text2 = text2.Substring(0, num1);
                }
            }
            //調用函數AuthenticateUser()實現使用者Windows登入
            return AuthenticateUser(text2, Password.Trim(), text1);
        }
        private bool AuthenticateUser(string UserName, string Password,string Domain)
        {              //設定使用者登入成功的標誌
            bool flag1 = false;
            try
            {
                int num1; IntPtr ptr1;
                //調用Windows登入的API
                if (!LogonUser(UserName, Domain, Password, 2, 0, out num1))
                {   //返回登入結果
                    return flag1;
                }
                //調用.NET中的Windows登入
                ptr1 = new IntPtr(num1);
                WindowsIdentity identity1 = new WindowsIdentity(ptr1);
                WindowsPrincipal principal1 = new WindowsPrincipal(identity1);
                HttpContext.Current.User = principal1;
                //設定系統Cookie和重新導向頁面
                FormsAuthentication.SetAuthCookie(principal1.Identity.Name, false);
                FormsAuthentication.RedirectFromLoginPage(UserName, false);
                flag1 = true;
            }
            catch (Exception) { }
            return flag1;
        }

 

    }
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.