• 一个在.net下进行用户模拟的类


    实质上是通过WindowsIdentity.Impersonate()的方法,其中需要调用Win API来获得活用的Handle,用法其实很简单,因为在自己的代码中需要用到,就稍微封装了一下:

    ?public class IdentityImpersonation {

    ??[DllImport("advapi32.dll", SetLastError=true)]
    ??public static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
    ???int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

    ??[DllImport("advapi32.dll", CharSet=CharSet.Auto, SetLastError=true)]
    ??public extern static bool DuplicateToken(IntPtr ExistingTokenHandle,
    ???int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);

    ??[DllImport("kernel32.dll", CharSet=CharSet.Auto)]
    ??public extern static bool CloseHandle(IntPtr handle);


    ??// 要模拟的用户的用户名、密码、域(机器名)
    ??private String _sImperUsername;
    ??private String _sImperPassword;
    ??private String _sImperDomain;
    ??// 记录模拟上下文
    ??private WindowsImpersonationContext _imperContext;
    ??private IntPtr _adminToken;
    ??private IntPtr _dupeToken;
    ??// 是否已停止模拟
    ??private Boolean _bClosed;

    ??public IdentityImpersonation(String impersonationUsername, String impersonationPassword, String impersonationDomain) {
    ???_sImperUsername = impersonationUsername;
    ???_sImperPassword = impersonationPassword;
    ???_sImperDomain = impersonationDomain;

    ???_adminToken = IntPtr.Zero;
    ???_dupeToken = IntPtr.Zero;
    ???_bClosed = true;
    ??}

    ??~IdentityImpersonation() {
    ???if(! _bClosed) {
    ????StopImpersonation();
    ???}
    ??}

    ??public Boolean BeginImpersonate() {
    ?
    ???Boolean bLogined = LogonUser(_sImperUsername, _sImperDomain, _sImperPassword, 2, 0, ref _adminToken);
    ???
    ???if(! bLogined) {
    ????return false;
    ???}

    ???Boolean bDuped = DuplicateToken(_adminToken, 2, ref _dupeToken);

    ???if(! bDuped) {
    ????return false;
    ???}

    ???WindowsIdentity fakeId = new WindowsIdentity(_dupeToken);
    ???_imperContext = fakeId.Impersonate();

    ???_bClosed = false;

    ???return true;
    ??}

    ??public void StopImpersonate() {
    ???_imperContext.Undo();
    ???CloseHandle(_dupeToken);
    ???CloseHandle(_adminToken);
    ???_bClosed = true;
    ??}
    ?}

    ?

    使用示例:

    IdentityImpersonation imper = new IdentityImpersonation("tsg", "123456", "webreal");
    imper.BeginImpersonation();
    // ...
    imper.StopImpersonation();

  • 相关阅读:
    kafka消费者如何才能从头开始消费某个topic的全量数据
    kafka消费者客户端启动之后消费不到消息的原因分析
    JMeter测试工具中的参数化使用[函数助手]
    在IDEA中使用gradle配置打可执行jar包[可执行jar与其所依赖的jar分离]
    一次tomcat配置参数调优Jmeter压力测试记录前后对比
    IntelliJ IDEA 14.1.4导入项目启动报错:Error during artifact deployment.[组件部署期间出错]
    BZOJ2861 : 双向边定向为单向边
    BZOJ4313 : 三维积木
    BZOJ4714 : 旋转排列
    BZOJ1395 : [Baltic2005]Trip
  • 原文地址:https://www.cnblogs.com/kaneboy/p/2436672.html
Copyright © 2020-2023  润新知