我使用的LogonUser Win32 API的:

token = LogonUser(...)
WindowsIdentity newId = new WindowsIdentity(token);            
WindowsImpersonationContext impersonatedUser = newId.Impersonate();

然而在此之后调用WCF服务时,我不能够使用模拟的标识。我想这是因为impersonatedUser.ImpersonationLevel等于模拟。

这是什么原因呢? 是ImpersonationLevel.Identification的水平我需要什么? 如何获得这样的水平?

有帮助吗?

解决方案

我不知道这是否会为WCF工作。但是,我们用它在我们的生产Web应用程序的模拟来读取和写入文件到文件系统。您需要定义API对AdvApi32.LogonUser,AdvApi32.DuplicateToken和Kernel32.CloseHandle并确保关闭WindowsImpersonationContext当你完成。

    /// <summary>impersonates a user</summary>
    /// <param name="username">domain\name of the user account</param>
    /// <param name="password">the user's password</param>
    /// <returns>the new WindowsImpersonationContext</returns>
    public static WindowsImpersonationContext ImpersonateUser(String username, String password)
    {
        WindowsIdentity winId = WindowsIdentity.GetCurrent();
        if (winId != null)
        {
            if (string.Compare(winId.Name, username, true) == 0)
            {
                return null;
            }
        }

        //define the handles
        IntPtr existingTokenHandle = IntPtr.Zero;
        IntPtr duplicateTokenHandle = IntPtr.Zero;

        String domain;
        if (username.IndexOf("\\") > 0)
        {
            //split domain and name
            String[] splitUserName = username.Split('\\');
            domain = splitUserName[0];
            username = splitUserName[1];
        }
        else
        {
            domain = String.Empty;
        }

        try
        {
            //get a security token

            bool isOkay = AdvApi32.LogonUser(username, domain, password,
                (int) AdvApi32.LogonTypes.LOGON32_LOGON_INTERACTIVE,
                (int) AdvApi32.LogonTypes.LOGON32_PROVIDER_DEFAULT,
                ref existingTokenHandle);

            if (!isOkay)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                int lastError = Kernel32.GetLastError();

                throw new Exception("LogonUser Failed: " + lastWin32Error + " - " + lastError);
            }

            // copy the token

            isOkay = AdvApi32.DuplicateToken(existingTokenHandle,
                (int) AdvApi32.SecurityImpersonationLevel.SecurityImpersonation,
                ref duplicateTokenHandle);

            if (!isOkay)
            {
                int lastWin32Error = Marshal.GetLastWin32Error();
                int lastError = Kernel32.GetLastError();
                Kernel32.CloseHandle(existingTokenHandle);
                throw new Exception("DuplicateToken Failed: " + lastWin32Error + " - " + lastError);
            }
            // create an identity from the token

            WindowsIdentity newId = new WindowsIdentity(duplicateTokenHandle);
            WindowsImpersonationContext impersonatedUser = newId.Impersonate();

            return impersonatedUser;
        }
        finally
        {
            //free all handles
            if (existingTokenHandle != IntPtr.Zero)
            {
                Kernel32.CloseHandle(existingTokenHandle);
            }
            if (duplicateTokenHandle != IntPtr.Zero)
            {
                Kernel32.CloseHandle(duplicateTokenHandle);
            }
        }
    }

其他提示

  

在这之后,我无法使用   模拟的标识

在模拟应该有效用于在同一个盒子访问,但不是在网络上。

这可能是,作为consultutah的代码所示,你只需要调用DuplicateToken()为了登录标记转换为模拟令牌,然后才能使用它。

  

我认为这是因为impersonatedUser.ImpersonationLevel等于模拟。

如果您需要作为模拟的用户到其他系统,你需要扮演一个更高层次称为“代表团”。这基本上等同于具有用户的密码,这样你就可以代表自己作为他们给别人。

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top