C# 模拟导致未处理的异常
我有一个C windows应用程序,我需要模拟一个用户ID,而不是当我访问文件共享上的文件时用户登录的用户ID。我使用模拟切换到此用户,然后切换回。我还需要恢复到登录用户以访问本地文件。我得到一个System.ExecutionEngineeException,应用程序没有捕获它,它只是停止。这是代码。任何关于我为什么会犯这个错误的想法,或者关于如何做得更好的建议 编辑:应用程序正在使用DevExpress提供的XAF框架C# 模拟导致未处理的异常,c#,xaf,C#,Xaf,我有一个C windows应用程序,我需要模拟一个用户ID,而不是当我访问文件共享上的文件时用户登录的用户ID。我使用模拟切换到此用户,然后切换回。我还需要恢复到登录用户以访问本地文件。我得到一个System.ExecutionEngineeException,应用程序没有捕获它,它只是停止。这是代码。任何关于我为什么会犯这个错误的想法,或者关于如何做得更好的建议 编辑:应用程序正在使用DevExpress提供的XAF框架 public static class Impersonation {
public static class Impersonation
{
#region Fields
public static WindowsImpersonationContext newUser;
public static string domain;
public static string userName;
public static string password;
#endregion
// group type enum
private enum SECURITY_IMPERSONATION_LEVEL : int
{
SecurityAnonymous = 0,
SecurityIdentification = 1,
SecurityImpersonation = 2,
SecurityDelegation = 3
}
// obtains user token
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);
// creates duplicate token handle
[DllImport("advapi32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private extern static bool DuplicateToken(IntPtr ExistingTokenHandle, int SECURITY_IMPERSONATION_LEVEL, ref IntPtr DuplicateTokenHandle);
#region Methods
public static void RevertUser()
{
newUser.Undo();
}
#region ImpersonateApexApplicationUser
/// <summary>
/// Attempts to impersonate a user. If successful, returns
/// a WindowsImpersonationContext of the new users identity.
/// </summary>
/// <param name="sUsername">Username you want to impersonate</param>
/// <param name="sDomain">Logon domain</param>
/// <param name="sPassword">User's password to logon with</param></param>
/// <returns></returns>
public static void ImpersonateApexApplicationUser()
{
// initialize tokens
IntPtr pExistingTokenHandle = new IntPtr(0);
IntPtr pDuplicateTokenHandle = new IntPtr(0);
pExistingTokenHandle = IntPtr.Zero;
pDuplicateTokenHandle = IntPtr.Zero;
try
{
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
if (!LogonUser(userName, domain, Encryption.Decrypt(password), LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref pExistingTokenHandle))
throw new Exception(string.Format("LogonUser() failed with error code {0}", Marshal.GetLastWin32Error()));
else
if (!DuplicateToken(pExistingTokenHandle, (int)SECURITY_IMPERSONATION_LEVEL.SecurityImpersonation, ref pDuplicateTokenHandle))
{
int errorCode = Marshal.GetLastWin32Error();
CloseHandle(pExistingTokenHandle); // close existing handle
throw new Exception(string.Format("DuplicateToken() failed with error code: {0}", errorCode));
}
else
{
// create new identity using new primary token
WindowsIdentity newId = new WindowsIdentity(pDuplicateTokenHandle);
newUser = newId.Impersonate();
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
// close handle(s)
if (pExistingTokenHandle != IntPtr.Zero)
CloseHandle(pExistingTokenHandle);
if (pDuplicateTokenHandle != IntPtr.Zero)
CloseHandle(pDuplicateTokenHandle);
}
}
#endregion
}
然后:
Impersonation.ImpersonateApexApplicationUser();
//file share access code
Impersonation.RevertUser();
如果单步执行,将在ImpersonateAppExApplicationUser方法中的哪一行/调用引发异常?或者它是在RevertUser中抛出的?我无法判断异常是在哪里抛出的,因为我的任何异常处理都没有捕获到它。它不在模拟类或其任何方法中。是否已将进程正在运行的主用户添加到本地安全策略中,并作为操作系统的一部分?登录用户拥有文件的权限没有问题。
Impersonation.ImpersonateApexApplicationUser();
//file share access code
Impersonation.RevertUser();