尝试从UNC共享中读取文件-“登录失败:未向用户授予此计算机上请求的登录类型”

问题描述

我在远程PC上具有文件共享,只读,来宾访问已打开。我正在尝试使用如下代码从桌面应用程序通过其UNC路径从共享中读取文件var bytes = File.ReadAllBytes(@"\\someservser\someshare\somefile.txt");,但出现以下异常:

System.IO.IOException
  HResult=0x80070569
  Message=logon failure: the user has not been granted the requested logon type at this computer.

  Source=<Cannot evaluate the exception source>
  StackTrace:
<Cannot evaluate the exception stack trace>

  This exception was originally thrown at this call stack:
    System.IO.__Error.WinIOError(int,string) in __error.cs
    System.IO.FileStream.Init(string,System.IO.FileMode,System.IO.FileAccess,int,bool,System.IO.FileShare,System.IO.FileOptions,Microsoft.Win32.Win32Native.Security_ATTRIBUTES,string,bool) in filestream.cs
    System.IO.FileStream.FileStream(string,bool) in filestream.cs
    System.IO.File.InternalReadAllBytes(string,bool) in file.cs

我可以从Windows用户界面打开该文件而没有任何问题(与运行失败的应用程序时使用的用户相同)。
有什么想法吗?

解决方法

您可以尝试以下方法(使用凭据)

using (new NetworkConnection(@"\\someservser\someshare",readCredentials)){
   var s = File.ReadAllBytes(@"\\server\read\file\somefile.txt");
}

check this address for NetworkConnection implementation

编辑:“登录失败:未在此计算机上授予用户所请求的登录类型。”错误,请尝试以下步骤

  • 在Telnet服务器上,打开组策略管理编辑器。去做 因此,请单击“开始”,然后在“开始搜索”框中,键入gpedit.msc,然后 然后按Enter。

  • 如果出现“用户帐户控制”对话框,请确认 它显示的是您想要的操作,然后单击“继续”。

  • 在导航窗格中,打开“计算机配置”,然后单击“ Windows”。 设置,安全设置,本地策略和用户权限 分配。

  • 在详细信息窗格中,双击“允许本地登录”。

  • 单击添加用户或组。

另一种选择是将访问权限授予每个用户,这样您就无需登录。

但最好的方法还是为此目的创建一个本地帐户