Monday, August 18, 2008

ASP.NET - Drag and Drop on a Webpage

ASP.NET - Drag and Drop on a Webpage

Introduction

Many web applications are required to be complex yet intuitive to the user. One way to achieve this is to create objects or areas of the web page draggable and droppable. Unfortunately, ASP.NET server controls do not have the Drag events and DragEventArgs. Fortunately, there is a way to enable drag and drop functionalities within a web page. Now keep in mind, this is not like a Windows application where you can drag files from the user's PC to the application. The drag and drop is limited to the boundaries of the web page.




Using the code



  1. Create a web application within your solution.

  2. Create a web page.

  3. Add the provided JavaScript in the <HEAD> of your <HTML>.

  4. Add two textboxes and a button in the web form.

  5. Add the DrawContainers method to the code-behind of the web page.

  6. Ensure that the click event for the button calls the DrawContainers method.


or




  • Download the source code and run the sample application.




JavaScript




This is the JavaScript that enables the drag and drop to take place:




Collapse



--------- Drag and Drop JavaScript -----------
var TimerID = 0;
var oEl = null;
var oTarget = null;
var beginDrag = false;
var tmpHTML = ""; function killTimer()
{ if (TimerID != 0 )
{
clearTimeout(TimerID);
TimerID = 0;
}
} function fnShowDragWindow()
{ var obj = document.all("DW");

killTimer(); if (oEl == null)
{ return;
}

obj.style.top = oEl.offsetTop;
obj.style.left = oEl.offsetLeft;
obj.style.height = oEl.offsetHeight - 3;
obj.style.width = oEl.offsetWidth - 3;
obj.innerText = oEl.SpecimenId;
obj.style.display = "block";
obj.style.zIndex = 999;

window.document.attachEvent( "onmousemove" , fnMove );
window.document.attachEvent( "onscroll" , fnMove );
window.document.attachEvent( "onmousemove" , fnCheckState );
window.document.attachEvent( "onmouseup" , fnRelease );
window.document.attachEvent( "onselectstart", fnSelect ); //window.document.attachEvent("onmouseover",setTarget); beginDrag = true;
} function setTarget(id)
{ var src = document.getElementById(id); if (src == null)
{ return;
} if (src.target == 'true')
{
oTarget = src;
} else {
oTarget = null;
}
} function BeginDrag(id)
{ // Get the item to be dragged. oEl = document.getElementById(id); // Is there an item? if(oEl == null)
{ return;
}

tmpHTML = oEl.innerHTML; // Set the window timeout. TimerID = setTimeout(fnShowDragWindow, 1);
} function fnCheckState()
{ if(event.button != 1)
{
fnRelease();
}
} function fnSelect()
{ return false;
} function fnMove()
{ if (event.button != 1)
{
fnRelease(); return;
} var obj = document.all("DW");

obj.style.top =
event.clientY - (obj.offsetHeight/2) + window.document.body.scrollTop;
obj.style.left = event.clientX + window.document.body.scrollLeft;
window.status = 'Top=' + obj.style.top + ' Left=' + obj.style.left; if (event.clientY > window.document.body.clientHeight - 10 )
{
window.scrollBy(0, 10);
} else if (event.clientY < 10)
{
window.scrollBy(event.clientX, -10);
}

} function fnRelease()
{ if (beginDrag == false) return;

window.document.detachEvent( "onmousemove" , fnMove );
window.document.detachEvent( "onscroll" , fnMove );
window.document.detachEvent( "onmousemove" , fnCheckState );
window.document.detachEvent( "onmouseup" , fnRelease );
window.document.detachEvent( "onselectstart", fnSelect ); //window.document.detachEvent( "onmouseover", setTarget ); var obj = document.all("DW"); if (oTarget != null)
{ var targetHTML = oTarget.innerHTML; var targetSpecId = oTarget.SpecimenId; var sourceSpecId = oEl.SpecimenId;

oEl.innerHTML = targetHTML;
oEl.SpecimenId = targetSpecId;
oTarget.SpecimenId = sourceSpecId;

oTarget.innerHTML = tmpHTML; // Is the container empty? if(oTarget.innerHTML != "")
{
oTarget.style.backgroundColor="beige";
} else {
oTarget.style.backgroundColor = "turquoise";
} if(oEl.innerHTML != "")
{
oEl.style.backgroundColor = "beige" } else {
oEl.style.backgroundColor = "turquoise" }
}

killTimer();

obj.style.display = "none";
oEl = null;
oTarget = null;
beginDrag = false;
TimerID = 0;
} function CancelDrag()
{ if (beginDrag == false)
{
killTimer();
}
}
--------- End of Drag and Drop JavaScript -----------



C# code behind - DrawContainers method




This is the code behind used to create the drag and drop objects:




Collapse



private void DrawContainers()
{
TableRowCollection trCol = this.Table1.Rows;
TableRow tr = null; // Should we continue? if(this.txtContX.Text == null this.txtContY.Text == null) return; // Size of the row. int rowSize = Int32.Parse(this.txtContX.Text); // Number of rows. int rowNumber = Int32.Parse(this.txtContY.Text); // Total number of containers. int numberOfContainers = rowSize * rowNumber; // Boolean value for empty table cells. bool isEmpty = false; // Loop through all of the containers. for(int i=0; i< numberOfContainers; i++)
{ // new row mod. int newRow = i % rowSize; // Should we create a new row? if(tr == null newRow == 0)
{
tr = new TableRow();
trCol.Add(tr);
} // Empty cell generator. if((i+1)%17==0)
{
isEmpty = true;
} else {
isEmpty = false;
} // Set the cell collection. TableCellCollection tdc = tr.Cells; // Create a new table cell. TableCell td = new TableCell();
td.ID = "cell_" + i.ToString(); // Set the cell backcolor. td.BackColor = Color.Turquoise; // Set the cell's class. td.CssClass = "SpecimenLoc";

td.Attributes.Add("SpecimenId", ""); // Is the cell empty? if(!isEmpty)
{
td.Attributes.Add("SpecimenId", i.ToString());
td.BackColor = Color.Beige;
td.Text = i.ToString();
} // Add javascript attributes to the cell. td.Attributes.Add("target", "true");
td.Attributes.Add("onmousedown", "BeginDrag(this.id);");
td.Attributes.Add("onmouseup", "CancelDrag();");
td.Attributes.Add("onmouseover", "setTarget(this.id);this.style.cursor='hand';");
td.Attributes.Add("onmouseout", "this.style.cursor='default';");
td.Width = Unit.Pixel(35);
td.Height = Unit.Pixel(35); // Add the cell to the cell collection. tdc.Add(td);
}
}



C# code behind - button click event




The click event of the button:




private void btnDrawContainers_Click(object sender, System.EventArgs e)
{ // Draw the containers. this.DrawContainers();
}


About Kenny Young


B.S. - University of South Florida.

Kenny currently works as a software architect at the Data Technology Coordinating Center for the Rare Diseases Clinical Research Network http://www.rarediseasesnetwork.org.
Click here to view

Friday, August 15, 2008

Implementing Active Directory Services in ASP.NET 2.0

Active Directory and VB.NET




