Winform程序,现在有个网络文件夹,一般用户没有权限访问。我有个可以访问此文件夹的用户和密码,现在我用WindowsIdentity进行用户模拟后,仍然不能访问那个网络共享文件夹。请个大侠帮忙看看。下面是模拟的类库。
public static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
{
try
{
// The Windows NT user token.
IntPtr tokenHandle = new IntPtr(0); const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3; tokenHandle = IntPtr.Zero; // Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(User, Domain, Password,
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle); if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
} //The WindowsIdentity class makes a new copy of the token.
//It also handles calling CloseHandle for the copy.
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
catch (Exception ex)
{
throw ex;
}
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);下面是外面的调用。
WindowsImpersonationContext wic = CredentialHelper.CreateIdentity("UserName", "Domain", "Password").Impersonate(); MessageBox.Show(System.Environment.UserName); DoThings(); wic.Undo();测试后,应该已经获取到了模拟的用户,说明模拟是成功的,但是访问还是报权限不足。各位大侠帮忙看看
谢谢
public static WindowsIdentity CreateIdentity(string User, string Domain, string Password)
{
try
{
// The Windows NT user token.
IntPtr tokenHandle = new IntPtr(0); const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_NETWORK = 3; tokenHandle = IntPtr.Zero; // Call LogonUser to obtain a handle to an access token.
bool returnValue = LogonUser(User, Domain, Password,
LOGON32_LOGON_NETWORK, LOGON32_PROVIDER_DEFAULT,
ref tokenHandle); if (false == returnValue)
{
int ret = Marshal.GetLastWin32Error();
} //The WindowsIdentity class makes a new copy of the token.
//It also handles calling CloseHandle for the copy.
WindowsIdentity id = new WindowsIdentity(tokenHandle);
CloseHandle(tokenHandle);
return id;
}
catch (Exception ex)
{
throw ex;
}
}
[DllImport("advapi32.dll", SetLastError = true)]
private static extern bool LogonUser(String lpszUsername, String lpszDomain, String lpszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken); [DllImport("kernel32.dll", CharSet = CharSet.Auto)]
private extern static bool CloseHandle(IntPtr handle);下面是外面的调用。
WindowsImpersonationContext wic = CredentialHelper.CreateIdentity("UserName", "Domain", "Password").Impersonate(); MessageBox.Show(System.Environment.UserName); DoThings(); wic.Undo();测试后,应该已经获取到了模拟的用户,说明模拟是成功的,但是访问还是报权限不足。各位大侠帮忙看看
谢谢
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货