我正在开发一个程序,它必须检查是否有到NAS的连接,以及是否可以从其中写入和读取数据。检查连接不是问题。
我在Windows Server 2012上运行,并且知道来自NAS的凭据。我尝试使用这些凭据进行模拟,但这不起作用。我总是得到同样的错误。用户名或密码不正确。但是,当我使用相同的凭据连接windows资源管理器时,它可以正常工作。
这是我的DLL导入和必要的变量
[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(String lpszUsername, String lpszDomain,
String lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
// Variables
private IntPtr m_Token;
private WindowsImpersonationContext m_Context = null;
private string m_Domain;
private string m_Password;
private string m_Username;m_Domain与本地域不同
这是实际的模拟
m_Token = IntPtr.Zero;
bool logonSuccessfull = LogonUser(
m_Username,
m_Domain,
m_Password,
(int)LOGON32_TYPE_NETWORK,
(int)LOGON32_PROVIDER_WINNT50,
ref m_Token);
if (logonSuccessfull == false)
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
}
WindowsIdentity identity = new WindowsIdentity(m_Token);
m_Context = identity.Impersonate();LOGON32_TYPE_NETWORK等于3,LOGON32_PROVIDER_WINNT50等于3
此代码是以管理员权限执行的。
这是抛出的错误:
System.ComponentModel.Win32Exception (0x80004005): The user name or password is incorrect此错误get被抛出
if (logonSuccessfull == false)
{
int error = Marshal.GetLastWin32Error();
throw new Win32Exception(error);
}还有别的办法吗?或者必须使用其他登录类型或LogonProvider?
我也尝试过这些方法,但对我来说也不起作用。
private const int LOGON32_PROVIDER_DEFAULT = 0;
private const int LOGON32_LOGON_INTERACTIVE = 2;
private const int LOGON32_TYPE_NEW_CREDENTIALS = 9;在使用LOGON32_TYPE_NEW_CREDENTIALS时,它不会抛出错误,但被模拟的用户根本不会更改。
发布于 2019-06-12 18:18:58
我可以通过使用网络凭据和mpr.dll来解决isse
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2(string name, int flags, bool force);
[StructLayout(LayoutKind.Sequential)]
public class NetResource
{
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;
}
public enum ResourceScope : int
{
Connected = 1,
GlobalNetwork,
Remembered,
Recent,
Context
};
public enum ResourceType : int
{
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,
}
public enum ResourceDisplaytype : int
{
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b
}
void ConnectToNas(string username, string password, string networkName){
NetworkCredential cred = new NetworkCredential(username, password);
var netResource = new NetResource
{
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName
};
var result = WNetAddConnection2(netResource, credentials.Password,userName,0);
if (result != 0)
{
throw new Win32Exception(result, "Error while connection to NAS");
}
}用于关闭连接:
void CloseConnection(string networkName){
WNetCancelConnection2(networkName, 0, true);
}通过这种方式,我能够连接到NAS,并检查它是否可读和可写,方法是写入一个文件,读取它,然后删除它。
https://stackoverflow.com/questions/56455181
复制相似问题