IIS中子线程对文件的读写的权限问题解决方案

 
1、模拟 IIS验证的帐户或用户

若要在收到 ASP.NET 应用程序中每个页的每个请求时模拟Microsoft Internet 信息服务 (IIS) 身份验证用户,必须在此应用程序的 Web.config 文件中包含<identity> 标记,并将 impersonate 属性设置为true

目前我找到的解决方法是通过System.Security.Principal.WindowsIdentity.Impersonate方法在子线程里模拟主线程的“windows账户标记”从而获得和主线程相同权限。下面是一段测试代码:

 
  1. protected void Page_Load(object sender, EventArgs e)
  2. {   
  3.     //先获得页面执行的主线程用户信息, 悟空注释,悟空的博客 www.7es.cn    
  4.     System.Security.Principal.WindowsIdentity obj = System.Security.Principal.WindowsIdentity.GetCurrent();
  5.     thread = new Thread(new ParameterizedThreadStart(proc));
  6.     thread.Start(obj);
  7. }
  8.  
  9. void proc(object obj)
  10. {
  11.     System.Security.Principal.WindowsIdentity wi = (System.Security.Principal.WindowsIdentity)obj;
  12.     
  13.     try
  14.     {
  15.         log.Write("test 0 start" + System.Security.Principal.WindowsIdentity.GetCurrent().Name);
  16.     }
  17.     catch (Exception ex)
  18.     {
  19.       System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "dbg.txt"
  20.       "这里可能无法写入日志,会访问错误异常的,只是表示一下此处没有权限写入,后面模拟账号之后才能获得权限");
  21.     }
  22.     
  23.     System.Security.Principal.WindowsIdentity.Impersonate(wi.Token); //模拟一下
  24.     
  25.     System.IO.File.AppendAllText(AppDomain.CurrentDomain.BaseDirectory + "dbg.txt"
  26.       "ok, 这里可以写入日志文件");
  27. }
这只是手写的测试代码,可能有出入手误的地方,具体自己根据代码自己修改吧。某些情况下可能还需要web.config里进行一些修改,当然,如果可以配置iis的话,这些问题都不用考虑了,完全可以通过iis配置给更高的权限解决读写文件问题。

2、为 ASP.NET应用程序的所有请求模拟特定用户

   若要为 ASP.NET 应用程序的所有页面上的所有请求模拟特定用户,可以在该应用程序的 Web.config 文件的<identity> 标记中指定 userName 和 password属性。例如:
<identity impersonate="true"userName="accountname" password="password"/>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值