[英]Windows Service getting access denied exception while writing to Network Drive
I have an interactive windows service which run on a Local System account and with Interact with desktop checkbox checked(this is mandatory for my project as my service needs to invoke .exe with UI ). 我有一个在本地系统帐户上运行的交互式Windows服务,并且选中了“与桌面交互”复选框(这对我的项目是必不可少的,因为我的服务需要使用UI调用.exe)。 I am getting an exception as Access denied while writing to network drive.
我遇到异常,因为写入网络驱动器时访问被拒绝。 I am passing the UNC path from config file.
我正在从配置文件传递UNC路径。 i tried giving full control access to anonymous user on the folder which i want to access but its still not working.
我尝试将完全控制权限授予要访问的文件夹上的匿名用户,但该文件夹仍然无法正常工作。 i cannot run my windows service under Network service account or under any other account as suggested in some other posts because i want it interact with desktop check box checked.
我不能在网络服务帐户下或在其他一些帖子中建议的任何其他帐户下运行Windows服务,因为我希望它与桌面交互复选框处于选中状态。 is there any way to achieve this?
有什么办法可以做到这一点?
Edit: UNC path of network drive: //server/ABC/pqr 编辑:网络驱动器的UNC路径://服务器/ ABC / pqr
my service should create .txt file in pqr folder. 我的服务应在pqr文件夹中创建.txt文件。 should have access to delete it afterwords too.
也应该有权删除它的后记。
i have tried creating anonymous user for pqr folder and giving it full control but still i am getting access denied exception. 我曾尝试为pqr文件夹创建匿名用户并给予其完全控制权,但我仍无法访问被拒绝。 as i mentioned before i cannot run it under any other account other than local system account because it will automatically disable interact with desktop option in the properties of that service.
如前所述,我无法在本地系统帐户以外的任何其他帐户下运行它,因为它将自动禁用与该服务属性中的桌面选项交互。 is there any way to make it run under Network Service Account and still keep it interactive(interact with desktop option checked in the properties of service)?
有什么方法可以使其在“网络服务帐户”下运行并保持交互性(与在服务属性中选中的桌面选项进行交互)?
Try using the following nugget package named SimpleImpersonation 尝试使用以下名为SimpleImpersonation的块包
This way you could wrap the code you use to access your remote file location like this: 这样,您可以像这样包装用于访问远程文件位置的代码:
using (Impersonation.LogonUser(domain, username, password, logonType))
{
// do whatever you want as this user.
}
It worked for me. 它为我工作。 I used it to turn on and turn off a windows service remotely.
我用它来打开和关闭Windows服务。 Like this:
像这样:
await Task.Factory.StartNew(() =>
{
using (
Impersonation.LogonUser(serviceInfo.Domain, serviceInfo.User, serviceInfo.Pswd,
Environment.MachineName.Equals(serviceInfo.ComputerName,
StringComparison.InvariantCultureIgnoreCase)
? LogonType.Network
: LogonType.Interactive))
{
var service = new ServiceController(serviceInfo.ServiceName, serviceInfo.ComputerName);
if (service.Status == ServiceControllerStatus.Stopped)
{
service.Start();
service.WaitForStatus(ServiceControllerStatus.Running, TimeSpan.FromSeconds(60));
}
else
{
service.Stop();
service.WaitForStatus(ServiceControllerStatus.Stopped, TimeSpan.FromSeconds(60));
}
}
});
(the snippet was taken from the project site) (摘录来自项目现场)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.