none
Getting Access to path denied error after impersonationg a user RRS feed

  • Question

  • Hi Team,

    I have a requirement in which I have to impersonate users based on their usernames, as I need to copy files from different folders on which these users are having permission, I am using a .net console application for this.

    I am impersonating using WindowsIdentity, object of this windows identity I am making by giving email address of the user.

    After impersonating I am getting Access to the path is denied error.

    I am having an account using which with no impersonation I am able to successfully copy files from a folder (on a network) but when I impersonate same account and try to copy file, it gives access denied error.

    Can you please help me resolve this.

    Thanks,

    Salil

    Monday, September 23, 2019 12:16 PM

All replies

  • You are probably impersonating with an insufficient impersonation level for the action you wish to perform.

    Check the WindowsIdentity.ImperonationLevgel property.  If it is not Impersonation or Delegation then you must use another method to construct the WindowsIdentity object.  See the examples that use the other user's token for this purpose.

    • Proposed as answer by RLWA32 Tuesday, September 24, 2019 10:09 AM
    Monday, September 23, 2019 12:26 PM
  • Hi,

    Important point here is, users who i need to impersonate are not logged into the application, its a console application which is running under a particular account.

    Just to be more sure, i will get users in rows, these users are having their respective network folders, on these folders, respective users are having access. For each such row, i am only having username and email address of the user( based on these only i can make the WindowsIdentity object).  Now, for each row, i need to impersonate the user, using this impersonation, i need to copy the files from repsective folder of the user. 

    Based on above requirement, can you please mention what is the ideal method to make the WindowsIdentity object? Please keep in mind, i just have username and email address of the user whose identity i have to use(for impersonation). 

    Thanks,

    Salil

    Monday, September 23, 2019 4:07 PM
  • Did your WindowsIdentity object have an ImpersonationLevel of Identification?

    I understand what you want but as a standard user you won't be able to instantiate a WindowsIdentity object with an ImpersonationLevel of Impersonation for other users based solely on a username and email address.  If you think about it, if a standard user was able to do this then no user's private data would be secure.



    • Edited by RLWA32 Monday, September 23, 2019 9:09 PM fixed typos
    • Proposed as answer by RLWA32 Tuesday, September 24, 2019 10:10 AM
    Monday, September 23, 2019 4:22 PM
  • Hi SinghSalil11, 

    Thank you for posting here.

    For your question, you want to use username and email address to find a way to make the WindowsIdentity object.

    As RLWA32 suggested, it may not be able to get the suitable ImpersonationLevel with these informaitons.

    We usually use the token handle returned by LogonUser (advapi32) function which log a user on to the local computer to make the WindowsIdentity object, so we need username, domain ,password and so on.

    Here’s the reference you can refer to do Impersonation.

    How do you do Impersonation in .NET?

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    • Proposed as answer by RLWA32 Tuesday, September 24, 2019 10:10 AM
    Tuesday, September 24, 2019 3:20 AM
    Moderator
  • > For each such row, I am only having username and email address of the user

    But not the password?  How did you think you'd be able to login as a user without their password?


    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Proposed as answer by RLWA32 Tuesday, September 24, 2019 10:10 AM
    Tuesday, September 24, 2019 3:47 AM
  • Hi All,

    Thank you for your replies. I checked, Impersonation level for above case is coming as Identification.

    As previously mentioned my requirement is as such I need to Impersonate a no. of users without them being logged in. I am able to impersonate but looks like Impersonation level is not what is required.

    Now, I am planning to remove Impersonation and schedule my console application so that it runs under an account which is having access to all user folders.

    Thanks,

    Salil

    Tuesday, September 24, 2019 9:25 AM
  • Hi SinghSalil11,

    Thanks for your feedback.

    According to your description, I have a question to confirm with you.

    >> schedule my console application so that it runs under an account which is having access to all user folders.

    Do you want to find a solution to make your console application run under the account which can access to all user folders?

    We are waiting for your update.

    Best Regards,

    Xingyu Zhao


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, September 25, 2019 8:58 AM
    Moderator
  • Do you want to find a solution to make your console application run under the account which can access to all user folders?

    That would be a different topic that should be addressed in a new question.

    In my opinion the inability to successfully impersonate using only a username and email address was adequately addressed and suggested solutions were given in earlier posts in several responses.

    Wednesday, September 25, 2019 10:22 AM
  • Hi,

    I do not want solution for that, was just letting all know the way I was planning to solve this issue.

    All replies about Impersonation were very helpful to me.

    Thanks,

    Salil

    Wednesday, September 25, 2019 11:58 AM