c# - 尝试从 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 打开此文件(与运行失败应用程序的用户相同)。
有任何想法吗?
解决方案
您可以尝试以下方法(使用凭据)
using (new NetworkConnection(@"\\someservser\someshare", readCredentials)){
var s = File.ReadAllBytes(@"\\server\read\file\somefile.txt");
}
并检查此地址以获取 NetworkConnection 实现
编辑:对于“登录失败:用户尚未在此计算机上被授予请求的登录类型。” 错误,请尝试以下步骤
在 Telnet 服务器上,打开组策略管理编辑器。为此,请单击“开始”,然后在“开始搜索”框中,键入 gpedit.msc,然后按 Enter。
如果出现“用户帐户控制”对话框,请确认它显示的操作是您想要的,然后单击“继续”。
在导航窗格中,打开计算机配置、Windows 设置、安全设置、本地策略和用户权限分配。
在详细信息窗格中,双击允许本地登录。
单击添加用户或组。
另一种选择是授予每个用户访问权限,这样您就无需登录。
但最好的方法是为此特定目的创建一个本地帐户
推荐阅读
- javascript - 单击时将类添加到 ReactJS 中的多个对象
- javascript - app.use(bodyParser.urlencoded({extended: true })); 在快递js中做吗?
- javascript - 是否有用于查找一组事物的顺序的 javascript 函数
- android - 如何操作 onBackPressed 的结果?
- kubernetes - 如何在 Kubernetes 机密中生成随机字符串/密码
- typescript - 从一个 Cloud Firestore 文档中获取一个字段以指定在另一个 Cloud Function 中侦听更改(或写入)的位置
- c# - 我如何在 Unity for android 中操纵立方体
- visual-studio - 使用 Azure DevOps 从多项目 .NET 解决方案部署单个项目时 CICD 管道构建出错
- javascript - 对 SpringBoot 控制器的 AJAX 调用导致 CORS 错误
- php - 文件存储在我的 Laravel 项目中不起作用