Microsoft Active Directory is a directory service that provides the foundation for distributed networks built on Windows 2000 and later domain controllers. The Active Directory APIs provide access to the data stored in a directory.



Active Directory Architecture.



The directory system agent (DSA) is the process that provides access to the store. The store is the physical store of directory information located on a hard disk. Clients access the directory using one of the following mechanisms supported by the DSA:






  • LDAP clients connect to the DSA using the LDAP protocol. LDAP is an acronym for Lightweight Directory Access Protocol. Active Directory supports LDAP 3.0, defined by RFC 2251, and LDAP 2.0, defined by RFC 1777.





  • MAPI clients such as Microsoft Exchange connect to the DSA using the MAPI remote procedure call interface.





  • Windows clients that use a previous version of Windows NT connect to the DSA using the Security Account Manager (SAM) interface.





  • Active Directory DSA's connect to each other to perform replication using a proprietary remote procedure call interface.




Active Directory data model is derived from the X.500 data model. The directory holds objects that represent things of various sorts, described by attributes. The universe of objects that can be stored in the directory is defined in the schema. For each object class, the schema defines what attributes an instance of the class must have, what additional attributes it may have, and what object class can be a parent of the current object class.



Active Directory schema is implemented as a set of object class instances stored in the directory. This is very different than many directories that have a schema but store it as a text file read at startup. Storing the schema in the directory has many advantages. For example, user applications can read it to discover what objects and properties are available.



Active Directory can consist of many partitions or naming contexts. The distinguished name (DN) of an object includes enough information to locate a replica of the partition that holds the object. Many times however, the user or application does not know the DN of the target object or which partition might contain the object. The global catalog (GC) allows users and applications to find objects in an Active Directory domain tree, given one or more attributes of the target object. The global catalog contains a partial replica of every naming context in the directory. It contains the schema and configuration naming contexts as well. This means the GC holds a replica of every object in Active Directory but with only a small number of their attributes.



The global catalog is built automatically by Active Directory replication system. The replication topology for the global catalog is generated automatically. The properties replicated into the global catalog include a base set defined by Microsoft. Administrators can specify additional properties to meet the needs of their installation.



Interfaces for accessing the Active Directory.






  1. LDAP. The Lightweight Directory Access Protocol (LDAP) is a directory service protocol that runs on a layer above the TCP/IP stack, and provides a mechanism for connecting to, searching, and modifying Internet directories. The LDAP directory service is based on a client-server model. The function of LDAP is to allow access to an existing directory. The data model (data and namespace) of LDAP is similar to that of the X.500 OSI directory service, but with lower resource requirements due to its streamlined features. The associated LDAP API simplifies writing Internet directory service applications.





  2. ADSI. Active Directory Service Interfaces (ADSI) is a set of COM interfaces used to access the capabilities of directory services from different network providers in a distributed computing environment, to present a single set of directory service interfaces for managing network resources. Administrators and developers can use ADSI services to enumerate and manage the resources in a directory service, regardless of the network environment that contains the resource.





  3. System.DirectoryServices. System.DirectoryServices is a namespace in the .NET Framework that provides simple programming access to LDAP directories such as Active Directory. System.DirectoryServices is built on the Active Directory Service Interfaces (ADSI) API.

    Using System.DirectoryServices namespace.



    This article will emphasize in the benefits of using the namespace System.DirectoryServices, such as:






    • Designed completely within common language runtime parameters. System.DirectoryServices leverages common language runtime features, such as garbage collection, custom indexer, and dictionaries (hashtables). It also offers other common language runtime features such as automatic memory management, efficient deployment, an object-oriented framework, evidence-based security and exception handling.





    • Simple to use. Although ADSI scripting was effective for many tasks, C++ applications for ADSI are sometimes difficult to develop. System.DirectoryServices implements some basic ADSI tasks to enable more efficient and effective application development.




    System administrators can use System.DirectoryServices to automate tasks to manage network resources in the directory, such as users and computers and also to build applications that search, create, or modify objects in a directory.



    Requirements. System.DirectoryServices is supported on Windows Server 2003. System.DirectoryServices can be redistributed on Windows 98, Windows 98 SE and Windows NT 4.0, as long as the DS Client is installed on client machines. It can also be redistributed on Windows 2000 Windows XP.



    I developed a lot of business objects which access the Active Directory, leveraging any application which needs the platform as its main database and for publishing objects in enterprise network.



    It's defined the interface for the business objects which serve as changing or setting up the password for a specific user in the directory. Later this interface is implemented with a class, which instances make the real interaction with the directory.



    In listing 1, it's shown the contract IADPasswdManager and the class ADPasswdManager.



    Imports System

    Imports System.DirectoryServices

    Namespace OLAActiveDirectory.Management

    Public Interface IADPasswdManager

    Sub ChangePassword(ByVal objUser As IADUser, ByVal strOldPasswd As String, ByVal strNewPasswd As String)

    Sub SetPassword(ByVal objUser As IADUser, ByVal strPasswd As String)

    End Interface

    Public Class ADPasswdManager : Implements IADPasswdManager

    Public Sub New()

    End Sub

    Public Sub SetPassword(ByVal objUser As IADUser, ByVal strPasswd As String) Implements IADPasswdManager.SetPassword

    Dim objLoginEntry As DirectoryEntry=objUser.DirectoryEntry

    If Not objLoginEntry Is Nothing Then

    objLoginEntry.Invoke("SetPassword", New Object(){strPasswd})

    objLoginEntry.CommitChanges()

    End If

    End Sub

    Public Sub ChangePassword(ByVal objUser As IADUser, ByVal strOldPasswd As String, ByVal strNewPasswd As String) Implements IADPasswdManager.ChangePassword

    Dim objLoginEntry As DirectoryEntry=objUser.DirectoryEntry

    If Not objLoginEntry Is Nothing Then

    objLoginEntry.Invoke("ChangePassword",New Object(){strOldPasswd,strNewPasswd})

    objLoginEntry.CommitChanges()

    End If

    End Sub

    End Class

    End Namespace



    Listing 1.



    A business entity must be defined for the users of the directory. It has all the information of a particular user in the directory knowing its Distinguished Name (DN).



    It's defined an interface IADUser and the implementation is realized in the class ADUser as shown in the Listing 2.



    Imports System

    Imports System.DirectoryServices

    Imports System.Collections

    Namespace OLAActiveDirectory.Management

    Public Interface IADUser

    ReadOnly Property DirectoryEntry() As DirectoryEntry

    ReadOnly Property IsUser() As Boolean

    ReadOnly Default Property Item(ByVal strKey As String) As PropertyValueCollection

    End Interface

    Public Class ADUser : Implements IADUser

    Private ReadOnly m_objUserEntry As DirectoryEntry

    Public Sub New(ByVal strLogin As String, ByVal strRootPath As String)

    Dim objRootEntry As DirectoryEntry = New DirectoryEntry(strRootPath)

    Dim objADSearcher As DirectorySearcher = New DirectorySearcher(objRootEntry)

    objADSearcher.Filter="(&(objectClass=user)(anr=" & strLogin & "))"

    Dim objResult As SearchResult=objADSearcher.FindOne()

    If (Not objResult Is Nothing) Then

    Me.m_objUserEntry=objResult.GetDirectoryEntry()

    Else

    Me.m_objUserEntry=Nothing

    End If

    End Sub

    Public ReadOnly Property DirectoryEntry() As DirectoryEntry Implements IADUser.DirectoryEntry

    Get

    Return Me.m_objUserEntry

    End Get

    End Property

    Public ReadOnly Default Property Item(ByVal strKey As String) As PropertyValueCollection

    Get

    Return Me.m_objUserEntry.Properties(strKey)

    End Get

    End Property

    Public ReadOnly Property IsUser() As Boolean Implements IADUser.IsUser

    Get

    Return Not Me.m_objUserEntry Is Nothing

    End Get

    End Property

    End Class

    End Namespace



    Listing 2.



    In the Presentation Layer resides an instance of the class ADUserInfoShower whose role is to create an information string for a specific user. This object is independent of the technology used for showing the user information. That is, this string can be rendered in a Web Browser, a Windows Client and a Mobile Device. In the listing 3, it's shown the code for this business object.



    Imports System

    Namespace OLAActiveDirectory.Management

    Public Interface IADUserInfoShower

    Function GetInformation(ByVal objUser As IADUser, ByVal strSep As String) As String

    End Interface

    Public Class ADUserInfoShower : Implements IADUserInfoShower

    Private Function prvInfoBuilder(ByVal objUser As IADUser, ByVal strSep As String) As String

    Dim strResult As String

    strResult="Fullname:" & objUser("givenName").Value & " " & objUser("sn").Value

    strResult &= strSep & "Mail:" & objUser("mail").Value

    strResult &= strSep & "Telephone(s):" & objUser("telephoneNumber").Value

    For Each strPhone As String In objUser("otherTelephone")

    strResult &= strSep & strPhone

    Next strPhone

    Return strResult

    End Function

    Public Sub New()

    End Sub

    Public Function GetInformation(ByVal objUser As IADUser, ByVal strSep As String) As String Implements IADUserInfoShower.GetInformation

    Return Me.prvInfoBuilder(objUser,strSep)

    End Function

    End Class

    End Namespace





