如何通过ASP.NET MVC访问Program Files文件夹

问题描述:

我们必须从我们的ASP.NET MVC应用程序访问Program Files文件夹(用于读/写操作)。如何通过ASP.NET MVC访问Program Files文件夹

正如你所猜测; Windows不允许这样做。我尝试使用用户ASPNET(ASP.NET机器帐户)完全控制访问权限到.NET错误日志中建议的文件夹和文件,但它不起作用。

  • IIS:版本8.5.9600.16384 @的Windows 8.1 x64的
  • 管道模式:集成
  • 应用程序池:自定义(我们的定制应用程序池)

**这里是.NET错误登录:**

<!DOCTYPE html> 
 
<html> 
 

 
<head> 
 
    <title>Access to the path 'C:\Program Files (x86)\DENEME\DENEMETEXT.txt' is denied.</title> 
 
    <meta name="viewport" content="width=device-width" /> 
 
    <style> 
 
    body { 
 
     font-family: "Verdana"; 
 
     font-weight: normal; 
 
     font-size: .7em; 
 
     color: black; 
 
    } 
 
    p { 
 
     font-family: "Verdana"; 
 
     font-weight: normal; 
 
     color: black; 
 
     margin-top: -5px 
 
    } 
 
    b { 
 
     font-family: "Verdana"; 
 
     font-weight: bold; 
 
     color: black; 
 
     margin-top: -5px 
 
    } 
 
    H1 { 
 
     font-family: "Verdana"; 
 
     font-weight: normal; 
 
     font-size: 18pt; 
 
     color: red 
 
    } 
 
    H2 { 
 
     font-family: "Verdana"; 
 
     font-weight: normal; 
 
     font-size: 14pt; 
 
     color: maroon 
 
    } 
 
    pre { 
 
     font-family: "Consolas", "Lucida Console", Monospace; 
 
     font-size: 11pt; 
 
     margin: 0; 
 
     padding: 0.5em; 
 
     line-height: 14pt 
 
    } 
 
    .marker { 
 
     font-weight: bold; 
 
     color: black; 
 
     text-decoration: none; 
 
    } 
 
    .version { 
 
     color: gray; 
 
    } 
 
    .error { 
 
     margin-bottom: 10px; 
 
    } 
 
    .expandable { 
 
     text-decoration: underline; 
 
     font-weight: bold; 
 
     color: navy; 
 
     cursor: hand; 
 
    } 
 
    @media screen and (max-width: 639px) { 
 
     pre { 
 
     width: 440px; 
 
     overflow: auto; 
 
     white-space: pre-wrap; 
 
     word-wrap: break-word; 
 
     } 
 
    } 
 
    @media screen and (max-width: 479px) { 
 
     pre { 
 
     width: 280px; 
 
     } 
 
    } 
 
    </style> 
 
</head> 
 

 
<body bgcolor="white"> 
 

 
    <span><H1>Server Error in '/' Application.<hr width=100% size=1 color=silver></H1> 
 

 
      <h2> <i>Access to the path 'C:\Program Files (x86)\DENEME\DENEMETEXT.txt' is denied.</i> </h2></span> 
 

 
    <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif "> 
 

 
      <b> Description: </b>An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 
 

 
      <br><br> 
 

 
      <b> Exception Details: </b>System.UnauthorizedAccessException: Access to the path 'C:\Program Files (x86)\DENEME\DENEMETEXT.txt' is denied. 
 
<br><br>ASP.NET is not authorized to access the requested resource. Consider granting access rights to the resource to the ASP.NET request identity. ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or Network Service on IIS 6 and IIS 7, and the configured application pool identity on IIS 7.5) that is used if the application is not impersonating. If the application is impersonating via &lt;identity impersonate=&quot;true&quot;/&gt;, the identity will be the anonymous user (typically IUSR_MACHINENAME) or the authenticated request user. 
 
<br><br>To grant ASP.NET access to a file, right-click the file in File Explorer, choose &quot;Properties&quot; and select the Security tab. Click &quot;Add&quot; to add the appropriate user or group. Highlight the ASP.NET account, and check the boxes for the desired access.<br><br> 
 

 
      <b>Source Error:</b> <br><br> 
 

 
      <table width=100% bgcolor="#ffffcc"> 
 
       <tr> 
 
        <td> 
 
         <code> 
 

 
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.</code> 
 

 
        </td> 
 
       </tr> 
 
      </table> 
 

 
      <br> 
 

 
      <b>Stack Trace:</b> <br><br> 
 

 
      <table width=100% bgcolor="#ffffcc"> 
 
       <tr> 
 
        <td> 
 
         <code><pre> 
 

 
