简体   繁体   English

Windows 服务无法访问。网络位置 (UNC) 路径

[英]Windows Service cannot access network location (UNC) Path

I have a windows service that polls a folder continuously for new files.For local directories this works fine.But when it comes to UNC Paths on another system in the same.network,the service cannot access the folder it seems.I have refereed to this post https://serverfault.com/a/881272 ;我有一个 windows 服务,它不断地为新文件轮询一个文件夹。对于本地目录,这工作正常。但是当涉及到同一网络中另一个系统上的 UNC 路径时,该服务似乎无法访问该文件夹。我已经提到这篇文章https://serverfault.com/a/881272 ; it states that i need to run the service as the currently logged in user.How im i supposed to do that?它指出我需要以当前登录的用户身份运行该服务。我该怎么做?

The path i wish to monitor using the service is我希望使用该服务监控的路径是

\DESKTOP-PC\Users\me\myfolder

Please advice请指教

UPDATE:更新:

I have developed the service using topshelf.I want to poll a folder on another local machine in the same.network.I go to.network places,double click on the computername,it asks for credentials,i enter the username and password of the remote computer i get access to the files on the system for that user(in explorer).我已经使用 topshelf 开发了该服务。我想轮询同一网络中另一台本地计算机上的文件夹。我 go 到.network places,双击计算机名,它要求提供凭据,我输入的用户名和密码远程计算机我可以为该用户访问系统上的文件(在资源管理器中)。

Now when i set the service to run as the local machine user under this machine,it cannot access the remote UNC Path that is accessible using explorer.I have tried installing it as the remote machine user,but it fails.现在,当我将服务设置为在这台机器下以本地机器用户身份运行时,它无法访问可以使用资源管理器访问的远程 UNC 路径。我尝试以远程机器用户身份安装它,但它失败了。

myService.exe install -username:DESKTOP-REMOTE\myname -password:mypassword
  1. Open the Control Panel > Administrative Tools > Services window on your Windows server. 在Windows服务器上打开“控制面板”>“管理工具”>“服务”窗口。
  2. Stop your windows service. 停止你的Windows服务。
  3. Open the Properties > Log On dialog. 打开“属性”>“登录”对话框。
  4. Change the service user account to the target user account. 将服务用户帐户更改为目标用户帐户。
  5. Start your service. 开始你的服务。

在此输入图像描述

If you are developing the service in Visual Studio, and want to emulate a different account you can also press shift + right mouse click on devenv.exe and click on "run as different user". 如果您正在Visual Studio中开发该服务,并且想要模拟其他帐户,您还可以按shift +右键单击devenv.exe并单击“以不同用户身份运行”。

Also make sure you are specifying the full path to the folder and that the account you use has full rights to the folder. 还要确保指定文件夹的完整路径,并确保您使用的帐户具有该文件夹的完全权限。

If you try to run your service under the desktop-remote\\myname account you probably will fail. 如果您尝试在desktop-remote\\myname帐户下运行服务,则可能会失败。 The local machine only likes accounts belonging to itself. 本地机器只喜欢属于自己的帐户。

What I would do is to turn it around. 我要做的就是转过身来。 Make the remote computer share a folder for your machine to poll. 使远程计算机共享一个文件夹供您的计算机轮询。 That way the remote machine has control over which data it is publishing, just like an object has its properties to private or public . 这样,远程机器就可以控制它发布的数据,就像对象的privatepublic属性一样。 If you are ok with having anyone who knows the address \\\\desktop-remote\\my-not-so-secret-folder\\ being able to read it, set it so anyone can read. 如果你没有知道地址\\\\desktop-remote\\my-not-so-secret-folder\\能够读取它的人,那么设置它以便任何人都可以阅读。 If you are more secretive, suffix the folder with $ like so: \\\\desktop-remote\\my-secret-folder$ . 如果你更隐秘,请将文件夹后缀为$ like: \\\\desktop-remote\\my-secret-folder$ Now it is not visble but still reachable. 现在它不是可见但仍然可以到达。
You can also create a special account that has access to said folder. 您还可以创建一个可以访问所述文件夹的特殊帐户。

Then you let your service run any account (with network access) of your choosing on your local machine. 然后,您可以让您的服务在您的本地计算机上运行您选择的任何帐户(具有网络访问权限)。 Let it try to connect to the remote folder with the special account. 让它尝试使用特殊帐户连接到远程文件夹。

(I have learned, the hard way, that reading a shared folder is slightly different between machines without domain and machines in a domain; the anyone-can-read does not work. At least in WindowsXP it was that way. I have since then not have the need to share a folder.) (我学到了很难的方法,读取共享文件夹在域中没有域和机器的机器之间略有不同;任何人都可以读取不起作用。至少在WindowsXP中就是这样。从那时起我就一直这样。不需要共享文件夹。)

HTH HTH

I was facing this problem with my UNC storage in the Azure cloud.我在 Azure 云中的 UNC 存储遇到了这个问题。 What worked is setting up the credentials in the windows credentials manager:有效的是在 windows 凭据管理器中设置凭据:

windows credential manager windows 凭证管理员

You can get the connection string from the Azure portal.您可以从 Azure 门户获取连接字符串。

This resolved our problem.这解决了我们的问题。

When you set the service to run as the local system account, it cannot access the remote UNC Path that is accessible using explorer with local machine account. 将服务设置为以本地系统帐户运行时,它无法访问使用本地计算机帐户的资源管理器可访问的远程UNC路径。

Simply because the explorer manages logged on user credentials, so, you have added UNC path credentials to the LOGGED user only. 只是因为资源管理器管理登录的用户凭据,因此,您只将UNC路径凭据添加到LOGGED用户。

When you change it to Local System Account, the user credentials to the share is not stored. 将其更改为本地系统帐户时,不会存储共享的用户凭据。

Try to create a local user without password expiration and then enable it access to the Share. 尝试创建没有密码过期的本地用户,然后启用它访问共享。

All you will need to do is to setup windows service with this new account registered. 您需要做的就是在注册这个新帐户的情况下设置Windows服务。

安全

You can have a look at Impersonation. 你可以看一下模拟。 Also, if the service is running as a localuser, then it would help to create the same user with similar password on the machine where the folder resides. 此外,如果服务作为本地用户运行,则有助于在文件夹所在的计算机上创建具有类似密码的同一用户。

Have a look here (the OP in this post is experiencing the same issue and he/she has given the shared folder “Everyone access to full control”. 看看这里(这篇文章中的OP遇到了同样的问题,并且他/她已经给出了共享文件夹“Everyone访问完全控制”。

https://serverfault.com/questions/177139/windows-service-cant-access-network-share https://serverfault.com/questions/177139/windows-service-cant-access-network-share

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM