How do you do Impersonation in .NET?

前端 未结 7 2249
情话喂你
情话喂你 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 07:39

    I'm aware that I'm quite late for the party, but I consider that the library from Phillip Allan-Harding, it's the best one for this case and similar ones.

    You only need a small piece of code like this one:

    private const string LOGIN = "mamy";
    private const string DOMAIN = "mongo";
    private const string PASSWORD = "HelloMongo2017";
    
    private void DBConnection()
    {
        using (Impersonator user = new Impersonator(LOGIN, DOMAIN, PASSWORD, LogonType.LOGON32_LOGON_NEW_CREDENTIALS, LogonProvider.LOGON32_PROVIDER_WINNT50))
        {
        }
    }
    

    And add his class:

    .NET (C#) Impersonation with Network Credentials

    My example can be used if you require the impersonated logon to have network credentials, but it has more options.

提交回复
热议问题