也许很多朋友在做WEB项目的时候都会碰到这样一个需求:

当用户上传文件时,需要将上传的文件保存到另外一台专门的文件服务器。

要实现这样一个功能,有两种解决方案:

方案一、在文件服务器上新建一站点,用来接收上传的文件,然后保存。

方案二、将文件服务器的指定目录共享给WEB服务器,用来保存文件。

方案一不用多说,应该是很简单的了,将上传文件的FORM表单的ACTION属性指向文件服务器上的站点即可,我们来重点说下方案二。

也许你会说,其实方案二也很简单,在WEB服务器上做下磁盘映射,然后直接访问不就行了。其实不是这样的,IIS默认账户为NETWORK_SERVICE,该账户是没权限访问共享目录的,所以当你把站点部署到IIS上的时候,再访问映射磁盘就会报“找不到路径”的错误。所以,直接创建磁盘映射是行不通的,我们需要在程序中用指定账户创建映射,并用该账户运行IIS进程,下面来说下详细步骤及相关代码。

1、在文件服务器上创建共享目录,并新建访问账户。

比如共享目录为:\\192.168.0.9\share

访问账户为:user-1 密码为:123456

2、在WEB服务器上新建用户:user-1 密码为:123456,用户组选择默认的user组即可。

3、在WEB项目中新建公共类WNetHelper

  1. using System.Runtime.InteropServices;
  2. public class WNetHelper
  3. {
  4. [DllImport("mpr.dll", EntryPoint = "WNetAddConnection2")]
  5. private static extern uint WNetAddConnection2(NetResource lpNetResource, string lpPassword, string lpUsername, uint dwFlags);
  6. [DllImport("Mpr.dll", EntryPoint = "WNetCancelConnection2")]
  7. private static extern uint WNetCancelConnection2(string lpName, uint dwFlags, bool fForce);
  8. [StructLayout(LayoutKind.Sequential)]
  9. public class NetResource
  10. {
  11. public int dwScope;
  12. public int dwType;
  13. public int dwDisplayType;
  14. public int dwUsage;
  15. public string lpLocalName;
  16. public string lpRemoteName;
  17. public string lpComment;
  18. public string lpProvider;
  19. }
  20. /// <summary>
  21. /// 为网络共享做本地映射
  22. /// </summary>
  23. /// <param name="username">访问用户名(windows系统需要加计算机名,如:comp-1\user-1)</param>
  24. /// <param name="password">访问用户密码</param>
  25. /// <param name="remoteName">网络共享路径(如:\\192.168.0.9\share)</param>
  26. /// <param name="localName">本地映射盘符</param>
  27. /// <returns></returns>
  28. public static uint WNetAddConnection(string username, string password, string remoteName, string localName)
  29. {
  30. NetResource netResource = new NetResource();
  31. netResource.dwScope = 2;
  32. netResource.dwType = 1;
  33. netResource.dwDisplayType = 3;
  34. netResource.dwUsage = 1;
  35. netResource.lpLocalName = localName;
  36. netResource.lpRemoteName = remoteName.TrimEnd('\\');
  37. uint result = WNetAddConnection2(netResource, password, username, 0);
  38. return result;
  39. }
  40. public static uint WNetCancelConnection(string name, uint flags, bool force)
  41. {
  42. uint nret = WNetCancelConnection2(name, flags, force);
  43. return nret;
  44. }
  45. }

4、为IIS指定运行账户user-1

要实现此功能,有两种办法:

a) 在web.config文件中的<system.web>节点下,添加如下配置:<identity impersonate="true" userName="user-1" password="123456" />

