ASP
  Home arrow ASP arrow Performing Web Authentication and Administ...
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ASP

Performing Web Authentication and Administration with LDAP
By: aspfree
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 14
    2002-06-18

    Table of Contents:

    Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Chapter 1: The Basic Login Chapter 2: Which Directory? (Root DSE Searches) Chapter 3:Add New User Chapter 4: General MaintenanceProviding a login interface for a website, as well as an administrator interface formaintaining a user dat ...

    • Chapter 1: The Basic Login
    • Chapter 2: Which Directory? (Root DSE Searches)
    • Chapter 3: Add New User
    • Chapter 4: General Maintenance

        Providing a login interface for a website, as well as an administrator interface for maintaining a user database is an important feature for any serious website. I'll demonstrate in this text how easy it is to do this.

        An easy and convenient way to deal with all of this information is with an LDAP server. There are many benefits of using an LDAP Directory over a database, but I won't get into that for now. In this document I will cover how to authenticate a user/password pair in ASP with your LDAP server, how to determine which directory a user is in (if your Directory Server contains more than one directory), how to add new users, as well as how to handle general maintenance of the users (additions, deletions, and modifications).

        In order to accomplish all of this I'm going to use the IP*Works! LDAP component from /n software. For this document, I'm going to specifically use the ASP Edition of these components. There are many different editions to choose from - ie ASP.Net which would provide an event driven asynchronous interface.

        Chapters

      • The Basic Login
      • Which Directory? (Root DSE Searches)
      • Add New User
      • General Maintenance
      • More Information

        
        

        
        

        Chapter 1.
        The basic login

        First things first - we need a login form for our users! Here's a nice simple one:

        
        
        
        
        User Name:
        Password :

        Now, when the user clicks on the "Login" button, the form will submit to itself. We'll add some asp code so that if a post is detected, the authentication of the user will take place. First, we'll create the LDAP object (set ldap = Server.CreateObject("IPWorksASP.LDAP")). After that we'll perform a search for a UID that matches the loginname of the user. To do that, we'll need to point the LDAP object to the LDAP server, provide a base DN on which to perform the search (see DSE Information for more details), and set our search filter to search for this particular UID.

        
        
        If Request("REQUEST_METHOD") = "POST" then
        'create the ldap object
        dim ldap
        set ldap = Server.CreateObject("IPWorksASP.LDAP")
        dim found 'flag variable for whether or not we

        find the loginname on the server ldap.Servername = "Server" ldap.DN = "ou=People,dc=Server" ldap.SearchFilter = "uid=" + Request("loginname") found = false ldap.Search

        Now, the NextResult property of the LDAP component will attempt to read a response from the server. Be sure to set a timeout first so that if no response is available, you won't waste time expecting one. The value of the NextResult property will be 1 for search results and 0 for other responses (ie a response to an Add, Delete, or Modify method). In this case we are obviously expecting a search result, so we'll wait until the NextResult property is 1.

        
        
        while ldap.NextResult = 1
        found = true 'set found to true if we find any 
        positive search results wend

        The above loop will allow you to set the found flag after a successful search result is obtained, signaling us that we have indeed found this UID in the directory. Now if we have found a possible match, we can see if the password he provided on the html form will successfully authenticate to the directory.

        
        
        if found=true then
        'try to authenicate
        ldap.DN = "uid=" + Request("loginname") + ", ou=People, dc=Server"
        ldap.password = Request("password")
        ldap.Bind
        if ldap.NextResult = 0 then 
        
        'NextResult will be zero if the server sends a bind result 'the result in this case will be either "OK", bound successfully 'or "Invalid Credentials", meaning the password does not match.
        
        
         if ldap.ResultDescription = "[ok]" then
        response.write "Success!  You have been validated."
        else 'login result was not "OK"
        response.write "Invalid Login."
        end if
        end if
        else 'found flag was false, we did not find the user in the directory
        response.write "Invalid Login."
        end if
        end if 'end for the if requestmethod=post statement
        
        
        

        
        

        Chapter 2.
        Which Directory? (Root DSE Searches)

        Its possible for an LDAP server to contain more than one directory. For example, a directory for customer/client contacts, and a directory for employees. So if you want to have the same login interface for both of these groups of people, you might need a way to determine exactly which base DN's to perform your search against. Each directory on the server will have a unique base DN. For example, on our server, the customer/client contacts directory base DN is:

        
        
        ou=People, dc=Server
        

        The employee directory has a base DN of:

        
        
        ou=Employees, dc=Server
        

        If you don't know what these are - how can you determine them programmatically? This is one of the things that can be resolved by the root DSE search. DSE information is directory specific entry info. This is information that all LDAP servers will provide so that clients can have access to attributes of the server itself. Some of this information can be quite useful. For one example - each LDAP server can actually contain several different directories. One of the DSE Attributes is called "namingContexts", and this attribute is a list of base DN's (one for each directory) that you can access on this particular server. DSE Information will also tell you which versions of the LDAP protocol the server can understand.

        A DSE search requires several attributes:

        
        
        Blank DN
        Search Filter of "objectClass=*"
        Search Scope of "Base"
        

        These properties can be easily assigned with the LDAP component, like so:

        
        
        LDAP1.ServerName = SERVERNAME
        LDAP1.DN = ""
        LDAP1.SearchFilter = "objectClass=*"
        LDAP1.SearchScope = ssBaseObject
        LDAP1.Search
        
        We'll use a similar method for obtaining the result from the server. First we'll wait for the NextResult property to = 1, signaling that the server is finished and has a search result.
        
        
        While ldap.NextResult = 1
        Response.Write("<b>Performed Root DSE Search:</b><br>")
        Wend
        
        Now the search result of a DSE search will not be like others - where you are searching for a particular DN. The only thing returned by this search will be attributes of the server, and these will arrive in the attribute property arrays of the LDAP component. Specifically, the ldap.AttrType() array will contain the type of each response attribute. The ldap.AttrValue() array will contain the corresponding values. AttrCount is the total number of attributes returned by the server. In this case, we are only interested in the namingContexts attributes, so that we can see exactly which base DN's we have on this server. Lets pick these out and write them.
        
        
        Dim foundnamingcontexts
        foundnamingcontexts = false
        For i = 0 To LDAP.AttrCount - 1
        'this line prints out ALL attributes
        'Response.Write("ATTR " + ldap.AttrType(i) + " = " + ldap.AttrValue(i) + "<br>")
        If ldap.AttrType(i) = "namingContexts" Then 
        foundnamingcontexts = true
        Response.Write("namingContexts: " & ldap.AttrValue(i) & "<br>")
        mybaseDN = ldap.AttrValue(i)
        Elseif ldap.AttrType(i) = "" And foundnamingcontexts = true Then
        Response.Write("namingContexts: " & ldap.AttrValue(i) & "<br>")
        Else
        foundnamingcontexts = false
        End if
        Next
        

        This for loop becomes a little more complicated than one might imagine. The first instance of the namingContexts type in the attribute arrays is of type "namingContexts". But for subsequent attributes of the same type, which arrive one after the other, the server doesn't specify that type, but just leaves the type as empty string. In other words, in order, the server sent attributes like so:

        
        
        Type: sometype      , Value: sometype value
        Type: namingContexts, Value: dc=siroe, dc=com
        Type:               , Value: dc=Server                
        Type:               , Value: dc=Netscape, dc=com
        Type: someotherType , Value: someothertype value
        Type:               , Value: someothertype value 
        
        So "dc=siroe, dc=com", "dc=Server", and "dc=Netscape, dc=com" are all namingContexts attributes, even though the second two have empty string as the type.

        In the demo with this document, I'm just going to pick the first base DN here. You can include code on your own to either hard code this differently, or even allow users to select which "group" they belong to. For example: an option box specifying whether they are a customer, employee, or what. Also, you could even try their login ID against all of them.

        
        

        
        

        Chapter 3.
        Add New User

        Ok, so now we've got a working login page for our website. But what happens when someone new drops by and wants to join or create a login? You need to programmatically add them to the LDAP server so that they can authenticate themselves. As it turns out, this is quite easy.

        The information that we'll need from the user is of course a UID (loginname) and a password. Just for demonstration, I'll also set a description attribute for the user. If you want other information - go for it. But keep in mind that the LDAP server allows only specific attribute types, and you'll need to stick with those. This shouldn't be a problem because there are tons defined: address, phone number, description, and many others for you to use. See your server documentation for a full list.

        
        
        
        
        User Name:
        Password :
        Confirm :

        When the user submits this information, we'll need to setup the DN and attribute arrays for this new LDAP entry first.

        Before you set the DN - you need to know what base DN to add this person to. This won't be the same base DN that you perform the search on (most of the time). For example, a common base DN for the search might have been simply "dc=Server". Most of the times, the directory "Server" will have a child named "People". So the base DN for adding an entry would be "ou=People, dc=Server". If you are unsure about this DN, please consult your server documentation, or browse the directory until you find the tree where you want to add the entry and find its DN. Once you have the DN (ie "ou=People, dc=Server") we'll want to modify it to create our new users dn. To do this, we just add their UID to the beginning of it. If our users loginname is "SJenkins", you can set the DN = "uid=SJenkins, ou=People, dc=Server".

        
        
        baseDN = "ou=People, dc=Server"
        ldap.DN = "uid=" + Request("loginname") + ", " + baseDN
        

        Every LDAP entry is required to have a set of "objectClass" type attributes. For a person, these are:

        
        
        type = objectClass, value = top
        type = objectClass, value = Person
        type = objectClass, value = organizationalPerson
        type = objectClass, value = inetorgperson
        
        So before we add this new DN to the server, we'll need to set its attributes. We do this using the ldap.AttrType() and AttrValue() arrays. Below we'll simply set a description, the UID and userPassword.
        
        
        ldap.AttrCount = 7
        ldap.AttrType(0) = "objectClass"
        ldap.AttrValue(0) = "top"
        ldap.AttrType(1) = "objectClass"
        ldap.AttrValue(1) = "Person"
        ldap.AttrType(2) = "objectClass"
        ldap.AttrValue(2) = "organizationalPerson"
        ldap.AttrType(3) = "objectClass"
        ldap.AttrValue(3) = "inetorgperson"
        ldap.AttrType(4) = "description"
        ldap.AttrValue(4) = "New Account"
        ldap.AttrType(5) = "uid"
        ldap.AttrValue(5) = Request("loginname")
        ldap.AttrType(6) = "userPassword"
        ldap.AttrValue(6) = Request("password")
        

        Voila! Now we have all of this new users attributes set up including his uid and password. We have his DN set. Now all thats left to do is Add him to the server. Thats the easiest part!

        
        
        ldap.Add
        
        SJenkins will now be able to login with his password via the method outlined in chapter 1.
        
        

        
        

        Chapter 4.
        General Maintenance

        Any administrator needs to have a method of manually adding, deleting, or modifying user accounts under their control. By using LDAP as an authentication and directory tool, you can of course perform general maintenance on these accounts by hand, in person, on the actual server itself. However, with this LDAP component, this could also be done via the same website. This would allow website administration to take place remotely.

        As far as adding new accounts - we'll - we've already covered that. Deleting and modifying accounts are equally as simple.

        In order to delete an account - all you need to do is set the DN for that account and use the Delete method.

        
        
        ldap.DN = "uid=SJenkins, ou=People, dc=Server"
        ldap.Delete
        

        Couldn't get much easier than that. But what if you don't want to delete the account - you just want to deactivate it. The purpose being you want to still have a record that this account exists, but you don't want the user to have login access any longer. To do this, you could use an attribute that specified account status. When the user attempts to login, check this attribute to verify that the user has login rights. You might use the description attribute for this. When we added SJenkins to our directory, we gave his description type attribute the value of "New Account". Now if we want to suspend this account, we could change this description attribute to "Suspended".

        To do this we'll need to modify the existing attribute. The LDAP component includes a modify method for doing this. The modify method can perform different kinds of attribute modifications: Add attribute, Delete attribute, and Replace attribute. Since the description attribute already exists, we'll use the Replace choice. This is defined in the AttrModOp() array.

        Here, we just have 1 attribute that we want to replace. So we'll set the AttrCount to 1, the zeroth (1st) element of the AttrType() array to the type we are looking for (description), the zeroth element of the AttrValue() array to the new value for this attribute, and the zeroth element of the AttrModOp() array to 2 (replace). Then just call the Modify method.

        
        
        ldap.AttrCount = 1
        ldap.AttrType(0) = "description"
        ldap.AttrValue(0) = "Suspended"
        ldap.AttrModOp(0) = 2 'Replace
        ldap.Modify
        
        After this, if we examine the description attribute for SJenkins, it will have a value of "Suspended" instead of "New Account". If we wanted to delete/add the attribute type description of the value "Suspended", we would use the same method, except we would set AttrModOp() to 0/1 (Delete/Add) instead of 2 (Replace).
        
        

        
        

        More Information

        For information about the author, please contact lancer@nsoftware.com. For more information about IP*Works! or the LDAP component, please visit /n software.

        Copyright © 2002, Lance Robinson - All Rights Reserved.  For publishing permissions, contact lancer@nsoftware.com.


        DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.

        More ASP Articles
        More By aspfree

         

        IBM® developerWorks developerWorks - FREE Tools!


        IBM DB2 Deep Compression ROI Tool

        The IBM DB2 Deep Compression ROI tool is designed for DBA’s and IT management personnel to perform a clinical analysis of the cost savings gained from the Storage Optimization feature of DB2 9 for Linux, UNIX and Windows. The feature, also known as Deep Compression, compresses data that lies within a database by up to 80% at times.
        FREE! Go There Now!


        NEW! IBM – Taking Web 2.0 to Work

        David Barnes, Lead Evangelist for IBM Emerging Internet Technologies will discuss aspects of Web 2.0 that bring value to corporations, academia, and government. He'll also discuss IBM's vision around Web 2.0, including the importance of remixability and consumability. The discussion will culminate with examples of various IBM Software Group solutions you can use to get ahead of the Web 2.0 adoption curve.
        FREE! Go There Now!


        NEW! Achieving True Agility -- How process can change the behavior of your tools

        Achieving true agility is a never-ending effort. We will showcase how you can become agile incrementally, a few practices at the time.Which practices should any agile team strive to adopt? What additional practices should you consider based on your needs to scale? Adopting practices are however made much easier with the right tool support. What about if your tools adapt to your practices? We will take a look at how the Jazz technology can be leveraged to make your process change the behavior of your tools.
        FREE! Go There Now!


        NEW! Develop Systems Software Assets with IBM Rational Asset Manager

        Join us for this on demand webcast to learn about developing complex systems more quickly and efficiently. We'll cover market drivers for developing, governing and reusing systems software assets and how you can develop system software assets with Rational Asset Manager.
        FREE! Go There Now!


        NEW! Download a free trial of WebSphere Business Modeler Advanced V6.1.1

        Visit IBM developerWorks to download a free trial version of WebSphere Business Modeler Advanced V6.1.1, IBM’s premier business process modeling and analysis tool for business users that offers process modeling, simulation, and analysis capabilities. IBM WebSphere Business Modeler helps you visualize, understand, and document business processes for continuous improvement.
        FREE! Go There Now!


        NEW! Evaluate WebSphere Extended Deployment Compute Grid V6.1

        Visit IBM developerWorks to download a free trial version of WebSphere Extended Deployment Compute Grid, which lets you schedule, execute, and monitor batch jobs. Because online transaction processing and batch jobs execute simultaneously on the same server resources, you can avoid costly duplication of resources. Compute Grid supports job types of Java transactional batch, compute-intensive and a new type called "native execution", which enables non-Java workloads to run on distributed end points.
        FREE! Go There Now!


        NEW! Rational Talks to You: Manage RUP-based CMMI initiatives

        Join this Rational Talks to You teleconference on December 4 at 1:00 pm ET to discuss how Rational Method Composer can help meet your compliance objectives. Get your questions answered!
        FREE! Go There Now!


        NEW! The role of integrated requirements management in software delivery

        This paper is about the critical role that a discipline called integrated require­ments management can play in helping to ensure that your business goals and IT investments are continuously aligned—whether you are sourcing, integrat­ing, building or maintaining software. It also looks at ways that automated IBM Rational® products can work together to help you use requirements in the very best way.
        FREE! Go There Now!


        NEW! Trial download: IBM Rational Performance Tester V7.0.1

        Get a free trial download of the latest version of IBM Rational Performance Tester V7.0.1, a load and performance testing solution for teams concerned about the scalability of their Web-based applications. Combining multiple ease-of-use features with granular detail, Rational Performance Tester simplifies the test-creation, load-generation and data-collection processes that help teams ensure the ability of their applications to accommodate required user loads.
        FREE! Go There Now!


        NEW! Understanding Web application security challenges

        As businesses grow increasingly dependent upon Web applications, these complex entities grow more difficult to secure. Most companies equip their Web sites with firewalls, Secure Sockets Layer (SSL), and network and host security, but the majority of attacks are on applications themselves – and these technologies cannot prevent them. This paper explains what you can do to help protect your organization, and it discusses an approach for improving your organization’s Web application security.
        FREE! Go There Now!



        All FREE IBM® developerWorks Tools!

        ASP ARTICLES

        - Using MySQL with ASP
        - ADO for the Beginner
        - ADO.NET 101: Data Rendering with a DataGrid ...
        - Introducing SoftArtisans OfficeWriter 3.0 En...
        - Getting Remote Files With ASP
        - The Real Basics of Functions in ASP
        - Enhancing Readability with ASP
        - Mimicking PHP's String Formatting Functions
        - Windows Server Hacks 12, 77, and 98
        - How to Sort a Multi-Dimensional Array
        - Developing an Information Management Tool wi...
        - What are Active Server Pages?
        - Getting Remote Pages with ASP
        - FTP’ing Files with ASP
        - Apply Single-Sign-On to Your Application





        © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
        For more Enterprise Application Development news, visit eWeek