首页 > 解决方案 > 尝试从 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, int, System.IO.FileOptions, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES, string, bool, bool, bool) in filestream.cs
    System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess, System.IO.FileShare, int, System.IO.FileOptions, string, bool, bool, bool) in filestream.cs
    System.IO.File.InternalReadAllBytes(string, bool) in file.cs

我可以毫无问题地从 Windows UI 打开此文件(与运行失败应用程序的用户相同)。
有任何想法吗?

标签: c#.netunc

解决方案


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

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

检查此地址以获取 NetworkConnection 实现

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

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

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

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

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

  • 单击添加用户或组。

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

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


推荐阅读