J
James Pemberton
I have been fighting with impersonation for quite sometime now and now
matter what I have tried it just won't work.
I am trying to get information on two items:
1) I'd like to retrieve a file listing from a directory on our file
server.
As with most cases I have read about, it works fine on my development PC, XP
OS, but when ran off of the web server, I receive <error: an exception of
type: {System.UnauthorizedAccessException} occurred>.
The only way I can get it to work is to set impersonation=true and set the
username and password as out system administrator. I did try to set the
user name and password as an AD user we created with full access to the
directory, but to no avail.
On IIS I have just Integrated Windows Authentication checked.
Web.config is as follows: <identity impersonate="true" />
Code:
Private Sub LoadFiles()
Dim impersonationContext As WindowsImpersonationContext
Dim currentWindowsIdentity As WindowsIdentity
currentWindowsIdentity = CType(WindowsIdentity.GetCurrent, WindowsIdentity)
impersonationContext = currentWindowsIdentity.Impersonate()
Dim dt As DataTable = New DataTable
Dim dr As DataRow
dt.Columns.Add("linkname")
dt.Columns.Add("textname")
Dim di As System.IO.DirectoryInfo
'Dim DirectoryDefault As String =
"\\ussfs01\private\Manufacturing\ProductRequest\" & intRequestNbr & "\"
Dim DirectoryDefault As String = "\\ussfs01\ProductRequest\" & intRequestNbr
& "\"
di = New System.IO.DirectoryInfo(DirectoryDefault)
If di.Exists Then
For Each filename As System.IO.FileInfo In di.GetFiles()
dr = dt.NewRow()
dr("linkname") = DirectoryDefault & filename.Name
dr("textname") = filename.Name
dt.Rows.Add(dr)
Next
Dim dv As DataView = New DataView(dt)
dlAttachments.DataSource = dv
dlAttachments.DataBind()
dlAttachments.Visible = True
End If
impersonationContext.Undo()
End Sub
2) In the same program I have been trying to retrive the users fullname,
displayname, or given name from our AD. Once again this works fine on my
Development PC, but on the web server I can't even retrieve those
attributes.
I have tried the following code to no avail:
Dim userkey As String = WindowsIdentity.GetCurrent.Name.Substring(3)
Dim dse As New DirectoryEntry("LDAP://US")
Dim dsearch As DirectorySearcher = New DirectorySearcher(dse)
dsearch.Filter = "(&(objectclass=user)(cn=" & userkey & "))"
dsearch.PropertiesToLoad.Add("displayname")
Dim sr As SearchResult = dsearch.FindOne
If Not (sr Is Nothing) Then
Dim rp As ResultPropertyCollection = sr.Properties
UserName = rp.Item("displayname").Item(0)
Else
UserName = Nothing
End If
I have tried the following code to no avail:
Any help would be greatly appreciated!
James Pemberton
matter what I have tried it just won't work.
I am trying to get information on two items:
1) I'd like to retrieve a file listing from a directory on our file
server.
As with most cases I have read about, it works fine on my development PC, XP
OS, but when ran off of the web server, I receive <error: an exception of
type: {System.UnauthorizedAccessException} occurred>.
The only way I can get it to work is to set impersonation=true and set the
username and password as out system administrator. I did try to set the
user name and password as an AD user we created with full access to the
directory, but to no avail.
On IIS I have just Integrated Windows Authentication checked.
Web.config is as follows: <identity impersonate="true" />
Code:
Private Sub LoadFiles()
Dim impersonationContext As WindowsImpersonationContext
Dim currentWindowsIdentity As WindowsIdentity
currentWindowsIdentity = CType(WindowsIdentity.GetCurrent, WindowsIdentity)
impersonationContext = currentWindowsIdentity.Impersonate()
Dim dt As DataTable = New DataTable
Dim dr As DataRow
dt.Columns.Add("linkname")
dt.Columns.Add("textname")
Dim di As System.IO.DirectoryInfo
'Dim DirectoryDefault As String =
"\\ussfs01\private\Manufacturing\ProductRequest\" & intRequestNbr & "\"
Dim DirectoryDefault As String = "\\ussfs01\ProductRequest\" & intRequestNbr
& "\"
di = New System.IO.DirectoryInfo(DirectoryDefault)
If di.Exists Then
For Each filename As System.IO.FileInfo In di.GetFiles()
dr = dt.NewRow()
dr("linkname") = DirectoryDefault & filename.Name
dr("textname") = filename.Name
dt.Rows.Add(dr)
Next
Dim dv As DataView = New DataView(dt)
dlAttachments.DataSource = dv
dlAttachments.DataBind()
dlAttachments.Visible = True
End If
impersonationContext.Undo()
End Sub
2) In the same program I have been trying to retrive the users fullname,
displayname, or given name from our AD. Once again this works fine on my
Development PC, but on the web server I can't even retrieve those
attributes.
I have tried the following code to no avail:
Dim userkey As String = WindowsIdentity.GetCurrent.Name.Substring(3)
Dim dse As New DirectoryEntry("LDAP://US")
Dim dsearch As DirectorySearcher = New DirectorySearcher(dse)
dsearch.Filter = "(&(objectclass=user)(cn=" & userkey & "))"
dsearch.PropertiesToLoad.Add("displayname")
Dim sr As SearchResult = dsearch.FindOne
If Not (sr Is Nothing) Then
Dim rp As ResultPropertyCollection = sr.Properties
UserName = rp.Item("displayname").Item(0)
Else
UserName = Nothing
End If
I have tried the following code to no avail:
Any help would be greatly appreciated!
James Pemberton