首页 > 解决方案 > 如何使用网络凭据连接网络中某处的共享文件夹?

问题描述

这是我的代码,我想知道如何在我的代码中使用网络凭据

 string filePath = Path.Combine(@"\\192.168.5.90\uploads", newfilename);                
     using (var filestream = new FileStream(filePath, FileMode.Create,FileAccess.Write))
     {
      await uploadfile.CopyToAsync(filestream);
     }
      return Ok(newfilename); 

标签: c#apiasp.net-core.net-corefilestream

解决方案


Windows 使用运行应用程序进程的用户的身份进行此身份验证。

您将需要模拟一个替代用户并在该模拟上下文中执行写入该文件的代码。查看WindowsIdentity.RunImpersonated方法 https://docs.microsoft.com/en-us/dotnet/api/system.security.principal.windowsidentity.runimpersonated?view=netcore-3.1


推荐阅读