b) 在WEB项目中添加公用类LogonImpersonate

  1. public class LogonImpersonate : IDisposable
  2. {
  3. static public string DefaultDomain
  4. {
  5. get
  6. {
  7. return ".";
  8. }
  9. }
  10. const int LOGON32_LOGON_INTERACTIVE = 2;
  11. const int LOGON32_PROVIDER_DEFAULT = 0;
  12. [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
  13. extern static int FormatMessage(int flag, ref   IntPtr source, int msgid, int langid, ref   string buf, int size, ref   IntPtr args);
  14. [System.Runtime.InteropServices.DllImport("Kernel32.dll")]
  15. extern static bool CloseHandle(IntPtr handle);
  16. [System.Runtime.InteropServices.DllImport("Advapi32.dll", SetLastError = true)]
  17. extern static bool LogonUser(
  18. string lpszUsername,
  19. string lpszDomain,
  20. string lpszPassword,
  21. int dwLogonType,
  22. int dwLogonProvider,
  23. ref   IntPtr phToken
  24. );
  25. IntPtr token;
  26. System.Security.Principal.WindowsImpersonationContext context;
  27. public LogonImpersonate(string username, string password)
  28. {
  29. if (username.IndexOf("\\") == -1)
  30. {
  31. Init(username, password, DefaultDomain);
  32. }
  33. else
  34. {
  35. string[] pair = username.Split(new char[] { '\\' }, 2);
  36. Init(pair[1], password, pair[0]);
  37. }
  38. }
  39. public LogonImpersonate(string username, string password, string domain)
  40. {
  41. Init(username, password, domain);
  42. }
  43. void Init(string username, string password, string domain)
  44. {
  45. if (LogonUser(username, domain, password, LOGON32_LOGON_INTERACTIVE, LOGON32_PROVIDER_DEFAULT, ref   token))
  46. {
  47. bool error = true;
  48. try
  49. {
  50. context = System.Security.Principal.WindowsIdentity.Impersonate(token);
  51. error = false;
  52. }
  53. finally
  54. {
  55. if (error)
  56. CloseHandle(token);
  57. }
  58. }
  59. else
  60. {
  61. int err = System.Runtime.InteropServices.Marshal.GetLastWin32Error();
  62. IntPtr tempptr = IntPtr.Zero;
  63. string msg = null;
  64. FormatMessage(0x1300, ref   tempptr, err, 0, ref   msg, 255, ref   tempptr);
  65. throw (new Exception(msg));
  66. }
  67. }
  68. ~LogonImpersonate()
  69. {
  70. Dispose();
  71. }
  72. public void Dispose()
  73. {
  74. if (context != null)
  75. {
  76. try
  77. {
  78. context.Undo();
  79. }
  80. finally
  81. {
  82. CloseHandle(token);
  83. context = null;
  84. }
  85. }
  86. }
  87. }

在访问映射磁盘之前首先调用此类为IIS更换运行用户:LogonImpersonate imper = new LogonImpersonate("user-1", "123456");

5、在访问共享目录前,调用WNetHelper.WNetAddConnection,添加磁盘映射

  1. public static bool CreateDirectory(string path)
  2. {
  3. uint state = 0;
  4. if (!Directory.Exists("Z:"))
  5. {
  6. state = WNetHelper.WNetAddConnection(@"comp-1\user-1", "123456", @"\\192.168.0.9\share", "Z:");
  7. }
  8. if (state.Equals(0))
  9. {
  10. Directory.CreateDirectory(path);
  11. return true;
  12. }
  13. else
  14. {
  15. throw new Exception("添加网络驱动器错误,错误号:" + state.ToString());
  16. }
  17. }

6、完成。

简洁代码就是:

LogonImpersonate imper = new LogonImpersonate("user-1", "123456");

WNetHelper.WNetAddConnection(@"comp-1\user-1", "123456", @"\\192.168.0.9\share", "Z:");
Directory.CreateDirectory(@"Z:\newfolder");

file1.SaveAs(@"Z:\newfolder\test.jpg");

ASP.NET访问网络驱动器(映射磁盘)的更多相关文章

  1. ASP.NET访问网络映射盘&实现文件上传读取功能

    最近在改Web的时候,遇到一个问题,要跨机器访问共享文件夹,以实现文件正常上传下载功能. 要实现该功能,可以采用HTTP的方式,也可以使用网络映射磁盘的方式,今天主要给大家分享一下使用网络映射磁盘的方 ...

  2. VBS映射网络驱动器 映射网络驱动器

    Dim objNetwork Set objNetwork = CreateObject("Wscript.Network") if objNetwork.EnumNetworkD ...

  3. iis访问网络路径映射问题(UNC share)

    最近在做一个功能,涉及到nas网络磁盘文件的保存和访问,在服务器上将对应的路径映射为Z盘,结果在iis上部署网站直接访问该路径,报无法找到该路径的错误. 用的是.net core开发,在vs直接启动程 ...

  4. IIS虚拟目录实现与文件服务器网络驱动器映射共享

    这篇文章转载别人,想原创作者致敬! 我本人也遇到同样的问题,故转载记录. 本文重点描述如何使用IIS访问共享资源来架设站点或执行 ASP.Net 等脚本. 通常情况下,拥有多台服务器的朋友在使用IIS ...

  5. 转:IIS虚拟目录实现与文件服务器网络驱动器映射共享

    这篇文章转载别人,想原创作者致敬! 我本人也遇到同样的问题,故转载记录. 本文重点描述如何使用IIS访问共享资源来架设站点或执行 ASP.Net 等脚本. 通常情况下,拥有多台服务器的朋友在使用IIS ...

  6. IIS下访问网络驱动器(网络位置)

    System.Diagnostics.Process p = new System.Diagnostics.Process(); p.StartInfo.FileName = "cmd.ex ...

  7. asp.net访问网络路径方法(模拟用户登录)

    public class IdentityScope : IDisposable { // obtains user token [DllImport("advapi32.dll" ...

  8. IIS/ASP.NET访问共享文件夹的可用方式

    [截止2014-10-14] 网上搜索了很多篇文章,所提及的总共有两种方式: 1.Asp.Net模拟登陆: 例如: 实战ASP.NET访问共享文件夹(含详细操作步骤) 实现一个2008serve的II ...

  9. 关于windows service不能访问网络共享盘(NetWork Drive)的解决方案

    我映射一个网络驱动器到本机的时候,发现本机的程序直接能访问读取网络驱动器,但是把本机的程序作为本机的windows服务运行的时候就不能访问了. Qt中的QDir::exist(folder)访问失败. ...

随机推荐

  1. 第八节:numpy之四则运算与逻辑运算

  2. BZOJ 1617 Usaco 2008 Mar. River Crossing渡河问题

    [题解] 显然是个DP题. 设$f[i]$表示送$i$头牛过河所需的最短时间,预处理出$t[i]$表示一次性送i头牛过河所需时间,那么我们可以得到转移方程:$f[i]=min(f[i],f[i-j]+ ...

  3. springcloud(四):Eureka客户端公共组件打包方式

    ,      一.前言  各位大佬应该知道,在大型项目中都需要有数据传输层,一般项目都采用的是MVC结构,如果有10个表,则会创建10个实体类,在各个层之间应该使用实体类传递数据: 在微服架构中,也许 ...

  4. 【Codeforces Global Round 1 C】Meaningless Operations

    [链接] 我是链接,点我呀:) [题意] 给你一个a 让你从1..a-1的范围中选择一个b 使得gcd(a^b,a&b)的值最大 [题解] 显然如果a的二进制中有0的话. 那么我们就让选择的b ...

  5. Excel 2010/2013/2016在鼠标右键新建xls或xlsx文件后,打开报错“无法打开文件”“文件格式或文件扩展名无效”

    近段时间,陆续有两个同事先后出现同样的问题(在Excel多个版本都可能出现),问题描述: 当用鼠标右键在任意文件夹或电脑桌面“新建”→“ Microsoft Excel 工作表”,再用鼠标双击打开这个 ...

  6. mybatis源码阅读-初始化六个工具(六)

    六个基本工具图集 图片来源:https://my.oschina.net/zudajun/blog/668596 ObjectFactory 类图 接口定义 public interface Obje ...

  7. 清北学堂模拟赛d2t4 最大值(max)

    题目描述LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目:这里有一个长度为n的正整数数列ai(下标为1~n).并且有一个参数k.你需要找两个正整数x,y,使得x+k<=y, ...

  8. springMVC入门笔记

    目录 一.回顾Servlet 二.SpringMVC简介 三.搭建SpringMVC第一个案例 四.简单流程及配置 五.使用注解开发Controller 六.参数绑定 基本数据类型的获取: 如果表单域 ...

  9. 在Hibernate中使用Memcached作为一个二级分布式缓存

    转自:http://www.blogjava.net/xmatthew/archive/2008/08/20/223293.html   hibernate-memcached--在Hibernate ...

  10. HTML5:去除IE10中输入框和密码框的X按钮和小眼睛

    在IE10和之后的IE版本中,当在输入框和密码框中输入的时候,后面会自动出现X按钮和小眼睛,如下图所示:  令人苦恼的是,这个效果只有IE才有,其它浏览器是没有这个功能的.为了统一,我们就需要去掉这个 ...