首页 > 解决方案 > IIS Windows 身份验证 - 无法拒绝特定用户

问题描述

最近我开发了一个非常简单的 .net 核心 API,然后将其部署在 IIS 上,并希望为某些用户启用 Windows 身份验证。为了能够实现它,我的 web.config 看起来像

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
     <authentication mode="Windows" />
    <authorization>
      <allow users="Tow\USER1"/>
      <deny users="*"/>
    </authorization>
    </system.web>
    <location path="." inheritInChildApplications="false">
    <system.webServer>
      <handlers>
        <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModuleV2" resourceType="Unspecified" />
      </handlers>
      <aspNetCore processPath="dotnet" arguments=".\Oculus.WebApi.dll" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" hostingModel="InProcess" />
    </system.webServer>

  </location>

</configuration>

可以看出,应该只允许 User1 访问,但每个人都可以访问。我的 IIS 身份验证如下所示: 在此处输入图像描述

有人可以帮忙吗?

标签: apiasp.net-coreiiswindows-authentication

解决方案


从这个线程,ASP.NET Core 不支持也不使用 web.config。发布的 web.config 仅用于 IIS 托管,因为 IIS 需要它。

一个解决方法是您可以尝试放置在 system.webServer 中,它直接用于配置 IIS。

<configuration>
  <system.webServer>
    <security>
      <authorization>
          <remove users = "*" roles="" verbs="" />
          <add accessType = "Allow" users="Tow\USER1"/>
      </authorization>
    </security>
  </system.webServer>
</configuration>

但推荐的方式是你最好在 asp.net core 中编写你自己的自定义授权策略

https://docs.microsoft.com/en-us/aspnet/web-api/overview/security/authentication-and-authorization-in-aspnet-web-api

https://docs.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-2.2


推荐阅读