我正在使用登录凭据访问其他服务器。我的问题是,如果我最初运行代码,它将显示错误为
Logon failure: unknown user name or bad password
但是如果我通过命令提示符连接到服务器一次后尝试运行代码。然后,该应用程序可以正常工作,并且不会引发任何错误。因此,每天我都需要通过命令提示符连接到服务器一次,以便运行应用程序而不会出错。
这是我的代码:
static void main()
{
string sourceDir = "//server.domain.mhc//drive";
string DestinationDir = "D:\\Test";
DirectoryCopy(sourceDir, DestinationDir, true);
}
[DllImport("advapi32.DLL", SetLastError = true)]
public static extern int LogonUser(string lpszUsername, string lpszDomain, string lpszPassword, int dwLogonType, int dwLogonProvider, ref IntPtr phToken);
private static void DirectoryCopy(string sourceDirName, string destDirName, bool copySubDirs)
{
clsEmail objEmail = new clsEmail();
try
{
IntPtr admin_token = default(IntPtr);
if(LogonUser("myusername","domain","pwd",9,0,ref admin_token) != 0)
{
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
}
c#大神给出的解决方案
找到解决方案。请参阅更新的代码以获取答案。
try
{
IntPtr admin_token = default(IntPtr);
//Added these 3 lines
WindowsIdentity wid_current = WindowsIdentity.GetCurrent();
WindowsIdentity wid_admin = null;
WindowsImpersonationContext wic = null;
if(LogonUser("myusername","domain","pwd",9,0,ref admin_token) != 0)
{
//Newly added lines
wid_admin = new WindowsIdentity(admin_token);
wic = wid_admin.Impersonate();
DirectoryInfo dir = new DirectoryInfo(sourceDirName);
DirectoryInfo[] dirs = dir.GetDirectories();
}
}
当回复有时是一个对象有时是一个数组时,如何在使用改造时解析JSON回复? - java我正在使用Retrofit来获取JSON答复。这是我实施的一部分-@GET("/api/report/list") Observable<Bills> listBill(@Query("employee_id") String employeeID); 而条例草案类是-public static class…
提交表单后显示模式对话框 - php提交下载文件后,我有一张表格。我要自动而不是自动下载文件..以显示模态对话框并显示下载链接。<form name="softwareform" id="softwareform" action="../downloadlink.php" method="POST" alig…
DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…
在Map中,如果我们使用现有键进行修改,则不会获得ConcurrentModificationException - java我有以下代码,我希望从情况2的情况下抛出ConcurrentModificationException,但它运行成功。据我所知,如果我对地图中的单个键执行相同的操作,则不会抛出异常,因为here但是当我重现这种具有两个案例的多个密钥的场景时,通过新密钥修改。通过现有密钥进行修改。情况1: Map<String,String> mp = new H…
字符串文字中的正斜杠表现异常 - python为什么S1和S2在撇号位置方面表现不同?S1="1/282/03/10" S2="4/107/03/10" R1="".join({"N\'" ,S1,"\'" }) R2="".join({"N\'…