01.
<%@ Assembly Name=
"System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
%>
02.
<%@ Import Namespace=
"System.DirectoryServices"
%>
03.
<%@ Import Namespace=
"System.Security.Cryptography"
%>
04.
<%@ Import Namespace=
"System.Text"
%>
05.
<%@ Page Language=
"C#"
%>
06.
<script runat=
"server"
>
07.
08.
string
initLDAPPath =
"dc=tc, dc=thaicreate, dc=com"
;
09.
string
initLDAPServer =
"192.168.0.5"
;
10.
string
initShortDomainName =
"tc"
;
11.
string
strErrMsg;
12.
13.
14.
public
string
MD5(
string
strString) {
15.
ASCIIEncoding ASCIIenc =
new
ASCIIEncoding();
16.
string
strReturn;
17.
byte
[] ByteSourceText = ASCIIenc.GetBytes(strString);
18.
MD5CryptoServiceProvider Md5Hash =
new
MD5CryptoServiceProvider();
19.
byte
[] ByteHash = Md5Hash.ComputeHash(ByteSourceText);
20.
strReturn =
""
;
21.
foreach
(
byte
b
in
ByteHash) {
22.
strReturn = (strReturn + b.ToString(
"x2"
));
23.
}
24.
return
strReturn;
25.
}
26.
27.
void
Page_Load(
object
sender, EventArgs e) {
28.
}
29.
30.
protected
void
btnLogin_Click(
object
sender, System.EventArgs e) {
31.
string
DomainAndUsername =
""
;
32.
string
strCommu;
33.
bool
flgLogin =
false
;
35.
+ (initLDAPServer + (
"/"
+ initLDAPPath)));
36.
DomainAndUsername = (initShortDomainName + (
"\\"
+ txtUser.Text));
37.
DirectoryEntry entry =
new
DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text);
38.
object
obj;
39.
try
{
40.
obj = entry.NativeObject;
41.
DirectorySearcher search =
new
DirectorySearcher(entry);
42.
SearchResult result;
43.
search.Filter = (
"(SAMAccountName="
44.
+ (txtUser.Text +
")"
));
45.
search.PropertiesToLoad.Add(
"cn"
);
46.
result = search.FindOne();
47.
if
((result ==
null
)) {
48.
flgLogin =
false
;
49.
strErrMsg =
"Please check user/password"
;
50.
}
51.
else
{
52.
flgLogin =
true
;
53.
}
54.
}
55.
catch
(Exception ex) {
56.
flgLogin =
false
;
57.
strErrMsg =
"Please check user/password"
;
58.
}
59.
if
((flgLogin ==
true
)) {
60.
this
.lbDisplay.Text = (
"Welcome "
+ txtUser.Text);
61.
}
62.
else
{
63.
this
.lbDisplay.Text = strErrMsg;
64.
}
65.
}
66.
</script>
67.
68.
<html>
69.
<head>
70.
<title>ThaiCreate.Com</title>
71.
</head>
72.
<body>
73.
<form id=
"form1"
runat=
"server"
>
74.
<asp:TextBox ID=
"txtUser"
runat=
"server"
Width=
"147px"
></asp:TextBox>
75.
<asp:TextBox ID=
"txtPwd"
runat=
"server"
Width=
"147px"
TextMode=
"Password"
></asp:TextBox>
76.
<asp:Button ID=
"btnLogin"
runat=
"server"
Text=
"Login"
OnClick=
"btnLogin_Click"
/>
77.
<br />
78.
<asp:Label ID=
"lbDisplay"
runat=
"server"
></asp:Label>
79.
</form>
80.
</body>
81.
</HTML>