I'm trying to write an ASP page that will authenicate the user against Active Directory. The page also needs to check the groups that the user is a member of. If the user is not a member of a certain group, they will not be given access to the web application. However, if they are a member of that group they will be redirected to the web application. I have pieced this code together from many sources. Right now it will authenticate the individual against Active Directory, but I cannot get the group portion of the code to function. It will login users that are not a member of the desired group. Any help would be greatly appreciated!
Here is the code:
<%
Option Explicit
response.buffer = true
dim strUsername,strpassword,domainname
dim objDomain,objADsPath,objConnection,objCommand,objRS
strUsername=Replace(Request.Form("txtUserLogin"), "'", "''")
strpassword=Replace(Request.Form("txtUserPassword"), "'", "''")
on error resume next
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set objConnection = Server.CreateObject("ADODB.Connection")
objConnection.provider ="ADsDSOObject"
objConnection.Properties("User ID") = "mydomain\"+strUsername
objConnection.Properties("Password") = strpassword
objConnection.open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText ="select cn FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set objRS = objCommand.Execute
'HandleError
If Err.Number <> 0 Then
session("logged_in") <> "true"
Response.Redirect("index.asp")
End If
'Handle navigation if no connection errors arise
If objRS.RecordCount > 0 Then
If (CheckUserGroups(strUsername)=True) Then
session("logged_in") = "true"
Response.Redirect("openings.asp")
Else
session("logged_in") <> "true"
Response.Redirect("index.asp")
End If
Else
session("logged_in") <> "true"
Response.Redirect("index.asp")
End If
objRS.Close
objConnection.Close
Set objRS = Nothing
Set objConnection = Nothing
Sub CheckUserGroups(uid)
dim arrMemberOf
'On Error Resume Next
Set objUser = GetObject("LDAP://" & uid & "") ' LDAP for User Info
With objUser
arrMemberOf = .GetEx("memberOf")
If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
strGroupList = "The memberOf attribute is not set."
Else
For each Group in arrMemberOf
If Group = "mygroup" Then
Return True
End If
'Group = Mid(Group,4)
'intLeft = Instr(Group,",")
'Group = Left(Group, intLeft) & " "
'strGroupList = strGroupList + Group
Next 'arrMemberOf
End If
End With ' objUser
End Sub
%>
Here is the code:
<%
Option Explicit
response.buffer = true
dim strUsername,strpassword,domainname
dim objDomain,objADsPath,objConnection,objCommand,objRS
strUsername=Replace(Request.Form("txtUserLogin"), "'", "''")
strpassword=Replace(Request.Form("txtUserPassword"), "'", "''")
on error resume next
Set objDomain = GetObject ("GC://rootDSE")
objADsPath = objDomain.Get("defaultNamingContext")
Set objDomain = Nothing
Set objConnection = Server.CreateObject("ADODB.Connection")
objConnection.provider ="ADsDSOObject"
objConnection.Properties("User ID") = "mydomain\"+strUsername
objConnection.Properties("Password") = strpassword
objConnection.open "Active Directory Provider"
Set objCommand = CreateObject("ADODB.Command")
Set objCommand.ActiveConnection = objConnection
objCommand.CommandText ="select cn FROM 'GC://"+objADsPath+"' where sAMAccountname='"+strUsername+"'"
Set objRS = objCommand.Execute
'HandleError
If Err.Number <> 0 Then
session("logged_in") <> "true"
Response.Redirect("index.asp")
End If
'Handle navigation if no connection errors arise
If objRS.RecordCount > 0 Then
If (CheckUserGroups(strUsername)=True) Then
session("logged_in") = "true"
Response.Redirect("openings.asp")
Else
session("logged_in") <> "true"
Response.Redirect("index.asp")
End If
Else
session("logged_in") <> "true"
Response.Redirect("index.asp")
End If
objRS.Close
objConnection.Close
Set objRS = Nothing
Set objConnection = Nothing
Sub CheckUserGroups(uid)
dim arrMemberOf
'On Error Resume Next
Set objUser = GetObject("LDAP://" & uid & "") ' LDAP for User Info
With objUser
arrMemberOf = .GetEx("memberOf")
If Err.Number = E_ADS_PROPERTY_NOT_FOUND Then
strGroupList = "The memberOf attribute is not set."
Else
For each Group in arrMemberOf
If Group = "mygroup" Then
Return True
End If
'Group = Mid(Group,4)
'intLeft = Instr(Group,",")
'Group = Left(Group, intLeft) & " "
'strGroupList = strGroupList + Group
Next 'arrMemberOf
End If
End With ' objUser
End Sub
%>