C# 使用另一个用户凭据从远程路径获取文件到 FileStream

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12001972/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 20:02:35  来源:igfitidea点击:

Get file to FileStream from remote path with another user credentials

c#filestreamcredentials

提问by Tschareck

In my application I use FileStreamto read from a file, that is on the fileshare somewhere in the network. So my remoteFilePathvariable is something like: \\computername\folder\file.pdf

在我的应用程序中,我FileStream用来读取文件,该文件位于网络中某处的文件共享上。所以我的remoteFilePath变量是这样的:\\computername\folder\file.pdf

FileStream fileStream = new FileStream(remoteFilePath, FileMode.Open, FileAccess.Read, FileShare.None, 1024 * 1024)

Unfortunately, the user that I'm running this application with (that I'm logged into the PC with) does not have access to this fileshare. I have another user (domain, login & password), that has access to those files.

不幸的是,与我一起运行此应用程序的用户(我登录到 PC 时使用的用户)无权访问此文件共享。我有另一个用户(域、登录名和密码),可以访问这些文件。

Is it possible to use the other user credentials to get a file to filestream? Can I impersonate the user onlyto get a file, and then continue with my own user?

是否可以使用其他用户凭据将文件获取到文件流?我可以模拟用户只是为了获取文件,然后继续使用我自己的用户吗?

采纳答案by Tschareck

Thank you for your answers. Since the share was in another domain, it was not so easy to impersonate.

谢谢您的回答。由于共享位于另一个域中,因此冒充并不容易。

I found another, easier solution. I mapped a network drive, and checked the option Connect using different credentials. Then I connect to this drive instead of the remote path.

我找到了另一个更简单的解决方案。我映射了一个网络驱动器,并选中了选项Connect using different credentials。然后我连接到这个驱动器而不是远程路径。

string mappedFilePath = filePath.Replace(@"\computername\", @"Y:\")

and use this new string in FileStreamconstructor.

并在FileStream构造函数中使用这个新字符串。

回答by Cybermaxs

You should use impersonation. More info at http://msdn.microsoft.com/en-us/library/w070t6ka.aspx

你应该使用模拟。更多信息请访问http://msdn.microsoft.com/en-us/library/w070t6ka.aspx