Hey all,
Working on revamping our Intranet here and making use of the LDPA, Active
Directory, Directory Services, etc. that .Net provides. I am still fairly
new on this subject, so the problem I have run into I am not sure how to
fix, and really not sure what is causing it.
Here's what is going on (test server - Windows 2003 Server):
I have a page in a folder (under anonymous authentication in IIS6) that has
a link on it that redirects the user to a page in a folder that is set to
use Integrated Windows Authentication but not anonymous. When redirected,
the IIS security setting forces the user to log in. Now, in the code of
this "login page" I get the user name via
"Context.User.I dentity.Name.To String" and store that to a Session Variable.
I found a bit of code that queries the LDAP server for the full name of a
user
(http://www.411asp.net/func/content?t.../usermana&id=3
972310) which I have put into a class file. After storing the user name, I
set the properties and query the LDAP server, using the function of that
class file, for the user's Full Name. I then save the full name to a
Session Variable as well. The last thing the page does is redirect back to
the starting page. Back on the starting page I then display the contents of
the user and full name Session Variables.
The point here is so I can keep my pages set to Anonymous under II6, but if
I can "authentica te" the visitor, then I can have the code-behind page "turn
on" extra features and what not. Other wise, the page comes up in normal
mode.
My problem...
This all works just fine as long as I am viewing the page from a browser on
the test box. When I fire up a browser on my machine and navigate to the
page on the test box, it errors out. The error is occurring on the second
page described above. I save the error message to a Session Variable and
display that variable's contents back on the first page if an error
occurred. This is the error I get:
System.Runtime. InteropServices .COMException (0x80072020): An operations
error occurred
at System.Director yServices.Direc toryEntry.Bind( Boolean throwIfFail)
at System.Director yServices.Direc toryEntry.Bind( )
at System.Director yServices.Direc toryEntry.get_A dsObject()
at System.Director yServices.Direc torySearcher.Fi ndAll(Boolean
findMoreThanOne )
at System.Director yServices.Direc torySearcher.Fi ndAll()
at WinAuth.Auth.ad si.GetUserProps () in
c:\inetpub\wwwr oot\WinAuth\ads i.vb:line 66
at WinAuth.index.P age_Load(Object sender, EventArgs e) in
c:\inetpub\wwwr oot\WinAuth\log in\index.aspx.v b:line 35
I need help here understanding what is going on and why this is happening.
I am thinking it is a permissions issue...in that when I am on the test box,
I can run stuff, but when viewing from a remote box I can't. Or could it be
trying to query the LDAP box from a remote machine? I am at a bit of a loss
here.
Your help is greatly appreciated.
-- Andrew
[Code Segment]
** First Page HTML **
----------------------------------------------------------
<%@ Page Language="vb" AutoEventWireup ="false" Codebehind="ind ex.aspx.vb"
Inherits="WinAu th.index1"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<title>index</title>
<meta content="Micros oft Visual Studio .NET 7.1" name="GENERATOR ">
<meta content="Visual Basic .NET 7.1" name="CODE_LANG UAGE">
<meta content="JavaSc ript" name="vs_defaul tClientScript">
<meta content="http://schemas.microso ft.com/intellisense/ie5"
name="vs_target Schema">
</HEAD>
<body style="MARGIN: 0px">
<form id="Form1" method="post" runat="server">
<TABLE id="Table1" cellSpacing="1" cellPadding="5" width="100%"
border="0">
<TR>
<TD vAlign="top" noWrap align="center" width="125">
<P><asp:label id="lblStatus" runat="server"
Visible="false" ></asp:label><br>
<asp:linkbutt on id="lnkLogin" runat="server"
Visible="false" >Please Log In</asp:linkbutton> <asp:label id="lblName"
runat="server" Visible="false" ></asp:label></P>
</TD>
<TD vAlign="top" width="100%">
<DIV align="center"> <asp:label id="Label2" runat="server"
Font-Bold="True" Font-Size="18pt"
Font-Names="Tahoma"> Welcome</asp:label></DIV>
<P align="justify" > &n bsp; Lorem ipsum dolor sit
amet,
consectetuer adipiscing elit. Praesent consequat porta sapien.
Fusce eleifend
urna sit amet justo. Nunc pellentesque justo vel neque. Donec
nonummy ante vel
metus. In aliquam vehicula leo. Curabitur metus. Donec arcu
orci, ultrices ac,
rutrum id, hendrerit vel, tellus. Duis lobortis malesuada
odio. Proin sed enim.
Proin vitae turpis. Integer mollis. Aenean ac quam. Quisque
vulputate purus sit
amet risus.
<br>
</P>
<div align="right">< asp:linkbutton id="lnkEdit" runat="server"
Visible="false" >Edit Text</asp:linkbutton> </div>
</TD>
</TR>
</TABLE>
<div align="center" style="width:50 0px;">
<p align="justify" >
<asp:Label id="lblError" runat="server"
Visible="false" ></asp:Label>
</p>
</div>
</form>
</body>
</HTML>
----------------------------------------------------------
** First Page Code-Behind **
----------------------------------------------------------
Public Class index1
Inherits System.Web.UI.P age
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
End Sub
Protected WithEvents lblStatus As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label2 As System.Web.UI.W ebControls.Labe l
Protected WithEvents lnkEdit As System.Web.UI.W ebControls.Link Button
Protected WithEvents lnkLogin As System.Web.UI.W ebControls.Link Button
Protected WithEvents lblName As System.Web.UI.W ebControls.Labe l
Protected WithEvents lblError As System.Web.UI.W ebControls.Labe l
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Not Page.IsPostBack Then
Select Case UserIsKnown()
Case False
lblStatus.Text = "Anonymous User"
lblStatus.Visib le = True
lnkLogin.Visibl e = True
Case True
Response.Write( "<!-- Session(""FullN ame""): " &
Session("FullNa me") & " -->" & ControlChars.Ne wLine)
Response.Write( "<!-- Session(""UserN ame""): " &
Session("UserNa me") & " -->" & ControlChars.Ne wLine)
Dim FullName As String = Session("FullNa me")
Dim UserName As String = Session("UserNa me")
lblStatus.Text = "Welcome Back!"
lblStatus.Visib le = True
lblName.Text = FullName
lblName.Visible = True
lnkEdit.Visible = True
End Select
If Not Session("Error" ) Is Nothing Then
lblError.Text = "<PRE>" & ControlChars.Ne wLine &
Session("Error" ) & "</PRE>"
lblError.Visibl e = True
End If
End If
End Sub
Private Function UserIsKnown() As Boolean
If Session("Logged In") Is Nothing Then Session("Logged In") = False
Return (Context.User.I dentity.IsAuthe nticated Or
Session("Logged In"))
End Function
Private Sub lnkLogin_Click( ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles lnkLogin.Click
Response.Redire ct("/WinAuth/Login/", True)
End Sub
End Class
----------------------------------------------------------
** Second Page HTML **
----------------------------------------------------------
No HTML was added to default "new page html"
----------------------------------------------------------
** Second Page Code-Behind **
----------------------------------------------------------
Public Class index
Inherits System.Web.UI.P age
Private cADSI As New Auth.adsi
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub
InitializeCompo nent()
End Sub
'NOTE: The following placeholder declaration is required by the Web Form
Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub
#End Region
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
If Context.User.Id entity.IsAuthen ticated Then
Try
Session("Logged In") = True
Session("UserNa me") = Context.User.Id entity.Name.ToS tring
Session("UserNa me") =
Session("UserNa me").Substring( Session("UserNa me").IndexOf("\ ") + 1)
With cADSI
.AD_ServerName = "pdcdns"
.AD_ServiceProv ider = "LDAP"
.AD_UserName = Session("UserNa me")
Session("FullNa me") = .GetUserProps()
End With
If Session("FullNa me") = "" Then
Session("Logged In") = False
End If
Catch ex As Exception
Session("Logged In") = False
Session("Error" ) = ex.ToString
End Try
End If
Response.Redire ct("/WinAuth/", True)
End Sub
End Class
----------------------------------------------------------
** Class File **
----------------------------------------------------------
'// Example By James Arceri
'// Please send comments or questions to: ja*****@starion .com
'// Code taken from:
http://www.411asp.net/func/content?tree=41 1asp/tutorial/howto/usermana&id=397 2310
Imports System
Imports System.Director yServices
Imports System.Text
Namespace Auth
' Active Directory Services Interfaces
Public Class adsi
Private mvar_AD_ServerN ame As String
Private mvar_AD_Service Provider As String
Private mvar_AD_UserNam e As String
' Active Directory server name
Public Property AD_ServerName() As String
Get
Return mvar_AD_ServerN ame
End Get
Set(ByVal Value As String)
mvar_AD_ServerN ame = Value
End Set
End Property
' Active Directory Service provider
Public Property AD_ServiceProvi der() As String
Get
Return mvar_AD_Service Provider
End Get
Set(ByVal Value As String)
mvar_AD_Service Provider = Value
End Set
End Property
' Active Directory username
Public Property AD_UserName() As String
Get
Return mvar_AD_UserNam e
End Get
Set(ByVal Value As String)
mvar_AD_UserNam e = Value
End Set
End Property
' Gets the info for a specific user
' Currently only supports Lightweight Directory Access Protocol
(LDAP) binding
Public Function GetUserProps() As String
Try
Dim strUserName As Array = Split(mvar_AD_U serName, "\", -1,
1)
Dim strFullName As String
Dim sPath As String
Dim objDirEnt As New
DirectoryServic es.DirectoryEnt ry(mvar_AD_Serv iceProvider & "://" &
mvar_AD_ServerN ame)
Dim objSearcher As New
System.Director yServices.Direc torySearcher(ob jDirEnt)
Dim objSearchRes As System.Director yServices.Searc hResult
objSearcher.Fil ter = ("(anr=" & strUserName(0) & ")")
For Each objSearchRes In objSearcher.Fin dAll
sPath = objSearchRes.Ge tDirectoryEntry .Path
Next
objDirEnt.Close ()
objDirEnt.Path = sPath
strFullName = objDirEnt.Invok e("GET", "Name")
Return strFullName
Catch ex As Exception
Throw
End Try
End Function
End Class
End Namespace
----------------------------------------------------------