当连接到当前用户(在我的情况下,支持网络的服务用户)没有权限的网络共享时,必须提供名称和密码.
我知道如何使用Win32函数(WNet*来自的家族mpr.dll),但是想用.Net(2.0)功能.
有哪些选择?
也许更多信息有助于:
所以在C#中我试图访问网络上的文件,例如"@applications/myapp/test.txt",如下所示:
const string fileLocation = @"//applications/myapp/test.txt";
using (StreamReader fin = new StreamReader(FileLocation))
{
while(!fin.EndOfStream()){
//Do some cool stuff with file
}
}
Run Code Online (Sandbox Code Playgroud)
但是我收到以下错误:
System.IO.IOException : Logon failure: unknown user name or bad password.
Run Code Online (Sandbox Code Playgroud)
我认为它是因为我需要提供一些网络凭证,但我不知道如何让这些在这种情况下工作.
有没有人知道访问受密码保护的位置上的这些文件的最佳方式(或任何方式)?
提前致谢!!
我正在编写控制台应用程序,它将文件从我的本地磁盘复制到文件服务器.此文件夹受用户名和密码保护.File.Copy()方法不起作用.它给出了权限错误.我看过这段代码
我试过了,但它不起作用.首先它是用VB编写的,但我已经将代码更改为C#,但是有一些错误.我不知道这个错误是什么意思.也许你可以告诉我其他方式应对文件到受保护的文件服务器
用简单的File.Copy(bla bla)它给了我"你没有许可"
当我将VB代码转换为C#时,它给出了以下错误:尝试读取或写入受保护的内存
我找到了解决方案
我在DomainA上运行ComputerA作为userA需要将一个非常大的文件复制到WorkgroupB上的ComputerB,其中ip为192.168.10.2到只有userB具有写访问权限的Windows共享.
没有netbios或dns解析因此必须通过IP来修复计算机
我先试试
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
WindowsIdentity UserB = new WindowsIdentity("192.168.10.2\\UserB", "PasswordB"); //Execption
WindowsImpersonationContext contex = UserB.Impersonate()
File.Copy(@"d:\bigfile", @"\\192.168.10.2\bifgile");
contex.Undo();
Run Code Online (Sandbox Code Playgroud)
但我得到一个System.Security.SecurityException"提供的名称不是一个正确形成的帐户名称."
所以我试过了
AppDomain.CurrentDomain.SetPrincipalPolicy(System.Security.Principal.PrincipalPolicy.WindowsPrincipal);
WindowsIdentity webinfinty = new WindowsIdentity("ComputerB\\UserB", "PasswordB"); //Execption
Run Code Online (Sandbox Code Playgroud)
但我得到"登录失败:未知的用户名或密码错误." 而是错误.
所以我试过
IntPtr token;
bool succeded = LogonUser("UserB", "192.168.10.2", "PasswordB", LogonTypes.Network, LogonProviders.Default, out token);
if (!succeded)
{
throw new Win32Exception(Marshal.GetLastWin32Error());
}
WindowsImpersonationContext contex = WindowsIdentity.Impersonate(token);
(...)
[DllImport("advapi32.dll", SetLastError = true)]
static extern bool LogonUser(
string principal,
string authority,
string password,
LogonTypes logonType,
LogonProviders logonProvider,
out IntPtr token); …Run Code Online (Sandbox Code Playgroud) 在c#2008中,我正在尝试将文件复制到目标路径(例如\newserver\destinationFolder),该路径可以位于另一个域中,也可以使用与当前用户不同的用户名/密码.如何在执行File.Copy(...)之前指定这些新的网络凭据?
谢谢!