====================================================================
จากตัวอย่างในเรื่องการ สร้าง Authen กับ Active Directory นั้น ทำการ authen นั้นผ่าน protocal LDAP ได้ ซึ่ง ผมได้ทำตัวอย่างไว้ ทั้งแบบ VB6 และแบบของ .Net เอง สร้างเป็น Class DLL แล้วนำไปใช้ใน Project นะครับ ทีนี้ เราก็จะสามารถ ตัดปัญหายุ่งยากเรื่อง username และ password อันหลากหลายในระบบงานที่พัฒนาขึ้นมาได้แล้วนะครับ ก็จะเป็นการทำ ในเรื่อง single singon นั่นเอง


============ เริ่มจาก ของ VB6 ก่อนเลยนะครับ ============
สำหรับในตัว VB6 นั้นข้อดีของการสร้าง Active X control จาก vb6 ทำให้เราสามารถ นำ Dll นั้นไปใช้งานกับ Project ที่พัฒนาด้วย เทคโนโลยีตัวเก่า ได้นะครับ เช่นใช้งานกับ vba ของ access หรือ excel หรือใช้งานร่วมกับ ภาษาโปรแกรมตัวอื่นๆ ก็ได้ครับ หรือ จะเอาไปใช้กับเทคโนโลยีของ .net ด้วยก็ยังได้ เพราะ dll ของเทคโนโลยีตัวเก่า สามารถ ใช้งานกับตัวใหม่ได้ แต่ถ้าพัฒนาจาก .net จะไม่สามารถนำไปใช้กับ ระบบงานเก่าๆ ได้ครับ


ของเริ่มเรื่อง เลยนะครับ


เริ่มจาก เปิด visual studio 6 ขึ้นมา สร้าง Project Active X Dll นะครับ
จากนั้นให้ เปลี่ยนชื่อ class เป็นชื่ออะไรก็ได้ ในที่นี้ผม ใช้ชื่อ cssLoginAD





Option Explicit
Const ADS_SECURE_AUTHENTICATION = 1
' method สำหรับการตรวจสอบ Login AD
Public Function ValidateAuthenAD(ByVal UserName As String, _
ByVal Domain As String, _
ByVal Password As String) As Integer
Dim MyNamespace As IADsOpenDSObject
Dim X As IADsContainer
Dim oUserName As String
Dim oPassword As String
oUserName = UserName
oPassword = Password
' ตรวจสอบ login AD
On Error GoTo CleanUp
If InStr(1, UserName, ".", vbTextCompare) <> 0 And InStr(1, UserName, "@", vbTextCompare) = 0 Then
oUserName = UserName
End If
' กำหนด Potocal LDAP สำหรับการเข้าไปค้นหา
Set MyNamespace = GetObject("LDAP:")
Set X = MyNamespace.OpenDSObject("LDAP://" + Domain, oUserName, oPassword, ADS_SECURE_AUTHENTICATION)
ValidateAuthenAD = 0
GoTo FinFonction
CleanUp:
Select Case Err.Number
Case -2147023570 ' Return Error หาก password ไม่ถูกต้อง
ValidateAuthenAD = 1
Case -2147016646 ' return Error หาก Domain ไม่ถูกต้อง
ValidateAuthenAD = 2
Case Else ' return Error อื่นๆ
ValidateAuthenAD = 99
End Select
FinFonction:
Set MyNamespace = Nothing
Set X = Nothing
End Function



หลักการของโปรแกรม



Class ActiveX cssLoginAD ในเวอร์ชั่นนี้จะประกอบไปด้วย Method ให้เรียกใช้งานได้ดังต่อไปนี้


ValidateAuthenAD Method เป็นชนิด Integer การทำงานของ method จะเปิด Directory Service ขึ้นมา เพื่อเข้าไปตรวจสอบ user และ pass และ Domain controller โดยผ่าน โปรโตคอล LDap เพื่อ Search หา user และ pass ใน DC ว่ามีอยู่หรือไม่ โดยจะ คืนค่ากลับมา เป็น 0 คือ ตรวจหาพบ หรือ error อื่นๆ ดังนี้
0 = login ถูกต้อง
1 = password ไม่ถูก
2 = ไม่พบ Domain
99 = error อื่นๆ
parameter ที่จะส่งเข้าไปใน method นี้
1. UserName
2. Domain
3. Password





//จากนั้นให้ทำการ Complier dll ออกมาเท่านี้ก็จะสามารถใช้งาน Authen AD ได้แล้วครับ

//เมื่อนำ Dll นั้นไปใช้กับ Project 

// สร้าง ฟังชั่นขึ้นมาเพื่อตรวจสอบ กับ Class cssLoginAD
Private Function chkLogin(ByVal user As String, ByVal pass As String, ByVal dc As String) As Boolean
Dim result As Boolean
Dim AuthenAD As New cssLoginAD
Dim valReturn As Integer
result = False ' กำหนด ให้ ค่าเริ่มต้นเป็น False
If user <> "" And pass <> "" And dc <> "" Then
valReturn = AuthenAD.ValidateAuthenAD(user, dc, pass)
Select Case valReturn
Case 0 ' กรณี Login ถูกต้อง
result = True
Case 1 ' กรณี password ผิด
MsgBox "กรุณาระบุ password ให้ถูกต้องด้วย !!", vbOKOnly + vbCritical, "Login ผิด !!"
Case 2 ' กรณี ชื่อ Domain ผิด
MsgBox "กรุณาระบุ Domain ให้ถูกต้องด้วย !!", vbOKOnly + vbCritical, "Login ผิด !!"
Case Else ' error อื่นๆ
MsgBox "Error !!"
End Select
Else
' หากกรอกข้อมูลไม่ครับ
MsgBox "กรุณากรอกข้อมูลให้ครบถ้วนด้วย !!", vbOKOnly + vbCritical, "Validate Form!!"
End If
Set AuthenAD = Nothing
chkLogin = result
End Function
‘ ส่วนของ ปุ่ม Submit
Private Sub Command1_Click()
If chkLogin(txtuser.Text, txtpassword.Text, Combo1.Text) Then
MsgBox "Login ถูกต้อง ยินดีต้อนรับคุณ " & txtuser.Text
Call clearText
End If
End Sub

จากตัวอย่างด้านบนเป็นการใช้ Active X Dll ที่พัฒนาจาก vb6 สำหรับระบบงาน เก่าๆ หรือใช้งาน กับ vba กับ Access นะครับ
ทีนี้ เรามาดูพระเอกของบทความนี้กันดีกว่า สร้าง Authen Active Directory ด้วย .Net

สำหรับใน .Net นั้นนะครับ ได้เตรียม namespace ไว้ให้เราเรียบร้อย จะใช้  System.DirectoryServices
ทีนี้เรามาสร้าง Class สำหรับ authen กับ Acitve directory กันนะครับ

อันดับแรก ก็เปิด vs.net แล้ว สร้าง class ขึ้นมานะครับ ดังโคดด้านล่างนี้



 Imports System.DirectoryServices  ' impoart namespace สำหรับจัดการ directoryservice

Namespace LoginAD ' สร้าง namespace ชื่อ LoginAD เอาไว้จัดกลุ่ม class นะครับ
Public Class css_AuthenAD
' สร้าง private property
Private strUser As String
Private strPass As String
Private strDomain As String
Private authenType As AuthenticationTypes

' ===============================
' สร้าง Public method
' method สำหรับ Set Domain
Public Function SetDomain(ByVal strValue As String) As Boolean
If strValue.Length <= 0 Then Return False
Me.strDomain = "LDAP://" & strValue
Return True
End Function

' method สำหรับ Set User
Public Function SetUser(ByVal strValue As String) As Boolean
If strValue.Length <= 0 Then Return False
Me.strUser = strValue
Return True
End Function







' method สำหรับ Set password
Public Function SetPass(ByVal strValue As String) As Boolean
If strValue.Length <= 0 Then Return False
Me.strPass = strValue
Return True
End Function

Public Sub SetAuthenType(Optional ByVal bValue As Boolean = False)
If bValue Then
' type สำหรับ edirectory
authenType = AuthenticationTypes.SecureSocketsLayer
Else
' type สำหรับ Active Directory
authenType = AuthenticationTypes.Secure
End If
End Sub

' และ method login สำหรับเอาไว้ authen นะครับ
Public Function Login() As Boolean
Dim result As Boolean = False
Dim DirEntry As New DirectoryEntry(Me.strDomain, _
Me.strUser, _
Me.strPass, _
Me.authenType)
Try
Dim native As Object = DirEntry.NativeObject()
result = True
Catch ex As Exception
result = False
End Try
Return result
End Function
' ===============================
End Class
End Namespace


Using Active Directory In ASP.NET - Dump Schema Information

Using Active Directory In ASP.NET - Dump Schema InformationBy Softomatix.
An article on using System.DirectoryServices classes in ASP.NET



This article is first in the series demonstrating the use of Active Directory in ASP.NET. Of course all the demo code is written in language of choice - C#. This series will not go into discussion of Active Directory or LDAP servers. We are assuming that the readers of these articles have basic understanding of these technologies.

.NET namespace and classes utilized

  • System.DirectoryServices

  • System.DirectoryServices.DirectoryEntry

  • System.DirectoryServices.DirectorySearcher

  • System.DirectoryServices.SearchResultCollection

  • System.DirectoryServices.SearchResult

  • System.DirectoryServices.ResultPropertyCollection

  • System.DirectoryServices.PropertyValueCollection



What is this article about?

Searching an Active Directory is one of the major tasks in manipulation of various resources. When I started with ADSI programming, I used to look for right kind of filter values to use. Some time I had to go back forth and look at the Active Directory schema to find value I should be using to get the information I was looking for. For example If you want to get the information when the user account was last changed, you need to create a filter looking for whenchanged property in schema. So we decided to write a small dump utility that will display all the properties that are used to describe a user's account in Active Directory.


How To Do It

The first step in using Directory Services interfaces is to make connection with the node that you want to search for. .NET framework provides DirectoryEntry class to specify the search node. For example if you want to search for a resource in whole domain, then you need to connect to the top node of domain in Active Directory. It is very important that you specify the search location as close as possible to the nearest location where the resource could be found. Otherwise the search will take longer time. For example if You want to search for a user information, you need to specify the location as User node and not the whole domain resource tree.


string strLDAP = "LDAP://pardesiservices.com"
m_obDirEntry = new DirectoryEntry(strLDAP);


After initializing the search node, you need to specify the query string in DirectorySearcher class object. You can set various parameter values of this object to fine tune your search and how the results will be returned. For this article we will only mention Filter property. This is the property that you will use to set your query string. The query string shall be specified in LDAP format. For example if you want to search for a user "foo", you can specify the query string as (cn=foo). It is very important that you specify the filter/query in parentheses. For more information on this property, look in the .NET documentation for Filter property of DirectorySearcher class.


DirectorySearcher srch = new DirectorySearcher(m_obDirEntry);
srch.Filter = "(cn=foo)";

The next step is to start the search. You will call FindAll or FindOne method on DirectorySearcher class object. If you are only interested in the first entry of the returned results, then call FindOne. Otherwise if you want to get all the search results, call FindAll method. This method returns the results as SearchResultCollection class subject.


The other property that is worth mentioning is PropertiesToLoad. This property lets you specify the values you want the search to return. If you don't specify any properties, then search returns all the properties by default. Therefore if you are only interested in some of the values, then make sure that you specify those properties in the PropertiesToLoad. This way you can avoid unnecessary loading of all the values in memory.


SearchResultCollection results;
results = srch.FindAll();

After getting all the search results, you can iterate over each SearchResult entry in the SearchResultCollection. The SearchResult class object has Properties property that returns ResultPropertyCollection object. This contains all the properties were found by search you specified.


foreach (SearchResult result in results)
{
ResultPropertyCollection propColl = result.Properties;
}

ResultPropertyCollection exposes ProperyNames property that returns the collection containing names of all the properties returned by search. You can iterate over this collection to get the names. We used this technique to get the names of all properties exposed by User objects.


foreach (string strKey in propColl.PropertyNames)
{
foreach (object obProp in propColl[strKey])
{
this.AppendPropertyNode(obTopNode, strKey, obProp);
}
}

And then you can use this property names to extract particular values from ResultPropertyCollection dictionary.


Demo Code


We have included the demo code with this article. All the Active Directory implementation has been encapsulated in ADSIUtil class. We have also created an utility class, ADSIUser. This class parses the search results and saves as a XMLDocument. And it also exposes some properties to get specific information like First Name, Last Name, etc. This class is not complete. But we will expand this as the series progress.


Platforms Tested


We have tested the included project on following platforms

  • Windows 2000 Adv. Server

  • Windows .NET Enterprise Server (Beta 3)

Contact Us


For any suggections ot comment you can visit us as at Softomatix or write to us, softomatix@pardesiservices.com



About Softomatix

To learn more about us, Please visit us at http://www.netomatix.com
Click
here to view Softomatix's online profile.

HOW TO: ASP.NET Generate PDF Output

สวัสดีครับ สำหรับในบทความนี้นะครับ จะเป็นบทความต่อจาก คราวที่แล้ว คือ วิธีการเขียน asp.net ให้ gen ข้อมูลออกมาเป็น PDF file ครับ

กล่าวนำนิดนึง หลายคนที่พัฒนา web application ด้วย asp.net คงจะ เจอปัญหา เวลาสั่ง print report ซึ่ง เมื่อ เราสั่ง print แล้วโดยปกติ จะได้ url อันไม่พึงประสงค์ติด มาพร้อมกับ รายงานด้วยนะครับ
วิธีการที่จะทำให้ เวลาที่เราสั่ง print แล้ว ได้ออกมาเป็นรูปแบบรายงานที่สวยงาม ไม่มี url ติดมา นั้น เราจำเป็นต้องทำ ออกมาเป็น PDF ไฟล์ก่อน จากนั้นถึงจะสั่ง print นะครับ

ซึ่งการทำ ให้ asp.net export ออกมาเป็น pdf มีอยู่มากมายหลายวิธี หรือใช้ component ช่วย ก็มีหลายตัว ทั้งเสียเงิน และไม่เสียเงิน

สำหรับวิธีการนี้ ผมจะใช้ component ฟรี ที่ช่วยในการ เขียน PDF นะครับ โดยผมขอแนะนำตัวช่วย ที่ได้กล่าวไปเมื่อกี้นะครับ คือ Itextsharp
หลายๆ คนคงจะรู้จัก กันพอสมควรแล้วนะครับ ว่า itextsharp คือ open source ที่พัฒนาขึ้นมาเพื่อ ทำงานกับ เอกสารได้เป็นอย่างดี โดยจะสามารถดาวน์โหลด component ได้ที่ http://itextsharp.sourceforge.net/ เมื่อดาวน์โหลดมาแล้ว เราจะได้ไฟล์ itextsharp.dll มา ตัวนี้แหละครับที่เราจะเอามา add reference เข้ามาใน Project ของเรา เพื่อเขียน gen pdf ไฟล์ครับ

ขั้นตอนในการใช้งาน Itextsharp เพื่อ เขียน PDF
1. เปิด vs.net ขึ้นมา สร้าง project ขึ้นจากนั้น ก็ทำการ add reference ไฟล์ itextsharp.dll ที่ดาวน์โหลด มา เข้าไปไว้ในโปรเจค เพื่อจะสามารถใช้งาน library ต่างๆได้
2. ที่ส่วนหัวของหน้า code ให้ทำการ import namespace เข้ามา

using iTextSharp.text;
using iTextSharp.text.pdf;

3. ส่วนโคดที่จะใช้ในการเขียน เพื่อสร้างเอกสาร pdf นั้น จะให้อยู่ใน event page_load นะครับ แล้วเขียนดังนี้
// ขั้นแรก จะต้องทำการ create object document ขึ้นมา
Document document = new Document(); // การ create document แบบแนว ตั้งปกติ ซึ่งเราอาจจะ จัดเอกสารให้เป็นขนาดอื่นๆ หรือ เป็นแนวตั้งได้ดังนี้
// Document document = new Document(PageSize.A4.Rotate(),20,20,20,20);
// ส่วนที่เป็น สีน้ำเงิน คือ ส่วนที่จะระบุเข้าไปในเอกสาร คือ ให้มีขนาด เป็น A4 และ สีแดงคือ margin ของเอกสาร

