Monday, January 4, 2010

Integration with active directory..

Yesterday I had a task to integrate a web application with the active directory of the company.

On the load of the login page a check has to be done to insure that:
  • the user is logged to the right domain. (compare the user current domain with the company's domain).
  • the user has an account in application.
if so the login page will be skipped and the user will log in automatically to the application, otherwise the user will stay in the login page.


so here are the steps i followed to solve the problem

1. Set the authentication mode in the web.config to "Windows"

2. On the load of the page, add the call of the following function:



Sub CheckUserOnActiveDirectory()



If Application.Item("CheckOnActiveDirectory") Then


If User.Identity.IsAuthenticated Then


Dim strFulName As String

Dim strUsername As String = String.Empty

Dim strDomainName As String = String.Empty

Dim intUserID As Integer = 0



strFulName = Page.User.Identity.Name 'This will rturn Domain\User


strUsername = strFulName.Substring(strFulName.IndexOf("\") + 1, strFulName.Length - strFulName.IndexOf("\") - 1)


strDomainName = strFulName.Substring(0, strFulName.IndexOf("\"))




' Add some code to take the user name and check if its available on the database, and return any information needed.




Me.Session("UserID") = intUserID

Me.Session("UserName") = strUserName

Me.Session("UserRole") = intUserRole

Me.Session("UserStatus") = intUserStatus

Me.Session("EmployeeID") = intEmplyeeId


System.Web.Security.FormsAuthentication.RedirectFromLoginPage(strUserName, False)


End If

End If

End If

End Sub



and we done :)