📄 windowslogin.aspx.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Security.Principal;
using System.Web.Security;
using System.Runtime.InteropServices;
namespace Example_12_3
{
/// <summary>
/// Summary description for WindowsLogin.
/// </summary>
public class WindowsLogin : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox tUserName;
protected System.Web.UI.WebControls.TextBox tPassword;
protected System.Web.UI.WebControls.Button UserLoginBtn;
protected System.Web.UI.WebControls.TextBox tDomain;
protected System.Web.UI.WebControls.Label LoginMsg;
protected System.Web.UI.WebControls.Button CancelBtn;
private void Page_Load(object sender, System.EventArgs e)
{
// Put user code to initialize the page here
}
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);
}
}
return AuthenticateUser(text2, Password.Trim(), text1);
}
private bool AuthenticateUser(string UserName, string Password, string Domain)
{
bool flag1 = false;
try
{
int num1;
IntPtr ptr1;
if (!LogonUser(UserName, Domain, Password, 2, 0, out num1))
{
return flag1;
}
ptr1 = new IntPtr(num1);
WindowsIdentity identity1 = new WindowsIdentity(ptr1);
WindowsPrincipal principal1 = new WindowsPrincipal(identity1);
HttpContext.Current.User = principal1;
FormsAuthentication.SetAuthCookie(principal1.Identity.Name, false);
FormsAuthentication.RedirectFromLoginPage(UserName, false);
flag1 = true;
}
catch (Exception)
{
}
return flag1;
}
[DllImport("advapi32.dll")]
public static extern bool LogonUser(
string lpszUsername,
string lpszDomain,
string lpszPassword,
int dwLogonType,
int dwLogonProvider,
out int phToken
);
#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.UserLoginBtn.Click += new System.EventHandler(this.UserLoginBtn_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void UserLoginBtn_Click(object sender, System.EventArgs e)
{
if(tDomain.Text.Trim().Length > 0 && tUserName.Text.Trim().Length > 0 && tPassword.Text.Trim().Length > 0)
{
if(Login(tUserName.Text.Trim(),tPassword.Text.Trim(),tDomain.Text.Trim()) == true)
{
LoginMsg.Text = "登录成功!!!";
LoginMsg.Visible = true;
return;
}
else
{
LoginMsg.Text = "登录失败,请重新输入用户名称、密码及其系统域名!!!";
LoginMsg.Visible = true;
}
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -