How do you do Impersonation in .NET?

前端 未结 7 2384
情话喂你
情话喂你 2020-11-21 07:01

Is there a simple out of the box way to impersonate a user in .NET?

So far I\'ve been using this class from code project for all my impersonation requirements.

7条回答
  •  谎友^
    谎友^ (楼主)
    2020-11-21 08:03

    This is probably what you want:

    using System.Security.Principal;
    using(WindowsIdentity.GetCurrent().Impersonate())
    {
         //your code goes here
    }
    

    But I really need more details to help you out. You could do impersonation with a config file (if you're trying to do this on a website), or through method decorators (attributes) if it's a WCF service, or through... you get the idea.

    Also, if we're talking about impersonating a client that called a particular service (or web app), you need to configure the client correctly so that it passes the appropriate tokens.

    Finally, if what you really want do is Delegation, you also need to setup AD correctly so that users and machines are trusted for delegation.

    Edit:
    Take a look here to see how to impersonate a different user, and for further documentation.

提交回复
热议问题