Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<head runat="server">
<title>登入頁面</title>
</head>
<body>
<table align="center"><tr><td>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default.aspx"
OnAuthenticate="Login1_Authenticate" BackColor="#EFF3FB" BorderColor="#B5C7DE"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="12px" ForeColor="#333333">
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" />
</asp:Login>
</div>
</form></td></tr>
</table>
</body>
</html>
Login.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.DirectoryServices;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Lin
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string ValidateUser(string ComputerName, string UserName, string Password)
{
string strPath;
if (ComputerName.IndexOf(',') != -1)
strPath = string.Format(@"ldap://{0/}", ComputerName);
else
strPath = string.Format(@"WinNT://{0}/{1}, user", ComputerName, UserName);
DirectoryEntry entry = new DirectoryEntry(strPath, UserName, Password);
try
{
string objectSid =
(new SecurityIdentifier((byte[])entry.Properties["objectSid"].Value, 0).Value);
return objectSid;
}
catch// (DirectoryServicesCOMException)
{
return null;
}
finally
{
entry.Dispose();
}
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string strComputerName = "test.com.tw";
string strUserName = ((System.Web.UI.WebControls.Login)sender).UserName;
string strPassword = ((System.Web.UI.WebControls.Login)sender).Password;
string strValidateUser = ValidateUser(strComputerName, strUserName, strPassword);
if (strValidateUser != null)
{
e.Authenticated = true;
}
else
{
e.Authenticated = false;
}
}
}
<head runat="server">
<title>登入頁面</title>
</head>
<body>
<table align="center"><tr><td>
<form id="form1" runat="server">
<div>
<asp:Login ID="Login1" runat="server" DestinationPageUrl="~/Default.aspx"
OnAuthenticate="Login1_Authenticate" BackColor="#EFF3FB" BorderColor="#B5C7DE"
BorderPadding="4" BorderStyle="Solid" BorderWidth="1px" Font-Names="Verdana"
Font-Size="12px" ForeColor="#333333">
<TextBoxStyle Font-Size="0.8em" />
<LoginButtonStyle BackColor="White" BorderColor="#507CD1" BorderStyle="Solid"
BorderWidth="1px" Font-Names="Verdana" Font-Size="0.8em" ForeColor="#284E98" />
<InstructionTextStyle Font-Italic="True" ForeColor="Black" />
<TitleTextStyle BackColor="#507CD1" Font-Bold="True" Font-Size="0.9em"
ForeColor="White" />
</asp:Login>
</div>
</form></td></tr>
</table>
</body>
</html>
Login.aspx.cs
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.DirectoryServices;
using System.Linq;
using System.Security.Principal;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Lin
public partial class Login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
public static string ValidateUser(string ComputerName, string UserName, string Password)
{
string strPath;
if (ComputerName.IndexOf(',') != -1)
strPath = string.Format(@"ldap://{0/}", ComputerName);
else
strPath = string.Format(@"WinNT://{0}/{1}, user", ComputerName, UserName);
DirectoryEntry entry = new DirectoryEntry(strPath, UserName, Password);
try
{
string objectSid =
(new SecurityIdentifier((byte[])entry.Properties["objectSid"].Value, 0).Value);
return objectSid;
}
catch// (DirectoryServicesCOMException)
{
return null;
}
finally
{
entry.Dispose();
}
}
protected void Login1_Authenticate(object sender, AuthenticateEventArgs e)
{
string strComputerName = "test.com.tw";
string strUserName = ((System.Web.UI.WebControls.Login)sender).UserName;
string strPassword = ((System.Web.UI.WebControls.Login)sender).Password;
string strValidateUser = ValidateUser(strComputerName, strUserName, strPassword);
if (strValidateUser != null)
{
e.Authenticated = true;
}
else
{
e.Authenticated = false;
}
}
}
程式碼工作室
ERP/EIP/CMS/CHART/REPORT/SPC/EDA/雲端 系統開發整合
ASP/PHP/JSP/ASP.NET 網頁設計
技術指導顧問
信箱:paulwu0114@gmail.com
http://www.coding.com.tw
沒有留言:
張貼留言