Windows登錄功能使用C#實(shí)現(xiàn)的案例?這個(gè)問題可能是我們?nèi)粘W(xué)習(xí)或工作經(jīng)常見到的。希望通過這個(gè)問題能讓你收獲頗深。下面是小編給大家?guī)?lái)的參考內(nèi)容,讓我們一起來(lái)看看吧!
創(chuàng)新互聯(lián)建站于2013年創(chuàng)立,先為興國(guó)等服務(wù)建站,興國(guó)等地企業(yè),進(jìn)行企業(yè)商務(wù)咨詢服務(wù)。為興國(guó)企業(yè)網(wǎng)站制作PC+手機(jī)+微官網(wǎng)三網(wǎng)同步一站式服務(wù)解決您的所有建站問題。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和重定向頁(yè)面 FormsAuthentication.SetAuthCookie(principal1.Identity.Name, false); FormsAuthentication.RedirectFromLoginPage(UserName, false); flag1 = true; } catch (Exception) { } return flag1; } } }
感謝各位的閱讀!看完上述內(nèi)容,你們對(duì)Windows登錄功能使用C#實(shí)現(xiàn)的案例大概了解了嗎?希望文章內(nèi)容對(duì)大家有所幫助。如果想了解更多相關(guān)文章內(nèi)容,歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道。