Git Product home page Git Product logo

simpleimpersonation's Introduction

SimpleImpersonation

Simple Impersonation Library for .Net

This library provides a managed wrapper for the LogonUser function of the Win32 API. Basically, it allows you to impersonate any user, as long as you have their credentials.

It is dual compiled for both .Net 2.0 and .Net 4.0 runtimes. It should work well under .Net 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, and the various "client profiles". It has no dependencies.

Note that it is not compiled for PCL or .NET Core, as it is Windows specific.

Installation

Use NuGet package SimpleImpersonation.

PM> Install-Package SimpleImpersonation

Usage

using (Impersonation.LogonUser(domain, username, password, logonType))
{
    // do whatever you want as this user.
}

The password parameter can be specified as a SecureString. You can still pass a regular string if desired, however SecureString is recommended.

Be sure to specify a logon type that makes sense for what you are doing. For example:

  • If you are interactively working as a particular user from a desktop application, use LogonType.Interactive.

  • If you are trying to connect to a SQL server with trusted authentication using specific credentials, use LogonType.NewCredentials.

    • But be aware that impersonation is not taken into account in connection pooling.
    • You will also need to vary your connection string.
    • Read more here
  • If impersonation fails, it will throw a custom ImpersonationException, which has the following properties:

    • Message : The string message describing the error.
    • NativeErrorCode : The native Windows error code, as described here.
    • ErrorCode : The HResult of the error.
    • InnerException : A Win32Exception used to derive the other properties.

    Note that it derives from ApplicationException for better compatibility with previous versions of this library.

See the MSDN documentation for additional logon types.

Changelog

1.0.0

  • Initial Version

1.0.1

  • Issue #2 - Fixes possible "SafeHandle cannot be null"

1.1.0

  • Issue #9 - Adds support for passing the password as a SecureString

2.0.0

  • Issue #14 - Throws a more useful exception. (This is a breaking change if you were previously parsing the error code out of the message string).

2.0.1

  • Issue #17 - Adds the NativeError code to the exception.

simpleimpersonation's People

Contributors

mattjohnsonpint avatar michael-wanke avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.