[UnauthorizedAccessException: Access to the path &#39;C:\Program Files (x86)\DENEME\DENEMETEXT.txt&#39; is denied.] 
 
    System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) +216 
 
    System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +1430 
 
    System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) +211 
 
    System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) +210 
 
    System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) +22 
 
    System.IO.StreamWriter..ctor(String path) +125 
 
    Publisher.Test.WebApp.Controllers.HomeController.Index() +118 
 
    lambda_method(Closure , ControllerBase , Object[]) +79 
 
    System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +242 
 
    System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +39 
 
    System.Web.Mvc.Async.AsyncControllerActionInvoker.&lt;BeginInvokeSynchronousActionMethod&gt;b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) +12 
 
    System.Web.Mvc.Async.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) +139 
 
    System.Web.Mvc.Async.AsyncInvocationWithFilters.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3d() +112 
 
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass46.&lt;InvokeActionMethodFilterAsynchronouslyRecursive&gt;b__3f() +452 
 
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass33.&lt;BeginInvokeActionMethodWithFilters&gt;b__32(IAsyncResult asyncResult) +15 
 
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass2b.&lt;BeginInvokeAction&gt;b__1c() +37 
 
    System.Web.Mvc.Async.&lt;&gt;c__DisplayClass21.&lt;BeginInvokeAction&gt;b__1e(IAsyncResult asyncResult) +241 
 
    System.Web.Mvc.Controller.&lt;BeginExecuteCore&gt;b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) +29 
 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111 
 
    System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53 
 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +19 
 
    System.Web.Mvc.MvcHandler.&lt;BeginProcessRequest&gt;b__5(IAsyncResult asyncResult, ProcessRequestState innerState) +51 
 
    System.Web.Mvc.Async.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) +111 
 
    System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +606 
 
    System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean&amp; completedSynchronously) +288 
 
</pre></code> 
 

 
        </td> 
 
       </tr> 
 
      </table> 
 

 
      <br> 
 

 
      <hr width=100% size=1 color=silver> 
 

 
      <b>Version Information:</b>&nbsp;Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.33440 
 

 
      </font> 
 

 
</body> 
 

 
</html> 
 
<!-- 
 
[UnauthorizedAccessException]: Access to the path &#39;C:\Program Files (x86)\DENEME\DENEMETEXT.txt&#39; is denied. 
 
    at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) 
 
    at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 
 
    at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share, Int32 bufferSize, FileOptions options, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost) 
 
    at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize, Boolean checkHost) 
 
    at System.IO.StreamWriter..ctor(String path, Boolean append, Encoding encoding, Int32 bufferSize) 
 
    at System.IO.StreamWriter..ctor(String path) 
 
    at Publisher.Test.WebApp.Controllers.HomeController.Index() 
 
    at lambda_method(Closure , ControllerBase , Object[]) 
 
    at System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) 
 
    at System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) 
 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<BeginInvokeSynchronousActionMethod>b__39(IAsyncResult asyncResult, ActionInvocation innerInvokeState) 
 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncResult`2.CallEndDelegate(IAsyncResult asyncResult) 
 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3d() 
 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.AsyncInvocationWithFilters.<>c__DisplayClass46.<InvokeActionMethodFilterAsynchronouslyRecursive>b__3f() 
 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass33.<BeginInvokeActionMethodWithFilters>b__32(IAsyncResult asyncResult) 
 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<>c__DisplayClass2b.<BeginInvokeAction>b__1c() 
 
    at System.Web.Mvc.Async.AsyncControllerActionInvoker.<>c__DisplayClass21.<BeginInvokeAction>b__1e(IAsyncResult asyncResult) 
 
    at System.Web.Mvc.Controller.<BeginExecuteCore>b__1d(IAsyncResult asyncResult, ExecuteCoreState innerState) 
 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
 
    at System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) 
 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
 
    at System.Web.Mvc.MvcHandler.<BeginProcessRequest>b__5(IAsyncResult asyncResult, ProcessRequestState innerState) 
 
    at System.Web.Mvc.Async.AsyncResultWrapper.WrappedAsyncVoid`1.CallEndDelegate(IAsyncResult asyncResult) 
 
    at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() 
 
    at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) 
 
-->

+0

你为什么要这么做?你可以解释吗? – Leo 2014-09-26 08:46:11

+0

我们正在使用某种在线语音识别系统。我们的第三方组件在程序文件中,并且不能在其他地方使用。这是我们应该做的唯一方式。所以我们的mvc应用程序必须访问程序文件...我知道它很烂。并不是最好的练习...... – 2014-09-26 08:49:34

尝试添加读/写权限˚F或:

IIS APPPOOL\*YOURAPPPOOLNAME* 
+0

谢谢你的回答。但没有运气......我尝试通过帐户添加权限:完全控制的“每个人”。现在正在工作。但是,添加每个人都不好,你知道...... – 2014-09-26 09:43:15

+0

@NAKRO是你在ApplicationPoolIdentity帐户下运行你的网站,并在集成模式和IIS 7+下运行吗? – 2014-09-26 09:47:50

+0

@NAKRO如果您从Windows上下文菜单添加权限并编写ISS APPPOOL \ applicationpoolname,然后单击checkNames,那么您应该为她的applicitonpoolname加下划线。另外不要忘记,您必须添加最低级别文件夹C:\ Program Files(x86)\ DENEME \ – 2014-09-26 09:56:57