 |
|
เอา Code ของ ASP.NET (By VB.NET,C#) เชื่อมต่อกับ Active Directory (AD/LDAP) มาฝากครับ |
|
 |
|
|
 |
 |
|
Code (VB.NET)
<%@ Assembly Name="System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
<%@ Import Namespace="System.DirectoryServices"%>
<%@ Import Namespace="System.Security.Cryptography"%>
<%@ Import Namespace="System.Text"%>
<%@ Page Language="VB" %>
<script runat="server">
Dim initLDAPPath = "dc=tc, dc=thaicreate, dc=com"
Dim initLDAPServer = "192.168.0.5"
Dim initShortDomainName = "tc"
Dim strErrMsg
Public Function MD5(ByVal strString As String) As String
Dim ASCIIenc As New ASCIIEncoding
Dim strReturn As String
Dim ByteSourceText() As Byte = ASCIIenc.GetBytes(strString)
Dim Md5Hash As New MD5CryptoServiceProvider
Dim ByteHash() As Byte = Md5Hash.ComputeHash(ByteSourceText)
strReturn = ""
For Each b As Byte In ByteHash
strReturn = strReturn & b.ToString("x2")
Next
Return strReturn
End Function
Sub Page_Load(sender As Object, e As EventArgs)
End Sub
Protected Sub btnLogin_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim DomainAndUsername As String = ""
Dim strCommu As String
Dim flgLogin As Boolean = False
strCommu = "LDAP://" & initLDAPServer & "/" & initLDAPPath
DomainAndUsername = initShortDomainName & "\" & txtUser.Text
Dim entry As New DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text)
Dim obj As Object
Try
obj = entry.NativeObject
Dim search As New DirectorySearcher(entry)
Dim result As SearchResult
search.Filter = "(SAMAccountName=" + txtUser.Text + ")"
search.PropertiesToLoad.Add("cn")
result = search.FindOne()
If result Is Nothing Then
flgLogin = False
strErrMsg = "Please check user/password"
Else
flgLogin = True
End If
Catch ex As Exception
flgLogin = False
strErrMsg = "Please check user/password"
End Try
IF flgLogin = True
Me.lbDisplay.Text = "Welcome " & txtUser.Text
Else
Me.lbDisplay.Text = strErrMsg
End IF
End Sub
</script>
<html>
<head>
<title>ThaiCreate.Com AD/LDAP (Active Directory)</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtUser" runat="server" Width="147px"></asp:TextBox>
<asp:TextBox ID="txtPwd" runat="server" Width="147px" TextMode="Password"></asp:TextBox>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<br />
<asp:Label ID="lbDisplay" runat="server"></asp:Label>
</form>
</body>
</html>
Code (C#)
<%@ Assembly Name="System.DirectoryServices, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"%>
<%@ Import Namespace="System.DirectoryServices"%>
<%@ Import Namespace="System.Security.Cryptography"%>
<%@ Import Namespace="System.Text"%>
<%@ Page Language="C#" %>
<script runat="server">
string initLDAPPath = "dc=tc, dc=thaicreate, dc=com";
string initLDAPServer = "192.168.0.5";
string initShortDomainName = "tc";
string strErrMsg;
public string MD5(string strString) {
ASCIIEncoding ASCIIenc = new ASCIIEncoding();
string strReturn;
byte[] ByteSourceText = ASCIIenc.GetBytes(strString);
MD5CryptoServiceProvider Md5Hash = new MD5CryptoServiceProvider();
byte[] ByteHash = Md5Hash.ComputeHash(ByteSourceText);
strReturn = "";
foreach (byte b in ByteHash) {
strReturn = (strReturn + b.ToString("x2"));
}
return strReturn;
}
void Page_Load(object sender, EventArgs e) {
}
protected void btnLogin_Click(object sender, System.EventArgs e) {
string DomainAndUsername = "";
string strCommu;
bool flgLogin = false;
strCommu = ("LDAP://"
+ (initLDAPServer + ("/" + initLDAPPath)));
DomainAndUsername = (initShortDomainName + ("\\" + txtUser.Text));
DirectoryEntry entry = new DirectoryEntry(strCommu, DomainAndUsername, txtPwd.Text);
object obj;
try {
obj = entry.NativeObject;
DirectorySearcher search = new DirectorySearcher(entry);
SearchResult result;
search.Filter = ("(SAMAccountName="
+ (txtUser.Text + ")"));
search.PropertiesToLoad.Add("cn");
result = search.FindOne();
if ((result == null)) {
flgLogin = false;
strErrMsg = "Please check user/password";
}
else {
flgLogin = true;
}
}
catch (Exception ex) {
flgLogin = false;
strErrMsg = "Please check user/password";
}
if ((flgLogin == true)) {
this.lbDisplay.Text = ("Welcome " + txtUser.Text);
}
else {
this.lbDisplay.Text = strErrMsg;
}
}
</script>
<html>
<head>
<title>ThaiCreate.Com</title>
</head>
<body>
<form id="form1" runat="server">
<asp:TextBox ID="txtUser" runat="server" Width="147px"></asp:TextBox>
<asp:TextBox ID="txtPwd" runat="server" Width="147px" TextMode="Password"></asp:TextBox>
<asp:Button ID="btnLogin" runat="server" Text="Login" OnClick="btnLogin_Click" />
<br />
<asp:Label ID="lbDisplay" runat="server"></asp:Label>
</form>
</body>
</HTML>
Tag : - - - -
|
|
 |
 |
 |
 |
Date :
2010-04-02 10:47:31 |
By :
webmaster |
View :
5648 |
Reply :
6 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอบคุณครับ จะลองศึกษาดูครับ
|
 |
 |
 |
 |
Date :
2010-04-02 10:54:13 |
By :
tungman |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Dim initLDAPServer = "192.168.0.5" คืออะไรค่ะ สามารถใช้เป็นชื่อเวปเลยได้ไหมค่ะ
Dim initShortDomainName = "tc" อันนี้เรากำหนดขึ้นเองหรอค่ะ
|
 |
 |
 |
 |
Date :
2010-05-09 22:57:33 |
By :
มือใหม่ |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
ขอถามเพิ่มอีกหน่อยน่ะครับ คือเอาไปเขียนแล้วไม่ได้อ่ะครับ
initLDAPPath = "dc=tc, dc=thaicreate, dc=com";
dc แต่ละตัว นั้นคืออะไรเหรอครับ (C#)
มันฟ้องว่า The server is not operational.
|
ประวัติการแก้ไข 2012-07-17 14:52:47 2012-07-17 14:54:53
 |
 |
 |
 |
Date :
2012-07-17 14:52:24 |
By :
KuNgbAng |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
เป็น LDAP Path ของ Server ครับ คนที่ดูแล Network จะรู้ครับ
|
 |
 |
 |
 |
Date :
2012-07-17 20:12:52 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
function MD5 เอาไว้ทำไรอะคับ ไม่เห็นมีเรียกใช้เลย
แล้วถ้าจะเอาค่า password มาแสดงจะทำยังไงคับ
|
 |
 |
 |
 |
Date :
2016-01-20 16:37:21 |
By :
axyz987 |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
Password ดึงมาไม่ได้ครับ 
|
 |
 |
 |
 |
Date :
2016-01-21 14:15:18 |
By :
mr.win |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|