Impersonation headache

Ask a Question related to ASP.NET Security, Design and Development.

  1. #1

    Default Impersonation headache

    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\ProductReques t\" & 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


    James Pemberton Guest

  2. Similar Questions and Discussions

    1. Bit of a headache
      Hi all, I have found this bit of javascript to validate a users form input, but the problem is that it does it in steps - ie it will check the...
    2. transform.rotation gives me headache
      Hello Im working with an Adventure game like Grim fandango. Currently im working on this script; when you get close to an interactive obejct,...
    3. Regex headache
      I am having a regex nightmare and can't see the wood for the trees. I want to extract data from an HTML file. I have been using the file() command...
    4. gradient headache
      Does any body see a problem with this? I get nothing. I want a triangle with blue in the lower right corner fading to transparent at the...
    5. Web Service headache
      Hi there, I am working on a web service, which was going fine until this morning. Both it and my test client app (a simple web app) are running...
  3. #2

    Default Re: Impersonation headache

    These both sound like double-hop delegation issues. The fact that it works
    when you specify specific credentials in your impersonate tag but doesn't
    work when you use Window Integrated Authentication (WIA) and try to access
    resources on a different machine than the IIS box suggests this. The
    impersonation token that WIA creates cannot hop to another machine on the
    network (like your file server or AD) unless Kerberos delegation has been
    enabled and working.

    I'd suggest you read up on that first and then come back here if you can't
    get it to work or need a different approach.

    [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;810572[/url]

    You'll find even more links with a little searching.

    Cheers,

    Joe K.

    "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    news:eD9ilYpuEHA.2172@TK2MSFTNGP14.phx.gbl...
    >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\ProductReques t\" & 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
    >
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  4. #3

    Default Re: Impersonation headache

    Thanks for the site.

    I've read quite a bit about delegates, but I have one more question. Do you
    have to setup your entire network, hardware and users, to utilize
    delegation? Or can you just set up those users or servers that you think
    will need to access information remotely?


    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:OOPj1upuEHA.272@TK2MSFTNGP15.phx.gbl...
    > These both sound like double-hop delegation issues. The fact that it
    > works when you specify specific credentials in your impersonate tag but
    > doesn't work when you use Window Integrated Authentication (WIA) and try
    > to access resources on a different machine than the IIS box suggests this.
    > The impersonation token that WIA creates cannot hop to another machine on
    > the network (like your file server or AD) unless Kerberos delegation has
    > been enabled and working.
    >
    > I'd suggest you read up on that first and then come back here if you can't
    > get it to work or need a different approach.
    >
    > [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;810572[/url]
    >
    > You'll find even more links with a little searching.
    >
    > Cheers,
    >
    > Joe K.
    >
    > "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    > news:eD9ilYpuEHA.2172@TK2MSFTNGP14.phx.gbl...
    >>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\ProductReques t\" & 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
    >>
    >>
    >
    >

    James Pemberton Guest

  5. #4

    Default Re: Impersonation headache

    I'm not a great expert on delegation, but you can enable delegation on a per
    user basis in AD. The other trick you have to be careful with is that
    delegation requires Kerberos, so you need to make sure your authentication
    is Kerberos end to end. If it fails over to NTLM, then delegation will
    suddenly break. Sometimes this will manifest itself as intermittent
    problems, where the user fails on one workstation, but works on another or a
    different network.

    Joe K.

    "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    news:Olwt45suEHA.2016@TK2MSFTNGP15.phx.gbl...
    > Thanks for the site.
    >
    > I've read quite a bit about delegates, but I have one more question. Do
    > you have to setup your entire network, hardware and users, to utilize
    > delegation? Or can you just set up those users or servers that you think
    > will need to access information remotely?
    >
    >
    > "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    > in message news:OOPj1upuEHA.272@TK2MSFTNGP15.phx.gbl...
    >> These both sound like double-hop delegation issues. The fact that it
    >> works when you specify specific credentials in your impersonate tag but
    >> doesn't work when you use Window Integrated Authentication (WIA) and try
    >> to access resources on a different machine than the IIS box suggests
    >> this. The impersonation token that WIA creates cannot hop to another
    >> machine on the network (like your file server or AD) unless Kerberos
    >> delegation has been enabled and working.
    >>
    >> I'd suggest you read up on that first and then come back here if you
    >> can't get it to work or need a different approach.
    >>
    >> [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;810572[/url]
    >>
    >> You'll find even more links with a little searching.
    >>
    >> Cheers,
    >>
    >> Joe K.
    >>
    >> "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    >> news:eD9ilYpuEHA.2172@TK2MSFTNGP14.phx.gbl...
    >>>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\ProductReques t\" & 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
    >>>
    >>>
    >>
    >>
    >
    >

    Joe Kaplan \(MVP - ADSI\) Guest

  6. #5

    Default Re: Impersonation headache

    In addition to Joe's comments, the following document is quite useful in
    getting these things setup:

    Troubleshooting Kerberos Errors
    [url]http://www.microsoft.com/technet/prodtechnol/windowsserver2003/technologies/security/tkerberr.mspx[/url]

    It covers Kerberos and delegation basics, common scenarios and
    troubleshooting tips and tools.

    Cheers
    Ken

    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:eRzBBatuEHA.1396@tk2msftngp13.phx.gbl...
    > I'm not a great expert on delegation, but you can enable delegation on a
    > per user basis in AD. The other trick you have to be careful with is that
    > delegation requires Kerberos, so you need to make sure your authentication
    > is Kerberos end to end. If it fails over to NTLM, then delegation will
    > suddenly break. Sometimes this will manifest itself as intermittent
    > problems, where the user fails on one workstation, but works on another or
    > a different network.
    >
    > Joe K.
    >
    > "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    > news:Olwt45suEHA.2016@TK2MSFTNGP15.phx.gbl...
    >> Thanks for the site.
    >>
    >> I've read quite a bit about delegates, but I have one more question. Do
    >> you have to setup your entire network, hardware and users, to utilize
    >> delegation? Or can you just set up those users or servers that you think
    >> will need to access information remotely?
    >>
    >>
    >> "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com>
    >> wrote in message news:OOPj1upuEHA.272@TK2MSFTNGP15.phx.gbl...
    >>> These both sound like double-hop delegation issues. The fact that it
    >>> works when you specify specific credentials in your impersonate tag but
    >>> doesn't work when you use Window Integrated Authentication (WIA) and try
    >>> to access resources on a different machine than the IIS box suggests
    >>> this. The impersonation token that WIA creates cannot hop to another
    >>> machine on the network (like your file server or AD) unless Kerberos
    >>> delegation has been enabled and working.
    >>>
    >>> I'd suggest you read up on that first and then come back here if you
    >>> can't get it to work or need a different approach.
    >>>
    >>> [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;810572[/url]
    >>>
    >>> You'll find even more links with a little searching.
    >>>
    >>> Cheers,
    >>>
    >>> Joe K.
    >>>
    >>> "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    >>> news:eD9ilYpuEHA.2172@TK2MSFTNGP14.phx.gbl...
    >>>>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\ProductReques t\" & 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
    >>>>
    >>>>
    >>>
    >>>
    >>
    >>
    >
    >

    Ken Schaefer Guest

  7. #6

    Default Re: Impersonation headache

    I actually did get this to work, without using delegation on the users and
    hardware in AD, using the example from:
    [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;306158[/url]


    Hopefully one last question though. I am currently hardcoding the user
    name, password, and domain into the code and really don't like doing that.
    I know I can retrieve the domain and username from the
    WindowsIdentity.Getcurrent.Name, but is there anyway to exacting the users
    password without having them type it in on a logon screen?

    Thanks

    "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    in message news:OOPj1upuEHA.272@TK2MSFTNGP15.phx.gbl...
    > These both sound like double-hop delegation issues. The fact that it
    > works when you specify specific credentials in your impersonate tag but
    > doesn't work when you use Window Integrated Authentication (WIA) and try
    > to access resources on a different machine than the IIS box suggests this.
    > The impersonation token that WIA creates cannot hop to another machine on
    > the network (like your file server or AD) unless Kerberos delegation has
    > been enabled and working.
    >
    > I'd suggest you read up on that first and then come back here if you can't
    > get it to work or need a different approach.
    >
    > [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;810572[/url]
    >
    > You'll find even more links with a little searching.
    >
    > Cheers,
    >
    > Joe K.
    >
    > "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    > news:eD9ilYpuEHA.2172@TK2MSFTNGP14.phx.gbl...
    >>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\ProductReques t\" & 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
    >>
    >>
    >
    >

    James Pemberton Guest

  8. #7

    Default Re: Impersonation headache

    The only way to get a plain text password is to either use forms
    authentication or use Basic authentication. With Windows Integrated
    authentication, the plain password is never sent to the IIS server, only a
    hash, so you can't get it. This is why IIS creates an impersonation token
    and you end up in your double-hop/delegation problem to begin with.

    So basically, you can't have it both ways. You either prompt for the
    password somehow or use delegation.

    If you have 2003 AD, you might also be able to use what is called Kerberos
    S4U. Essentially, this allows you to specify a machine as trusted and allow
    it to create a token for the user given only their userPrincipalName. There
    is a good article on this here:

    [url]http://msdn.microsoft.com/msdnmag/issues/03/04/SecurityBriefs/default.aspx[/url]

    Joe K.

    "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    news:e0lGoz3uEHA.452@TK2MSFTNGP09.phx.gbl...
    >I actually did get this to work, without using delegation on the users and
    >hardware in AD, using the example from:
    > [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;306158[/url]
    >
    >
    > Hopefully one last question though. I am currently hardcoding the user
    > name, password, and domain into the code and really don't like doing that.
    > I know I can retrieve the domain and username from the
    > WindowsIdentity.Getcurrent.Name, but is there anyway to exacting the users
    > password without having them type it in on a logon screen?
    >
    > Thanks
    >
    > "Joe Kaplan (MVP - ADSI)" <joseph.e.kaplan@removethis.accenture.com> wrote
    > in message news:OOPj1upuEHA.272@TK2MSFTNGP15.phx.gbl...
    >> These both sound like double-hop delegation issues. The fact that it
    >> works when you specify specific credentials in your impersonate tag but
    >> doesn't work when you use Window Integrated Authentication (WIA) and try
    >> to access resources on a different machine than the IIS box suggests
    >> this. The impersonation token that WIA creates cannot hop to another
    >> machine on the network (like your file server or AD) unless Kerberos
    >> delegation has been enabled and working.
    >>
    >> I'd suggest you read up on that first and then come back here if you
    >> can't get it to work or need a different approach.
    >>
    >> [url]http://support.microsoft.com/default.aspx?scid=kb;en-us;810572[/url]
    >>
    >> You'll find even more links with a little searching.
    >>
    >> Cheers,
    >>
    >> Joe K.
    >>
    >> "James Pemberton" <james.pemberton@devro-casings.com> wrote in message
    >> news:eD9ilYpuEHA.2172@TK2MSFTNGP14.phx.gbl...
    >>>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\ProductReques t\" & 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
    >>>
    >>>
    >>
    >>
    >
    >

    Joe Kaplan \(MVP - ADSI\) Guest

Posting Permissions

  • You may not post new threads
  • You may post replies
  • You may not post attachments
  • You may not edit your posts

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139