Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
.net 如何清除来自logonuser的模拟”;“新证书”;_.net_Vb.net_Pinvoke_Impersonation - Fatal编程技术网

.net 如何清除来自logonuser的模拟”;“新证书”;

.net 如何清除来自logonuser的模拟”;“新证书”;,.net,vb.net,pinvoke,impersonation,.net,Vb.net,Pinvoke,Impersonation,我有一个winform,我想在其中模拟不同的用户来调用IIS中托管的服务。我从文本框中获取用户凭据,非常基本。我正在pinvoking“logonuser”以使用logontype“New Credentials”获取模拟令牌(我不能使用logontype“Interactive”,因为我们的组策略不允许它) 这很好,我可以使用模拟身份调用IIS服务,因为它有一组新的网络凭据,但我的问题是,我找不到以后如何清除模拟(如果可能的话,我也不想使用winapi“RevertoSelf”函数) 如何恢复

我有一个winform,我想在其中模拟不同的用户来调用IIS中托管的服务。我从文本框中获取用户凭据,非常基本。我正在pinvoking“logonuser”以使用logontype“New Credentials”获取模拟令牌(我不能使用logontype“Interactive”,因为我们的组策略不允许它)

这很好,我可以使用模拟身份调用IIS服务,因为它有一组新的网络凭据,但我的问题是,我找不到以后如何清除模拟(如果可能的话,我也不想使用winapi“RevertoSelf”函数)

如何恢复到原始用户网络凭据?以下是我的模拟功能:

       Friend Shared Function GetContext(ByVal username As String, ByVal domain As String, ByVal password As SecureString) As WindowsImpersonationContext

            Dim token As IntPtr = IntPtr.Zero
            Dim passwordPtr As IntPtr = IntPtr.Zero
            Dim idContext As WindowsIdentity

            Try
                passwordPtr = Marshal.SecureStringToGlobalAllocUnicode(password)

                If NativeMethods.LogonUser( username,
                                            domain,
                                            passwordPtr,
                                            CInt(NativeMethods.LogonSessionType.NewCredentials),
                                            CInt(NativeMethods.LogonProvider.Default),
                                            token) = False Then
                    Throw New ComponentModel.Win32Exception(Marshal.GetLastWin32Error())
                End If

                idContext = New WindowsIdentity(token)

                Return idContext.Impersonate()
            Catch ex As Exception
                Throw
            Finally
                Marshal.ZeroFreeGlobalAllocUnicode(passwordPtr)
                If Not IntPtr.op_Equality(token, IntPtr.Zero) Then
                    NativeMethods.CloseHandle(token)
                End If
            End Try

        End Function
因此,在使用来自此函数的windowsimpersonationcontext后,如果我只对其调用“undo()”,它将不会恢复为原始用户的网络凭据。如何清除此新凭据

有什么想法吗

编辑


我现在使用LOGON32\u LOGON\u NETWORK\u CLEARTEXT LogonType,获得了所需的行为。对函数返回的上下文调用Undo()现在停止模拟,我可以使用网络操作的凭据

尝试处理上下文和标识(除非重新使用)。@AntonTykhyy我在finally块中尝试了它。不走运。这个windowsidentity对象从不离开函数,我只在以后保留impersonationcontext(我在上面尝试“.Undo()”),您肯定不会在这里处理它!在使用完模拟后,您将对其进行处理。您还需要复制令牌,请参见这个问题@AntonTykhyy谢谢,我通过更改调用logonuser时使用的logontype找到了答案。我认为还值得注意的是,windowsidentity Constructor为您复制了令牌,因此无需事先pinvoke duplicatetoken。Cheers WindowsIdentity构造函数仅复制令牌句柄(DuplicateHandle),而不复制令牌本身(DuplicateToken)。IIRC必须复制令牌才能将其转换为模拟令牌。