【问题标题】:C# REST API Service access to path deniedC# REST API 服务访问路径被拒绝
【发布时间】:2017-11-27 16:21:04
【问题描述】:

我对 Windows 环境还很陌生,为了让我的 c# API 在 IIS 上工作,我付出了很多努力。我只设法在我的 Web API 上执行 GET。

当我尝试做一个 POST 时,我得到了这个:

{"Message": "An error has occurred.",
"ExceptionMessage": "Access to the path 'C:\\Path\\To\\API' is denied.",
"ExceptionType": "System.UnauthorizedAccessException",
"StackTrace": "   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)\r\n   at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)\r\n   at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)\r\n   at MiningAPI.Controllers.ValuesController.Post(HttpRequestMessage request) in C:\\Users\\Tavernier\\Desktop\\MiningReport\\MiningAPI\\MiningAPI\\Controllers\\ValuesController.cs:line 90\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.<>c__DisplayClass10.<GetExecutor>b__9(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.<InvokeActionAsyncCore>d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.<ExecuteAsync>d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()"}

IUSRSIIS_IUSRS 的权限完全可以读取 Web API 文件夹中的文件。 我在 Windows 10 上,运行 IIS-10

更新:

当我通过 Visual Studio 启动 Web API 时,POST 正在工作。 (localhost:port, 现在是 192.168.1.30:port)

我尝试将我的文件夹移动到D:,将NETWORK SERVICE 添加到权限中。

【问题讨论】:

标签: c# asp.net iis asp.net-web-api iis-10


【解决方案1】:

正在使用的用户取决于您的应用程序池。如果您的应用程序使用的 IIS 应用程序池中没有特定标识,则默认值为“IIS AppPool\”(不带引号)。

请参阅https://www.iis.net/learn/manage/configuring-security/application-pool-identities 了解更多信息。

我建议应用程序池用户需要写入的任何文件夹都被明确授予权限。允许应用程序池用户完全写入权限,尤其是对代码目录的完全写入权限是不明智的,因为这可能会导致修改源(如果存在)。

检查权限的代码

这里有一些代码可以对权限进行基本检查。它不会找到用户所属的所有用户组,因此在扩展情况下不会工作。但是,对于使用默认标识的应用程序池,它们应该获取应用程序池名称或 BUILTIN\Users

Import-Module WebAdministration

$websites = (Get-ChildItem IIS:\Sites)

foreach ($website in $websites)
{
    $appPool = (Get-Item IIS:\AppPools\$($website.applicationPool))    
    Write-Output "Found $($website.name) with $($appPool.name) in mode $($appPool.processModel.identityType)";
    $folder = [System.Environment]::ExpandEnvironmentVariables($website.physicalPath);
    if ($appPool.processModel.identityType -eq 3)
    {
        $user = $appPool.processModel.userName;
        Write-Output "Using explicit username '$user'";
    } else {
        $user = "IIS AppPool\" + $appPool.name;
        Write-Output "Using application pool name '$user'";
    }

    $permission = (Get-Acl $Folder).Access | ?{$_.IdentityReference -match $User -or $_.IdentityReference -match "BUILTIN\\Users" } | Select IdentityReference,FileSystemRights
    If ($permission){
        $permission | % {Write-Host "User $($_.IdentityReference) has '$($_.FileSystemRights)' rights on folder $folder"}
    } Else {
        Write-Host "$User Doesn't have any permission on $Folder"
    }
}

在我的系统上,这给出了以下输出

Found Default Web Site with DefaultAppPool in mode ApplicationPoolIdentity
Using application pool name 'IIS AppPool\DefaultAppPool'
User BUILTIN\Users has 'ReadAndExecute, Synchronize' rights on folder C:\inetpub\wwwroot
User BUILTIN\Users has '-1610612736' rights on folder C:\inetpub\wwwroot

【讨论】:

  • 我的 Web 服务在 DefaultAppPool 下,就像我们第一次打开 IIS 时的 Default Web Site 一样。我刚刚在Basic Settings... 中注意到,当我点击Test Settings... 时,我得到了这个:Cannot verify access to path ('C:\Path\To\API)
  • 那么用户将是 IIS AppPool\DefaultAppPool 并且该用户需要使用文件资源管理器在目录 C:\Path\To\Api 上分配权限。如果您在工作域中,则需要确保在尝试添加用户时将位置设置为本地计算机。有关更多信息,请参阅上面的链接。
  • 我已经在你的链接中做了所有的事情,当我尝试connect as... 我得到The user name or password is incorrect. (Exception from HRESULT: 0x8007052E)The credentials used to access the specified physical path are invalid. Verify the credentials and test these settings again.
  • 我用IIS AppPool\DefaultAppPool有用户,没有密码。它位于良好的池中,并且为DefaultAppPool 用户提供了完整的写入。
  • 使用 icacls 导出权限并仔细检查是否正确。如果需要,请私下发给我,我会检查一下。
【解决方案2】:

您的 WebAPI 尝试访问路径 C:\\Path\\To\\API。它代表无权访问它的 IIS 用户运行。尝试授予NETWORK_SERVICE 用户的访问权限。然后在 CMD 中执行iisreset 以防万一。

如果这不起作用,只需将您的项目移动到磁盘 D:,因为磁盘 C: 是系统磁盘,它通常具有更多限制。

【讨论】:

  • 我试过NETWORK_SERVICE并将文件移动到D:,但没有成功。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2016-07-19
  • 2010-11-29
  • 1970-01-01
  • 2016-11-11
相关资源
最近更新 更多