C# 创建网络共享不同的凭据覆盖
我正在用C#编写一个windows应用程序。我有一个要求,即上传到windows窗体的文件要保存到网络共享中,但并非所有用户都可以访问网络共享(\\FileServer\SharedFolder)。只有一个用户(FileWriter)对此文件夹具有读/写/执行权限。当前用户EmployeeUser对此共享没有任何权限。我通过打开Start->Run\\FileServer\SharedFolder验证了这一点。这将给出一个“拒绝访问”错误框 我使用了,使用C# 创建网络共享不同的凭据覆盖,c#,credentials,network-shares,C#,Credentials,Network Shares,我正在用C#编写一个windows应用程序。我有一个要求,即上传到windows窗体的文件要保存到网络共享中,但并非所有用户都可以访问网络共享(\\FileServer\SharedFolder)。只有一个用户(FileWriter)对此文件夹具有读/写/执行权限。当前用户EmployeeUser对此共享没有任何权限。我通过打开Start->Run\\FileServer\SharedFolder验证了这一点。这将给出一个“拒绝访问”错误框 我使用了,使用wnetadconnection2与Fi
wnetadconnection2
与FileWriter的不同凭据连接,使用file.Create保存文件Sample.txt。到目前为止一切正常。WNetCancelConnection2
被调用,我已经在代码调试中进行了验证,程序退出。现在,从当前用户开始,我打开了startmenus-->Run并键入\\FileServer\SharedFolder,即使windows用户是EmployeeUser,共享也会立即打开。我已经关闭了资源管理器,几分钟后(通过尝试随机更改),我打开了Start->Run\\FileServer\SharedFolder。现在它给出了一个拒绝访问错误框
我无法理解这一点,非常感谢您的帮助。
现在,在“拒绝访问”框之后,我用相同的步骤再次运行该程序,只是Sample.txt(使用File.Create)被静默覆盖。它不是应该给出一个文件存在错误吗?您可以改用模拟程序:
using (var impersonator = new Impersonator(username, password))
{
File.Copy(source, destination, true);
}
这是从我们的执行过去的副本,所以请调整您的域名
using System;
using System.Runtime.InteropServices;
using System.Security.Principal;
public class Impersonator : IDisposable
{
/// <summary>
/// The Impersonator class is used to access a network share with other credentials.
/// </summary>
private readonly WindowsImpersonationContext _impersonatedUser;
private readonly IntPtr _userHandle;
/// <summary>
/// Constructor
/// </summary>
/// <param name="username">The user of the network share</param>
/// <param name="password">The password of the network share</param>
public Impersonator(string username, string password, string userDomain = "YOURDOMAIN")
{
_userHandle = new IntPtr(0);
bool returnValue = LogonUser(username, userDomain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT,
ref _userHandle);
if (!returnValue)
throw new ApplicationException(
"The applications wasn't able to impersonate the user with the specified credentials!");
var newId = new WindowsIdentity(_userHandle);
_impersonatedUser = newId.Impersonate();
}
#region IDisposable Members
public void Dispose()
{
if (_impersonatedUser != null)
{
_impersonatedUser.Undo();
CloseHandle(_userHandle);
}
}
#endregion
#region Interop imports/constants
public const int LOGON32_LOGON_INTERACTIVE = 2;
public const int LOGON32_LOGON_SERVICE = 3;
public const int LOGON32_PROVIDER_DEFAULT = 0;
[DllImport("advapi32.dll", CharSet = CharSet.Auto)]
public static extern bool LogonUser(String lpszUserName, String lpszDomain, String lpszPassword, int dwLogonType,
int dwLogonProvider, ref IntPtr phToken);
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public static extern bool CloseHandle(IntPtr handle);
#endregion
}
使用系统;
使用System.Runtime.InteropServices;
使用System.Security.Principal;
公共类模拟程序:IDisposable
{
///
///模拟程序类用于使用其他凭据访问网络共享。
///
私有只读WindowsImpersonationContext\u impersonatedUser;
私有只读IntPtr\u userHandle;
///
///建造师
///
///网络共享的用户
///网络共享的密码
公共模拟程序(字符串用户名、字符串密码、字符串userDomain=“YOURDOMAIN”)
{
_userHandle=newintptr(0);
bool returnValue=LogonUser(用户名、用户域、密码、LOGON32\u登录\u交互、LOGON32\u提供程序\u默认、,
参考(用户句柄);
if(!returnValue)
抛出新的ApplicationException(
“应用程序无法使用指定的凭据模拟用户!”);
var newId=新的WindowsIdentity(_userHandle);
_impersonatedUser=newId.Impersonate();
}
#区域IDisposable成员
公共空间处置()
{
if(_impersonatedUser!=null)
{
_impersonatedUser.Undo();
CloseHandle(_userHandle);
}
}
#端区
#区域互操作导入/常量
public const int LOGON32\u LOGON\u INTERACTIVE=2;
public const int LOGON32\u LOGON\u SERVICE=3;
public const int LOGON32_PROVIDER_DEFAULT=0;
[DllImport(“advapi32.dll”,CharSet=CharSet.Auto)]
公共静态外部bool LogonUser(字符串lpszUserName、字符串lpszDomain、字符串lpszPassword、int-dwLogonType、,
int dwLogonProvider,ref IntPtr phToken);
[DllImport(“kernel32.dll”,CharSet=CharSet.Auto)]
公共静态外部布尔闭合手柄(IntPtr手柄);
#端区
}
调用时,WNetCancelConnection2
返回什么值?说明“如果指定的文件不存在,则创建该文件;如果该文件存在且不是只读的,则覆盖内容。”@stuartdWNetCancelConnection2
返回0。还感谢您提供文件。创建。我完全忘了,谢谢你。它工作得很好。现在,在使用块之后,EmployeeUser无法浏览\\FileServer\SharedFolder,即使立即尝试。您的解决方案确实解决了我的问题。不过,顺便说一句,我想知道WNetCancelConnection2
的问题,只是为了教育目的,如果不是为了其他目的,返回0,但允许EmployeeUser浏览\\FileServer\SharedFolder,如我的原始帖子所述。我不知道WNetCancelConnection2
,从未使用过它。很抱歉