使用C#在Windows中访问受密码保护的网络驱动器?

因此,在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

     }

}

但是我收到以下错误:


System.IO.IOException : Logon failure: unknown user name or bad password.

我知道了它的原因,因为我需要提供一些网络凭据,但是我不确定如何在这种情况下使用它们。


有谁知道最好的方法(或任何方法)来访问密码保护位置上的这些文件?


提前致谢!!


MMMHUHU
浏览 550回答 2
2回答

大话西游666

这是我修改代码的方式:using System;using System.Runtime.InteropServices;/// <summary>/// Implements P/Invoke Interop calls to the operating system./// </summary>internal static class NativeMethods{&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// The type of logon operation to perform.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; internal enum LogonType : int&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type is intended for users who will be interactively&nbsp; &nbsp; &nbsp; &nbsp; /// using the computer, such as a user being logged on by a&nbsp; &nbsp; &nbsp; &nbsp; /// terminal server, remote shell, or similar process.&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type has the additional expense of caching logon&nbsp; &nbsp; &nbsp; &nbsp; /// information for disconnected operations; therefore, it is&nbsp; &nbsp; &nbsp; &nbsp; /// inappropriate for some client/server applications, such as a&nbsp; &nbsp; &nbsp; &nbsp; /// mail server.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Interactive = 2,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type is intended for high performance servers to&nbsp; &nbsp; &nbsp; &nbsp; /// authenticate plaintext passwords.&nbsp; &nbsp; &nbsp; &nbsp; /// The LogonUser function does not cache credentials for this&nbsp; &nbsp; &nbsp; &nbsp; /// logon type.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Network = 3,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type is intended for batch servers, where processes&nbsp; &nbsp; &nbsp; &nbsp; /// may be executing on behalf of a user without their direct&nbsp; &nbsp; &nbsp; &nbsp; /// intervention.&nbsp; This type is also for higher performance servers&nbsp; &nbsp; &nbsp; &nbsp; /// that process many plaintext authentication attempts at a time,&nbsp; &nbsp; &nbsp; &nbsp; /// such as mail or Web servers.&nbsp; &nbsp; &nbsp; &nbsp; /// The LogonUser function does not cache credentials for this&nbsp; &nbsp; &nbsp; &nbsp; /// logon type.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Batch = 4,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Indicates a service-type logon.&nbsp; The account provided must have&nbsp; &nbsp; &nbsp; &nbsp; /// the service privilege enabled.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Service = 5,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type is for GINA DLLs that log on users who will be&nbsp; &nbsp; &nbsp; &nbsp; /// interactively using the computer.&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type can generate a unique audit record that shows&nbsp; &nbsp; &nbsp; &nbsp; /// when the workstation was unlocked.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Unlock = 7,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type preserves the name and password in the&nbsp; &nbsp; &nbsp; &nbsp; /// authentication package, which allows the server to make&nbsp; &nbsp; &nbsp; &nbsp; /// connections to other network servers while impersonating the&nbsp; &nbsp; &nbsp; &nbsp; /// client.&nbsp; A server can accept plaintext credentials from a&nbsp; &nbsp; &nbsp; &nbsp; /// client, call LogonUser, verify that the user can access the&nbsp; &nbsp; &nbsp; &nbsp; /// system across the network, and still communicate with other&nbsp; &nbsp; &nbsp; &nbsp; /// servers.&nbsp; &nbsp; &nbsp; &nbsp; /// NOTE: Windows NT:&nbsp; This value is not supported.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; NetworkCleartext = 8,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// This logon type allows the caller to clone its current token&nbsp; &nbsp; &nbsp; &nbsp; /// and specify new credentials for outbound connections.&nbsp; The new&nbsp; &nbsp; &nbsp; &nbsp; /// logon session has the same local identifier but uses different&nbsp; &nbsp; &nbsp; &nbsp; /// credentials for other network connections.&nbsp; &nbsp; &nbsp; &nbsp; /// NOTE: This logon type is supported only by the&nbsp; &nbsp; &nbsp; &nbsp; /// LOGON32_PROVIDER_WINNT50 logon provider.&nbsp; &nbsp; &nbsp; &nbsp; /// NOTE: Windows NT:&nbsp; This value is not supported.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; NewCredentials = 9&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Specifies the logon provider.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; internal enum LogonProvider : int&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Use the standard logon provider for the system.&nbsp; &nbsp; &nbsp; &nbsp; /// The default security provider is negotiate, unless you pass&nbsp; &nbsp; &nbsp; &nbsp; /// NULL for the domain name and the user name is not in UPN format.&nbsp; &nbsp; &nbsp; &nbsp; /// In this case, the default provider is NTLM.&nbsp; &nbsp; &nbsp; &nbsp; /// NOTE: Windows 2000/NT:&nbsp; &nbsp;The default security provider is NTLM.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Default = 0,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Use this provider if you'll be authenticating against a Windows&nbsp; &nbsp; &nbsp; &nbsp; /// NT 3.51 domain controller (uses the NT 3.51 logon provider).&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; WinNT35 = 1,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Use the NTLM logon provider.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; WinNT40 = 2,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// Use the negotiate logon provider.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; WinNT50 = 3&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// The type of logon operation to perform.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; internal enum SecurityImpersonationLevel : int&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// The server process cannot obtain identification information&nbsp; &nbsp; &nbsp; &nbsp; /// about the client, and it cannot impersonate the client.&nbsp; It is&nbsp; &nbsp; &nbsp; &nbsp; /// defined with no value given, and thus, by ANSI C rules,&nbsp; &nbsp; &nbsp; &nbsp; /// defaults to a value of zero.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Anonymous = 0,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// The server process can obtain information about the client,&nbsp; &nbsp; &nbsp; &nbsp; /// such as security identifiers and privileges, but it cannot&nbsp; &nbsp; &nbsp; &nbsp; /// impersonate the client.&nbsp; This is useful for servers that export&nbsp; &nbsp; &nbsp; &nbsp; /// their own objects, for example, database products that export&nbsp; &nbsp; &nbsp; &nbsp; /// tables and views.&nbsp; Using the retrieved client-security&nbsp; &nbsp; &nbsp; &nbsp; /// information, the server can make access-validation decisions&nbsp; &nbsp; &nbsp; &nbsp; /// without being able to use other services that are using the&nbsp; &nbsp; &nbsp; &nbsp; /// client's security context.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Identification = 1,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// The server process can impersonate the client's security&nbsp; &nbsp; &nbsp; &nbsp; /// context on its local system.&nbsp; The server cannot impersonate the&nbsp; &nbsp; &nbsp; &nbsp; /// client on remote systems.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Impersonation = 2,&nbsp; &nbsp; &nbsp; &nbsp; /// <summary>&nbsp; &nbsp; &nbsp; &nbsp; /// The server process can impersonate the client's security&nbsp; &nbsp; &nbsp; &nbsp; /// context on remote systems.&nbsp; &nbsp; &nbsp; &nbsp; /// NOTE: Windows NT:&nbsp; This impersonation level is not supported.&nbsp; &nbsp; &nbsp; &nbsp; /// </summary>&nbsp; &nbsp; &nbsp; &nbsp; Delegation = 3&nbsp; &nbsp; }&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Logs on the user.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="userName">Name of the user.</param>&nbsp; &nbsp; /// <param name="domain">The domain.</param>&nbsp; &nbsp; /// <param name="password">The password.</param>&nbsp; &nbsp; /// <param name="logonType">Type of the logon.</param>&nbsp; &nbsp; /// <param name="logonProvider">The logon provider.</param>&nbsp; &nbsp; /// <param name="token">The token.</param>&nbsp; &nbsp; /// <returns>True if the function succeeds, false if the function fails.&nbsp; &nbsp; /// To get extended error information, call GetLastError.</returns>&nbsp; &nbsp; [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]&nbsp; &nbsp; [return: MarshalAs(UnmanagedType.Bool)]&nbsp; &nbsp; internal static extern bool LogonUser(&nbsp; &nbsp; &nbsp; &nbsp; string userName,&nbsp; &nbsp; &nbsp; &nbsp; string domain,&nbsp; &nbsp; &nbsp; &nbsp; string password,&nbsp; &nbsp; &nbsp; &nbsp; LogonType logonType,&nbsp; &nbsp; &nbsp; &nbsp; LogonProvider logonProvider,&nbsp; &nbsp; &nbsp; &nbsp; out IntPtr token);&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Duplicates the token.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="existingTokenHandle">The existing token&nbsp; &nbsp; /// handle.</param>&nbsp; &nbsp; /// <param name="securityImpersonationLevel">The security impersonation&nbsp; &nbsp; /// level.</param>&nbsp; &nbsp; /// <param name="duplicateTokenHandle">The duplicate token&nbsp; &nbsp; /// handle.</param>&nbsp; &nbsp; /// <returns>True if the function succeeds, false if the function fails.&nbsp; &nbsp; /// To get extended error information, call GetLastError.</returns>&nbsp; &nbsp; [DllImport("advapi32.dll", CharSet = CharSet.Unicode, SetLastError = true)]&nbsp; &nbsp; [return: MarshalAs(UnmanagedType.Bool)]&nbsp; &nbsp; internal static extern bool DuplicateToken(&nbsp; &nbsp; &nbsp; &nbsp; IntPtr existingTokenHandle,&nbsp; &nbsp; &nbsp; &nbsp; SecurityImpersonationLevel securityImpersonationLevel,&nbsp; &nbsp; &nbsp; &nbsp; out IntPtr duplicateTokenHandle);&nbsp; &nbsp; /// <summary>&nbsp; &nbsp; /// Closes the handle.&nbsp; &nbsp; /// </summary>&nbsp; &nbsp; /// <param name="handle">The handle.</param>&nbsp; &nbsp; /// <returns>True if the function succeeds, false if the function fails.&nbsp; &nbsp; /// To get extended error information, call GetLastError.</returns>&nbsp; &nbsp; [DllImport("kernel32.dll", CharSet = CharSet.Unicode, SetLastError = true)]&nbsp; &nbsp; [return: MarshalAs(UnmanagedType.Bool)]&nbsp; &nbsp; internal static extern bool CloseHandle(IntPtr handle);}其次是&nbsp; &nbsp; IntPtr token;&nbsp; &nbsp; if (!NativeMethods.LogonUser(&nbsp; &nbsp; &nbsp; &nbsp; this.userName,&nbsp; &nbsp; &nbsp; &nbsp; this.domain,&nbsp; &nbsp; &nbsp; &nbsp; this.password,&nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.LogonType.NewCredentials,&nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.LogonProvider.Default,&nbsp; &nbsp; &nbsp; &nbsp; out token))&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; throw new Win32Exception();&nbsp; &nbsp; }&nbsp; &nbsp; try&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; IntPtr tokenDuplicate;&nbsp; &nbsp; &nbsp; &nbsp; if (!NativeMethods.DuplicateToken(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; token,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; NativeMethods.SecurityImpersonationLevel.Impersonation,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out tokenDuplicate))&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new Win32Exception();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; try&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; using (WindowsImpersonationContext impersonationContext =&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; new WindowsIdentity(tokenDuplicate).Impersonate())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Do stuff with your share here.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; impersonationContext.Undo();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; finally&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (tokenDuplicate != IntPtr.Zero)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!NativeMethods.CloseHandle(tokenDuplicate))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Uncomment if you need to know this case.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ////throw new Win32Exception();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; finally&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; if (token != IntPtr.Zero)&nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (!NativeMethods.CloseHandle(token))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Uncomment if you need to know this case.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ////throw new Win32Exception();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }

慕桂英546537

在MSDN上阅读advapi32.dll仅用于向本地计算机进行身份验证之后,我也一直在与这个问题作斗争。我几乎忽略了所有使用此方法的解决方案,但不能忽略有多少解决方案。我认为重复令牌的密钥是将LogonType传递给第一个调用,如果您阅读此类型的注释,则传递“ NewCredentials”-“此登录类型允许调用方克隆其当前令牌并为出站连接指定新的凭据”,因此第二部分必须是出站连接的凭据。
打开App,查看更多内容
随时随地看视频慕课网APP