string filename = Server.MapPath("
../report_pdf/ชื่อไฟล์.pdf"); // กำหนด path และชื่อไฟล์
PdfWriter.GetInstance(document, new FileStream(filename, FileMode.Create));


// เริ่มทำการ Add meta information ของเอกสาร
document.AddAuthor("Test asp.net gen Report PDF");
document.AddSubject("This is the result of Report.");
document.Open(); // เปิด object เอกสารเพื่อทำการเขียนข้อมูล

// กำหนด font ภาษาไทย เราสามารถกำหนด ชนิดของ font ได้ว่าจะให้เป็น font ตัวไหน หรือจะ upload font ขึ้นไปไว้บน server แล้วทำการ อ้าง path ก็ได้นะครับ หากไม่กำหนด font เราจะสามารถใช้ ภาษาไทย ได้นะครับ
BaseFont EnCodefont = BaseFont.CreateFont(Server.MapPath("../font/ANGSA.TTF"), BaseFont.IDENTITY_H, BaseFont.EMBEDDED);
Font Nfont= new Font(EnCodefont, 18, Font.NORMAL, new Color(0, 0, 255));
// ===================

// การเขียน ข้อมูลลงใน PDF นั้นทำได้ ด้วยการใช้ method add และ ข้อมูลที่จะใส่ไว้ใน method add นั้น เราสามารถ สร้าง object ข้อมูลได้ หลายอย่างมากครับ ผมจะขอ นำเสอน ทีละอย่างนะครับ
// อย่างแรก การสร้างข้อมูลด้วย object paragraph แล้วกำหนด รูปแบบ font ด้วยตัวแปร Nfont ที่ได้สร้างไว้
document.Add(new Paragraph("รายงานข้อมูลประจำวันที่ " + DateTime.ToString("dd/MM/yyyy"), Nfont));
document.Close();

// อย่างที่สองการสร้าง เป็น table ด้วย object table
Table DT = new Table(4); // สร้าง table columns 4 columns
DT.Padding = 2; // กำหนด ระยะของ ขอบ ตารางและตัวอักษร
DT.Spacing = 0; //กำหนดระยะของขอบตาราง
DT.DefaultHorizontalAlignment = Element.ALIGN_CENTER; // จัดเรียก alignment
DT.DefaultVerticalAlignment = Element.ALIGN_TOP;
float[] headerwidths = { 12, 18, 10, 10 }; // กำหนด ความกว้างของ columns
DT.Widths = headerwidths;
DT.WidthPercentage = 100;
DT.AddCell(new Phrase("ลำดับ", Sfont)); // columns 1
DT.AddCell(new Phrase("ชื่อ - นามสกุล", Sfont)); // columns 2
DT.AddCell(new Phrase("เพศ", Sfont)); // columns 3
DT.AddCell(new Phrase("อายุ", Sfont)); // columns 4

// เมื่อได้ object tableของข้อมูลมาแล้ว ก็จะทำการ Add ใน method add ของ object document
document.Add(DT);
document.Close();

// เพียงเท่านี้ เราก็จะสามารถ create PDF ไฟล์ได้แล้วครับ
// ที่เหลือ ก็จะเป็นการ นำเอาไปปรับเปลี่ยนวิธีการเพื่อแสดง ผล แล้วก็ advance ต่อไป ซึ่ง จะมีทั้งตัวอย่าง การทำในแบบต่างๆ อยู่ในไฟล์ ที่ itextsharp ได้ทำไว้ให้เป็น sample ด้วยมากมาย ผมขอ ยกมากล่าว เพียงเท่านี้นะครับ

asp.net connect upload FTP

ในเรื่องการ upload ไฟล์ หลายคน อาจจะเจอปัญหา การ upload ไฟล์ ขนาดใหญ่มากๆ ซึ่ง หากเราใช้ asp.net ปกติ upload ไฟล์ขนาดใหญ่ จะต้องทำการ เซ็ทค่า ให้สามารถ upload ไฟล์ขนาดใหญ่ได้เสียก่อน ไม่อย่างนั้น จะเกิดการ timeout ขึ้น การ เซ็ทค่า เราอาจจะทำจาก web.config ได้ดังนี้
ซึ่งค่าปกติ ที่กำหนดไว้ โดยประมาณคือ 4M หรือ ขนาด maxRequestLength = 4096 ซึ่งเรา
จากการ upload แบบธรรมดาที่ได้กล่าวไปนั้นจะเป็นการ upload โดยผ่าน HTTP เต็มที่ของไฟล์ ก็คงไม่สามารถ upload ไฟล์ ขนาดเป็นร้อยM ได้
ดังนั้นวิธีที่ดีที่สุดสำหรับการ upload ไฟล์ ขนาดใหญ่มากๆ ด้วย asp.net จึงจำเป็นต้องใช้ protocal FTP สำหรับ รับส่งข้อมูลโดยตรง
สำหรับการใช้ asp.net ติดต่อไปยัง FTP เพื่อ ทำการ Transfer Data ผ่าน Protocal FTP นั้น เราสามารถมารถทำได้ สำหรับในตัวอย่างบทความนี้ จะเป็นการใช้ asp.net by C# ในการเขียน Class เพื่อติดต่อ FTP ไปยัง host แล้วทำการ upload ไฟล์ ขึ้นไปนะครับ
ขั้นแรก ที่ project ให้ add new ITEM Class ขึ้นมา จากนั้นก็ทำการสร้าง class namespace FTP

สามารถดาวน์โหลด class FTPclient ได้จากที่นี่

เมื่อได้ Class Namespace สำหรับ Ftpclient เรียบร้อยแล้ว ต่อไปจะเป็นการ สร้างโคดสำหรับเรียกใช้งาน class namespace ftpต่อมา Addnew item สร้างไฟล์ aspx ปกติ แล้วสร้างฟอร์ม upload ไฟล์

โคดทั้งหมด สามารถดาวน์โหลดได้ ที่ link ด้านบนครับ

เมื่อดาวน์โหลดไฟล์ทั้งหมดไปแล้ว ในหน้า CS จะมีตัวแปร FTPhost,FTPuser,FTPpass อย่าลืมระบุค่าของ ftp server ลงไปด้วยนะครับ

Active Directory and VB.NET

Microsoft Active Directory is a directory service that provides the foundation for distributed networks built on Windows 2000 and later domain controllers. The Active Directory APIs provide access to the data stored in a directory.

Active Directory Architecture.

The directory system agent (DSA) is the process that provides access to the store. The store is the physical store of directory information located on a hard disk. Clients access the directory using one of the following mechanisms supported by the DSA:




  • LDAP clients connect to the DSA using the LDAP protocol. LDAP is an acronym for Lightweight Directory Access Protocol. Active Directory supports LDAP 3.0, defined by RFC 2251, and LDAP 2.0, defined by RFC 1777.


  • MAPI clients such as Microsoft Exchange connect to the DSA using the MAPI remote procedure call interface.


  • Windows clients that use a previous version of Windows NT connect to the DSA using the Security Account Manager (SAM) interface.


  • Active Directory DSA's connect to each other to perform replication using a proprietary remote procedure call interface.




Active Directory data model is derived from the X.500 data model. The directory holds objects that represent things of various sorts, described by attributes. The universe of objects that can be stored in the directory is defined in the schema. For each object class, the schema defines what attributes an instance of the class must have, what additional attributes it may have, and what object class can be a parent of the current object class.

Active Directory schema is implemented as a set of object class instances stored in the directory. This is very different than many directories that have a schema but store it as a text file read at startup. Storing the schema in the directory has many advantages. For example, user applications can read it to discover what objects and properties are available.

Active Directory can consist of many partitions or naming contexts. The distinguished name (DN) of an object includes enough information to locate a replica of the partition that holds the object. Many times however, the user or application does not know the DN of the target object or which partition might contain the object. The global catalog (GC) allows users and applications to find objects in an Active Directory domain tree, given one or more attributes of the target object. The global catalog contains a partial replica of every naming context in the directory. It contains the schema and configuration naming contexts as well. This means the GC holds a replica of every object in Active Directory but with only a small number of their attributes.

The global catalog is built automatically by Active Directory replication system. The replication topology for the global catalog is generated automatically. The properties replicated into the global catalog include a base set defined by Microsoft. Administrators can specify additional properties to meet the needs of their installation.

Interfaces for accessing the Active Directory.



  1. LDAP. The Lightweight Directory Access Protocol (LDAP) is a directory service protocol that runs on a layer above the TCP/IP stack, and provides a mechanism for connecting to, searching, and modifying Internet directories. The LDAP directory service is based on a client-server model. The function of LDAP is to allow access to an existing directory. The data model (data and namespace) of LDAP is similar to that of the X.500 OSI directory service, but with lower resource requirements due to its streamlined features. The associated LDAP API simplifies writing Internet directory service applications.


  2. ADSI. Active Directory Service Interfaces (ADSI) is a set of COM interfaces used to access the capabilities of directory services from different network providers in a distributed computing environment, to present a single set of directory service interfaces for managing network resources. Administrators and developers can use ADSI services to enumerate and manage the resources in a directory service, regardless of the network environment that contains the resource.


  3. System.DirectoryServices. System.DirectoryServices is a namespace in the .NET Framework that provides simple programming access to LDAP directories such as Active Directory. System.DirectoryServices is built on the Active Directory Service Interfaces (ADSI) API.

    Using System.DirectoryServices namespace.

    This article will emphasize in the benefits of using the namespace System.DirectoryServices, such as:



    • Designed completely within common language runtime parameters. System.DirectoryServices leverages common language runtime features, such as garbage collection, custom indexer, and dictionaries (hashtables). It also offers other common language runtime features such as automatic memory management, efficient deployment, an object-oriented framework, evidence-based security and exception handling.

    • Simple to use. Although ADSI scripting was effective for many tasks, C++ applications for ADSI are sometimes difficult to develop. System.DirectoryServices implements some basic ADSI tasks to enable more efficient and effective application development.




    System administrators can use System.DirectoryServices to automate tasks to manage network resources in the directory, such as users and computers and also to build applications that search, create, or modify objects in a directory.

    Requirements. System.DirectoryServices is supported on Windows Server 2003. System.DirectoryServices can be redistributed on Windows 98, Windows 98 SE and Windows NT 4.0, as long as the DS Client is installed on client machines. It can also be redistributed on Windows 2000 Windows XP.

    I developed a lot of business objects which access the Active Directory, leveraging any application which needs the platform as its main database and for publishing objects in enterprise network.

    It's defined the interface for the business objects which serve as changing or setting up the password for a specific user in the directory. Later this interface is implemented with a class, which instances make the real interaction with the directory.

    In listing 1, it's shown the contract IADPasswdManager and the class ADPasswdManager.

    Imports System

    Imports System.DirectoryServices

    Namespace OLAActiveDirectory.Management

    Public Interface IADPasswdManager

    Sub ChangePassword(ByVal objUser As IADUser, ByVal strOldPasswd As String, ByVal strNewPasswd As String)

    Sub SetPassword(ByVal objUser As IADUser, ByVal strPasswd As String)

    End Interface

    Public
    Class ADPasswdManager : Implements IADPasswdManager

    Public Sub New()

    End Sub

    Public
    Sub SetPassword(ByVal objUser As IADUser, ByVal strPasswd As String) Implements IADPasswdManager.SetPassword

    Dim objLoginEntry As DirectoryEntry=objUser.DirectoryEntry

    If Not objLoginEntry Is Nothing Then

    objLoginEntry.Invoke("SetPassword", New Object(){strPasswd})

    objLoginEntry.CommitChanges()

    End If

    End
    Sub

    Public
    Sub ChangePassword(ByVal objUser As IADUser, ByVal strOldPasswd As String, ByVal strNewPasswd As String) Implements IADPasswdManager.ChangePassword

    Dim objLoginEntry As DirectoryEntry=objUser.DirectoryEntry

    If Not objLoginEntry Is Nothing Then

    objLoginEntry.Invoke("ChangePassword",New Object(){strOldPasswd,strNewPasswd})

    objLoginEntry.CommitChanges()

    End If

    End
    Sub

    End
    Class

    End
    Namespace



    Listing 1.



    A business entity must be defined for the users of the directory. It has all the information of a particular user in the directory knowing its Distinguished Name (DN).



    It's defined an interface IADUser and the implementation is realized in the class ADUser as shown in the Listing 2.



    Imports System

    Imports System.DirectoryServices

    Imports System.Collections

    Namespace OLAActiveDirectory.Management

    Public Interface IADUser

    ReadOnly Property DirectoryEntry() As DirectoryEntry

    ReadOnly Property IsUser() As Boolean

    ReadOnly
    Default Property Item(ByVal strKey As String) As PropertyValueCollection

    End Interface

    Public Class ADUser : Implements IADUser

    Private ReadOnly m_objUserEntry As DirectoryEntry

    Public Sub New(ByVal strLogin As String, ByVal strRootPath As String)

    Dim objRootEntry As DirectoryEntry = New DirectoryEntry(strRootPath)

    Dim objADSearcher As DirectorySearcher = New DirectorySearcher(objRootEntry)

    objADSearcher.Filter="(&(objectClass=user)(anr=" & strLogin & "))"

    Dim objResult As SearchResult=objADSearcher.FindOne()

    If (Not objResult Is Nothing) Then

    Me.m_objUserEntry=objResult.GetDirectoryEntry()

    Else

    Me.m_objUserEntry=Nothing

    End
    If

    End
    Sub

    Public
    ReadOnly Property DirectoryEntry() As DirectoryEntry Implements IADUser.DirectoryEntry

    Get

    Return
    Me.m_objUserEntry

    End Get

    End
    Property

    Public
    ReadOnly Default Property Item(ByVal strKey As String) As PropertyValueCollection

    Get

    Return
    Me.m_objUserEntry.Properties(strKey)

    End Get

    End
    Property

    Public
    ReadOnly Property IsUser() As Boolean Implements IADUser.IsUser

    Get

    Return
    Not Me.m_objUserEntry Is Nothing

    End
    Get

    End
    Property

    End
    Class

    End
    Namespace



    Listing 2.



    In the Presentation Layer resides an instance of the class ADUserInfoShower whose role is to create an information string for a specific user. This object is independent of the technology used for showing the user information. That is, this string can be rendered in a Web Browser, a Windows Client and a Mobile Device. In the listing 3, it's shown the code for this business object.



    Imports System

    Namespace OLAActiveDirectory.Management

    Public Interface IADUserInfoShower

    Function GetInformation(ByVal objUser As IADUser, ByVal strSep As String) As String

    End
    Interface

    Public Class ADUserInfoShower : Implements IADUserInfoShower

    Private Function prvInfoBuilder(ByVal objUser As IADUser, ByVal strSep As String) As String

    Dim
    strResult As String

    strResult="Fullname:" & objUser("givenName").Value & " " & objUser("sn").Value

    strResult &= strSep & "Mail:" & objUser("mail").Value

    strResult &= strSep & "Telephone(s):" & objUser("telephoneNumber").Value

    For Each strPhone As String In objUser("otherTelephone")

    strResult &= strSep & strPhone

    Next strPhone

    Return strResult

    End Function

    Public
    Sub New()

    End Sub

    Public
    Function GetInformation(ByVal objUser As IADUser, ByVal strSep As String) As String Implements IADUserInfoShower.GetInformation

    Return Me.prvInfoBuilder(objUser,strSep)

    End Function

    End
    Class

    End
    Namespace





====================================================================
จากตัวอย่างในเรื่องการ สร้าง Authen กับ Active Directory นั้น ทำการ authen นั้นผ่าน protocal LDAP ได้ ซึ่ง ผมได้ทำตัวอย่างไว้ ทั้งแบบ VB6 และแบบของ .Net เอง สร้างเป็น Class DLL แล้วนำไปใช้ใน Project นะครับ ทีนี้ เราก็จะสามารถ ตัดปัญหายุ่งยากเรื่อง username และ password อันหลากหลายในระบบงานที่พัฒนาขึ้นมาได้แล้วนะครับ ก็จะเป็นการทำ ในเรื่อง single singon นั่นเอง


============ เริ่มจาก ของ VB6 ก่อนเลยนะครับ ============
สำหรับในตัว VB6 นั้นข้อดีของการสร้าง Active X control จาก vb6 ทำให้เราสามารถ นำ Dll นั้นไปใช้งานกับ Project ที่พัฒนาด้วย เทคโนโลยีตัวเก่า ได้นะครับ เช่นใช้งานกับ vba ของ access หรือ excel หรือใช้งานร่วมกับ ภาษาโปรแกรมตัวอื่นๆ ก็ได้ครับ หรือ จะเอาไปใช้กับเทคโนโลยีของ .net ด้วยก็ยังได้ เพราะ dll ของเทคโนโลยีตัวเก่า สามารถ ใช้งานกับตัวใหม่ได้ แต่ถ้าพัฒนาจาก .net จะไม่สามารถนำไปใช้กับ ระบบงานเก่าๆ ได้ครับ


ของเริ่มเรื่อง เลยนะครับ


เริ่มจาก เปิด visual studio 6 ขึ้นมา สร้าง Project Active X Dll นะครับ
จากนั้นให้ เปลี่ยนชื่อ class เป็นชื่ออะไรก็ได้ ในที่นี้ผม ใช้ชื่อ cssLoginAD





Option Explicit
Const ADS_SECURE_AUTHENTICATION = 1
' method สำหรับการตรวจสอบ Login AD
Public Function ValidateAuthenAD(ByVal UserName As String, _
ByVal Domain As String, _
ByVal Password As String) As Integer
Dim MyNamespace As IADsOpenDSObject
Dim X As IADsContainer
Dim oUserName As String
Dim oPassword As String
oUserName = UserName
oPassword = Password
' ตรวจสอบ login AD
On Error GoTo CleanUp
If InStr(1, UserName, ".", vbTextCompare) <> 0 And InStr(1, UserName, "@", vbTextCompare) = 0 Then
oUserName = UserName
End If
' กำหนด Potocal LDAP สำหรับการเข้าไปค้นหา
Set MyNamespace = GetObject("LDAP:")
Set X = MyNamespace.OpenDSObject("LDAP://" + Domain, oUserName, oPassword, ADS_SECURE_AUTHENTICATION)
ValidateAuthenAD = 0
GoTo FinFonction
CleanUp:
Select Case Err.Number
Case -2147023570 ' Return Error หาก password ไม่ถูกต้อง
ValidateAuthenAD = 1
Case -2147016646 ' return Error หาก Domain ไม่ถูกต้อง
ValidateAuthenAD = 2
Case Else ' return Error อื่นๆ
ValidateAuthenAD = 99
End Select
FinFonction:
Set MyNamespace = Nothing
Set X = Nothing
End Function



หลักการของโปรแกรม



Class ActiveX cssLoginAD ในเวอร์ชั่นนี้จะประกอบไปด้วย Method ให้เรียกใช้งานได้ดังต่อไปนี้


ValidateAuthenAD Method เป็นชนิด Integer การทำงานของ method จะเปิด Directory Service ขึ้นมา เพื่อเข้าไปตรวจสอบ user และ pass และ Domain controller โดยผ่าน โปรโตคอล LDap เพื่อ Search หา user และ pass ใน DC ว่ามีอยู่หรือไม่ โดยจะ คืนค่ากลับมา เป็น 0 คือ ตรวจหาพบ หรือ error อื่นๆ ดังนี้
0 = login ถูกต้อง
1 = password ไม่ถูก
2 = ไม่พบ Domain
99 = error อื่นๆ
parameter ที่จะส่งเข้าไปใน method นี้
1. UserName
2. Domain
3. Password





//จากนั้นให้ทำการ Complier dll ออกมาเท่านี้ก็จะสามารถใช้งาน Authen AD ได้แล้วครับ

//เมื่อนำ Dll นั้นไปใช้กับ Project 

// สร้าง ฟังชั่นขึ้นมาเพื่อตรวจสอบ กับ Class cssLoginAD
Private Function chkLogin(ByVal user As String, ByVal pass As String, ByVal dc As String) As Boolean
Dim result As Boolean
Dim AuthenAD As New cssLoginAD
Dim valReturn As Integer
result = False ' กำหนด ให้ ค่าเริ่มต้นเป็น False
If user <> "" And pass <> "" And dc <> "" Then
valReturn = AuthenAD.ValidateAuthenAD(user, dc, pass)
Select Case valReturn
Case 0 ' กรณี Login ถูกต้อง
result = True
Case 1 ' กรณี password ผิด
MsgBox "กรุณาระบุ password ให้ถูกต้องด้วย !!", vbOKOnly + vbCritical, "Login ผิด !!"
Case 2 ' กรณี ชื่อ Domain ผิด
MsgBox "กรุณาระบุ Domain ให้ถูกต้องด้วย !!", vbOKOnly + vbCritical, "Login ผิด !!"
Case Else ' error อื่นๆ
MsgBox "Error !!"
End Select
Else
' หากกรอกข้อมูลไม่ครับ
MsgBox "กรุณากรอกข้อมูลให้ครบถ้วนด้วย !!", vbOKOnly + vbCritical, "Validate Form!!"
End If
Set AuthenAD = Nothing
chkLogin = result
End Function
‘ ส่วนของ ปุ่ม Submit
Private Sub Command1_Click()
If chkLogin(txtuser.Text, txtpassword.Text, Combo1.Text) Then
MsgBox "Login ถูกต้อง ยินดีต้อนรับคุณ " & txtuser.Text
Call clearText
End If
End Sub

จากตัวอย่างด้านบนเป็นการใช้ Active X Dll ที่พัฒนาจาก vb6 สำหรับระบบงาน เก่าๆ หรือใช้งาน กับ vba กับ Access นะครับ
ทีนี้ เรามาดูพระเอกของบทความนี้กันดีกว่า สร้าง Authen Active Directory ด้วย .Net

สำหรับใน .Net นั้นนะครับ ได้เตรียม namespace ไว้ให้เราเรียบร้อย จะใช้  System.DirectoryServices
ทีนี้เรามาสร้าง Class สำหรับ authen กับ Acitve directory กันนะครับ

อันดับแรก ก็เปิด vs.net แล้ว สร้าง class ขึ้นมานะครับ ดังโคดด้านล่างนี้



 Imports System.DirectoryServices  ' impoart namespace สำหรับจัดการ directoryservice

Namespace LoginAD ' สร้าง namespace ชื่อ LoginAD เอาไว้จัดกลุ่ม class นะครับ
Public Class css_AuthenAD
' สร้าง private property
Private strUser As String
Private strPass As String
Private strDomain As String
Private authenType As AuthenticationTypes

' ===============================
' สร้าง Public method
' method สำหรับ Set Domain
Public Function SetDomain(ByVal strValue As String) As Boolean
If strValue.Length <= 0 Then Return False
Me.strDomain = "LDAP://" & strValue
Return True
End Function

' method สำหรับ Set User
Public Function SetUser(ByVal strValue As String) As Boolean
If strValue.Length <= 0 Then Return False
Me.strUser = strValue
Return True
End Function







' method สำหรับ Set password
Public Function SetPass(ByVal strValue As String) As Boolean
If strValue.Length <= 0 Then Return False
Me.strPass = strValue
Return True
End Function

Public Sub SetAuthenType(Optional ByVal bValue As Boolean = False)
If bValue Then
' type สำหรับ edirectory
authenType = AuthenticationTypes.SecureSocketsLayer
Else
' type สำหรับ Active Directory
authenType = AuthenticationTypes.Secure
End If
End Sub

' และ method login สำหรับเอาไว้ authen นะครับ
Public Function Login() As Boolean
Dim result As Boolean = False
Dim DirEntry As New DirectoryEntry(Me.strDomain, _
Me.strUser, _
Me.strPass, _
Me.authenType)
Try
Dim native As Object = DirEntry.NativeObject()
result = True
Catch ex As Exception
result = False
End Try
Return result
End Function
' ===============================
End Class
End Namespace