Windows 7 中的模拟

发布于 2024-12-01 15:43:59 字数 1531 浏览 2 评论 0原文

我有一个模拟类,到目前为止在 Windows XP 上运行得很好,但是当我尝试在 Windows 7 上使用相同的模拟时,我遇到了问题。据我所知,模拟是有效的,但是当我尝试访问该用户下的文件时,我收到访问被拒绝的错误。还有其他人遇到过这个问题并找到解决方法吗?

公共子 BeginImpersonation()

Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2

Dim win32ErrorNumber As Integer

_tokenHandle = IntPtr.Zero
_dupeTokenHandle = IntPtr.Zero

If Not LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then
  win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
  Throw New ImpersonationException(win32ErrorNumber, GetErrorMessage(win32ErrorNumber), _username, _domainname)
End If

If Not DuplicateToken(_tokenHandle, SecurityImpersonation, _dupeTokenHandle) Then
  win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()

  CloseHandle(_tokenHandle)
  Throw New ImpersonationException(win32ErrorNumber, "Unable to duplicate token!", _username, _domainname)
End If

Dim newId As New System.Security.Principal.WindowsIdentity(_dupeTokenHandle)
_impersonatedUser = newId.Impersonate()

结束子

  Dim aa As fcGlobals.AliasAccount
  'Use Impersonation
  aa = New fcGlobals.AliasAccount("xxxx", "xxxx")
  aa.BeginImpersonation()
  For a = 0 To mPages.Count - 1
    If System.IO.File.Exists(mFileNames(a)) = True Then
      System.IO.File.Delete(mFileNames(a))
    End If
  Next

  aa.EndImpersonation()
  aa.Dispose()

I have an impersonation class that so far has been working great on windows XP, however when I try to use the same Impersonation on Windows 7 I have an issue. The impersonation as far as I know works but when I try and access files under this user I get the access denied error. Has anyone else run into this issue and found a fix?

Public Sub BeginImpersonation()

Const LOGON32_PROVIDER_DEFAULT As Integer = 0
Const LOGON32_LOGON_INTERACTIVE As Integer = 2
Const SecurityImpersonation As Integer = 2

Dim win32ErrorNumber As Integer

_tokenHandle = IntPtr.Zero
_dupeTokenHandle = IntPtr.Zero

If Not LogonUser(_username, _domainname, _password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, _tokenHandle) Then
  win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()
  Throw New ImpersonationException(win32ErrorNumber, GetErrorMessage(win32ErrorNumber), _username, _domainname)
End If

If Not DuplicateToken(_tokenHandle, SecurityImpersonation, _dupeTokenHandle) Then
  win32ErrorNumber = System.Runtime.InteropServices.Marshal.GetLastWin32Error()

  CloseHandle(_tokenHandle)
  Throw New ImpersonationException(win32ErrorNumber, "Unable to duplicate token!", _username, _domainname)
End If

Dim newId As New System.Security.Principal.WindowsIdentity(_dupeTokenHandle)
_impersonatedUser = newId.Impersonate()

End Sub

  Dim aa As fcGlobals.AliasAccount
  'Use Impersonation
  aa = New fcGlobals.AliasAccount("xxxx", "xxxx")
  aa.BeginImpersonation()
  For a = 0 To mPages.Count - 1
    If System.IO.File.Exists(mFileNames(a)) = True Then
      System.IO.File.Delete(mFileNames(a))
    End If
  Next

  aa.EndImpersonation()
  aa.Dispose()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文