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=
"VB"
%>
06.
<script runat=
"server"
>
07.
08.
Dim
initLDAPPath =
"dc=tc, dc=thaicreate, dc=com"
09.
Dim
initLDAPServer =
"192.168.0.5"
10.
Dim
initShortDomainName =
"tc"
11.
Dim
strErrMsg
12.
13.
Public
Function
MD5(
ByVal
strString
As
String
)
As
String
14.
Dim
ASCIIenc
As
New
ASCIIEncoding
15.
Dim
strReturn
As
String
16.
Dim
ByteSourceText()
As
Byte
= ASCIIenc.GetBytes(strString)
17.
Dim
Md5Hash
As
New
MD5CryptoServiceProvider
18.
Dim
ByteHash()
As
Byte
= Md5Hash.ComputeHash(ByteSourceText)
19.
20.
strReturn =
""
21.
22.
For
Each
b
As
Byte
In
ByteHash
23.
strReturn = strReturn & b.ToString(
"x2"
)
24.
Next
25.
26.
Return
strReturn
27.
28.
End
Function
29.
30.
Sub
Page_Load(sender
As
Object
, e
As
EventArgs)
31.
32.
33.
End
Sub
34.
35.
Protected
Sub
btnLogin_Click(
ByVal
sender
As
Object
,
ByVal
e
As
System.EventArgs)
36.
37.
Dim
DomainAndUsername
As
String
=
""
38.
Dim
strCommu
As
String
39.
Dim
flgLogin
As
Boolean
=
False
40.
strCommu =
"LDAP://"
& initLDAPServer &
"/"
& initLDAPPath
41.
DomainAndUsername = initShortDomainName &
"\"
& txtUser.Text
42.
43.
Dim
entry
As
New
DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text)
44.
Dim
obj
As
Object
45.
46.
Try
47.
48.
obj = entry.NativeObject
49.
Dim
search
As
New
DirectorySearcher(entry)
50.
Dim
result
As
SearchResult
51.
search.Filter =
"(SAMAccountName="
+ txtUser.Text +
")"
52.
search.PropertiesToLoad.Add(
"cn"
)
53.
result = search.FindOne()
54.
55.
If
result
Is
Nothing
Then
56.
flgLogin =
False
57.
strErrMsg =
"Please check user/password"
58.
Else
59.
flgLogin =
True
60.
End
If
61.
62.
Catch
ex
As
Exception
63.
flgLogin =
False
64.
strErrMsg =
"Please check user/password"
65.
End
Try
66.
67.
68.
IF flgLogin =
True
69.
Me
.lbDisplay.Text =
"Welcome "
& txtUser.Text
70.
Else
71.
Me
.lbDisplay.Text = strErrMsg
72.
End
IF
73.
End
Sub
74.
</script>
75.
76.
<html>
77.
<head>
78.
<title>ThaiCreate.Com AD/LDAP (Active Directory)</title>
79.
</head>
80.
<body>
81.
<form id=
"form1"
runat=
"server"
>
82.
<asp:TextBox ID=
"txtUser"
runat=
"server"
Width=
"147px"
></asp:TextBox>
83.
<asp:TextBox ID=
"txtPwd"
runat=
"server"
Width=
"147px"
TextMode=
"Password"
></asp:TextBox>
84.
<asp:Button ID=
"btnLogin"
runat=
"server"
Text=
"Login"
OnClick=
"btnLogin_Click"
/>
85.
<br />
86.
<asp:Label ID=
"lbDisplay"
runat=
"server"
></asp:Label>
87.
</form>
88.
</body>
89.
</html>