本文實(shí)例講述了C#實(shí)現(xiàn)的WINDOWS登錄功能。分享給大家供大家參考,具體如下:
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)    {      //驗(yàn)證用戶的輸入是否為空      if (tDomain.Text.Trim().Length > 0 && tUserName.Text.Trim().Length > 0&& tPassword.Text.Trim().Length > 0)      {  //調(diào)用函數(shù)Login(string UserName, string Password, string Domain)        //實(shí)現(xiàn)Windows登錄        if (Login(tUserName.Text.Trim(), tPassword.Text.Trim(),tDomain.Text.Trim()) == true)        {  //顯示登錄成功信息          LoginMsg.Text = "登錄成功?。。?;          LoginMsg.Visible = true;          return;        }        else        {  //顯示登錄失敗信息          LoginMsg.Text = "登錄失敗,請(qǐng)重新輸入用戶名稱、密碼及其系統(tǒng)域名?。。?;          LoginMsg.Visible = true;        }      }    }    private bool Login(string UserName, string Password, string Domain)    {    //獲取用戶名稱和系統(tǒng)域名      string text1 = Domain.Trim();      string text2 = UserName.Trim();      text2 = text2.Replace("/", @"/");   //處理符號(hào)“/”      int num1 = text2.IndexOf('//');    //獲取符號(hào)“/”的索引      if (num1 != -1)      {  //格式化用戶名稱和系統(tǒng)域名        text1 = text2.Substring(0, num1);        text2 = text2.Substring(num1 + 1);      }      else      {  //格式化用戶名稱和系統(tǒng)域名        num1 = text2.IndexOf('@');        if (num1 != -1)        {          text1 = text2.Substring(num1 + 1);          text2 = text2.Substring(0, num1);        }      }      //調(diào)用函數(shù)AuthenticateUser()實(shí)現(xiàn)用戶Windows登錄      return AuthenticateUser(text2, Password.Trim(), text1);    }    private bool AuthenticateUser(string UserName, string Password,string Domain)    {       //設(shè)置用戶登錄成功的標(biāo)志      bool flag1 = false;      try      {        int num1; IntPtr ptr1;        //調(diào)用Windows登錄的API        if (!LogonUser(UserName, Domain, Password, 2, 0, out num1))        {  //返回登錄結(jié)果          return flag1;        }        //調(diào)用.NET中的Windows登錄        ptr1 = new IntPtr(num1);        WindowsIdentity identity1 = new WindowsIdentity(ptr1);        WindowsPrincipal principal1 = new WindowsPrincipal(identity1);        HttpContext.Current.User = principal1;        //設(shè)置系統(tǒng)Cookie和重定向頁面        FormsAuthentication.SetAuthCookie(principal1.Identity.Name, false);        FormsAuthentication.RedirectFromLoginPage(UserName, false);        flag1 = true;      }      catch (Exception) { }      return flag1;    }  }}更多關(guān)于C#相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《C#常見控件用法教程》、《WinForm控件用法總結(jié)》、《C#程序設(shè)計(jì)之線程使用技巧總結(jié)》、《C#數(shù)據(jù)結(jié)構(gòu)與算法教程》及《C#面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》
希望本文所述對(duì)大家C#程序設(shè)計(jì)有所幫助。
新聞熱點(diǎn)
疑難解答
圖片精選