ASP
  Home arrow ASP arrow Page 4 - Apply Single-Sign-On to Your Application
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

Apply Single-Sign-On to Your Application
By: Softwaremaker
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 90
    2003-12-23

    Table of Contents:
  • Apply Single-Sign-On to Your Application
  • Sign-On: LDAP as the Key
  • What are the Pitfalls?
  • Two Solutions
  • Conclusion

  • 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


    Apply Single-Sign-On to Your Application - Two Solutions


    (Page 4 of 5 )

    Two Solutions

    Therefore, the best for now would be a hybrid of both scenarios, taking the pros and cons of both. Have an application-architecture with a single user repository at the organization's LDAP Directory, but letting users sign on again for added security with the same user-credentials. This may solve the problem at hand. Still, the ideal can be achieved. Imagine your client email application asking you for your password each time its goes out to receive or send your emails.

    Microsoft .NET has made it easy to do both with just some switches of commands in a configuration file. Therefore, it doesn’t take much to deploy either solution depending on client needs.

    I will dive into some VB.NET Coding to explain how both can be achieved.

    Scenario A : True-Blue Single-Sign-On

    1. At the launch of the application, query LDAP and see if the user is in the proper user-groups to access the application. I am, of course, assuming that groups are assigned to applications, and not individuals. [Note: ' / ' indicates that the syntax on the next line is part of the previous - Editor]

       
      Private 
      Sub 
      Page_Load(ByVal sender As System.Object

      ByVal e As System.EventArgs
      Handles MyBase
      .Load 
      Dim p 
      As 
      WindowsPrincipal Thread.CurrentPrincipal 
      Dim a 
      As New LDAPQuery 
      Dim 
      objCook 
      As New HttpCookie("LDAPCookie"
      'Check for authentication and write 
      into Cookie 
      Dim sLDAPCN As String = (a.GetCNByLDAP(p.Identity.Name)) 

      '
      Check for LDAP Authentication First 
      If sLDAPCN 
      <> "" And 
      (a.blnFindCNinGroups(a.GetCNByLDAP(p.Identity.Name), 
      "AppAUserGroups")) 

      Then objCook
      .Value = (sLDAPCNResponse.Cookies.Add(objCook
      'Check for 
      LDAP User Existence in Application Database 
      '
      If User exists in Application 
      DB
      Start Application 
      If 
      (FindLDAPCNinDB(sLDAPCN)) 
      Then 
      'DROP 
      Authentication Cookie token 
      '
      Compute a hashed value of the current session 
      ID 
      'into a cookie to signal authentication success 
      '
      Be sure to check for 
      this cookie for every page load 
      'if value doesnt match or cookie doesnt 
      exists, 
      '
      Re-do the whole authentication process again
      'Single-sign-on 
      should continue, the cookies drop 
      '
      prevent authenticated LDAP Users from not 
      having 
      'their CN mapped with other application-specific 
      '
      fields or data
      Response.Redirect("Welcome.aspx") Else 
      'If False, do Database mapping 

      Response.Redirect("CollectOtherInfo.aspx") 
      End If Else 
      Response.Redirect("LoginError.htm") 
      End If 
      End Sub 
      <LI>Private 
      Function FindLDAPCNinDB(ByVal sLDAPCN 
      / As String) As 
      Boolean 
      '
      For ease of setupI am using a Flat XML File as a database 

      'However, a proper way would be to use a relational database 
      '
      You may 
      use the ADO.NET to query and get 
      information from the databse 
      Dim 
      xmldoc 
      As New XmlDocument 
      Dim xmlnode 
      As XmlNode 
      Dim xmlnodelist 
      As 
      XmlNodeList xmldoc.Load("D:WebDeployLDAPQueryFlatDB.xml"
      xmlnodelist 

      xmldoc.SelectNodes("/Users/User"
      For Each xmlnode In xmlnodelist 
      'Find 
      LDAPCN 
      If xmlnode.ChildNodes.Item(0).InnerText = sLDAPCN 
      Then Return 
      True 
      End If 
      Next 
      Return False 
      End Function 
      '
      ########### 

      '########### 
      Imports System.DirectoryServices 
      Public Class LDAPQuery 

      '
      This function returns the Common Name (CN
      'of the Login information of 
      the 
      '
      LDAP DirectoryThe CN is an unique identity 
      / and cannot be 
      changed 
      or edited in LDAP 
      Public 
      Function GetCNByLDAP
      (ByVal strLogin 

      / As String) As String 
      Dim str 
      As String "" 
      'Parse the string to 
      check if domain name is present. 
      Dim idx As Integer = strLogin.IndexOf("") 

      If (idx = -1) Then idx = strLogin.IndexOf("@") 
      End If 
      Dim strDomain 
      As String 
      Dim strName As String 
      If (idx <> -1) 
      Then strDomain 
      = strLogin.Substring(0, idx) 
      strName = strLogin.Substring(idx + 1) 
      Else 
      strDomain = Environment.MachineName 
      strName = strLogin 
      End If 
      Dim 
      obDirEntry As DirectoryEntry = Nothing 
      Try 
      Dim strPath As String = 

      "<A href="ldap://DC=Softwaremaker,DC=net"> LDAP://DC=Softwaremaker,DC=net </A>" 

      obDirEntry = New DirectoryEntry(strPath) 
      Dim rootSearch As New / 
      DirectorySearcher(obDirEntry) 
      Dim SearchResult As SearchResult 
      Dim spn 
      As String = strName & "@" 
      & strDomain rootSearch.Filter = 
      ("(&(objectCategory=user) 
      / (userPrincipalName=" & spn & "))") 

      For Each SearchResult In rootSearch.FindAll / 
      '
      Or FindOne 
      Dim i 
      As 
      Integer 
      'Check here - Should only return ONE result 
      For i = 0 To 
      SearchResult.Properties("cn").Count - 1 str += SearchResult.Properties("cn")(i) 

      Next 
      Next Catch ex As Exception str = ex.Message 
      End Try 
      Return 
      str 
      End Function 
      '
      Function finds and returns if User if in the specified 
      user group 
      Public 
      Function blnFindCNinGroups(ByVal sLogonUserCN 
      / As 
      StringByVal sGroup As String) As Boolean 
      Try 
      Dim sDirEnt 
      As String 
      "<A href="ldap://server/CN">LDAP://server/CN</A>= " & 

      sLogonUserCN&",CN=Users,DC=Softwaremaker,DC=net" 
      Dim user 
      As 
      DirectoryEntry = New DirectoryEntry(sDirEnt
      Dim pcoll 
      As PropertyCollection 
      user.Properties 
      Dim i 
      As Integer Dim s As String 
      'The loop will return 
      all Groups of which the CN is a member Of 
      For i = 0 To 
      pcoll("memberOf").Count - 1 s = pcoll("memberOf")(i).ToString 
      If 
      QueryLDAP(s, sGroup) = True 
      Then Return True 
      Next Return False 
      Catch 
      ex As Exception 
      Return False 
      End Try 
      End Function 
      Public 
      Function 
      QueryLDAP(ByVal strQuery As String, / ByVal sGroup As String) 

      As Boolean Dim obDirEntry As DirectoryEntry = Nothing 
      Try 
      Dim 
      strPath As String = "LDAP://" & strQuery 
      Dim s As String obDirEntry = 
      New DirectoryEntry(strPath) 
      Dim rootSearch As New 
      DirectorySearcher(obDirEntry) 
      Dim SearchResult As SearchResult 
      For Each 
      SearchResult In rootSearch.FindAll 
      Dim i As Integer '
      Check here Should 
      only 
      return ONE result 
      For i 
      0 To SearchResult.Properties("cn").Count 
      += SearchResult.Properties("cn")(i
      Next 
      If s 
      sGroup 
      Then 
      Return 
      True 
      Next 
      Catch ex As Exception Return False 
      End 
      Try 
      End 
      Function 

      End 
      Class 
      '########### 

       

    2. I am by no means an LDAP Query guru or expert. I am sure I am taking one step too many just to query whether the user exists within the specified user group in the LDAP Directory. However, I am selling the idea, not the code. Hopefully you get the idea.

    3. Several things that can be applied include:
      • Using Windows Authentication in the IIS Settings

      • At the web.config file level...

         '########### 
        <IDENTITY 
        impersonate="true" />
        <AUTHENTICATION mode="Windows" /><AUTHORIZATION>
        <ALLOW 
        roles="SoftwaremakerNetAppAUserGroup" />
        </AUTHORIZATION>
        '
        ########### 


    Scenario B : Hybrid Forms Authentication with LDAP

    (*Please see http://support.microsoft.com/default.aspx?scid=kb;EN-US;326340 for a detailed working version.)

    More ASP Articles
    More By Softwaremaker


     

    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
    Stay green...Green IT