首页 文章

如何在ASP.NET MVC 3应用程序中禁用WEB API的Windows身份验证

提问于
浏览
1

我有一个带有Windows身份验证的ASP.NET MVC 3应用程序 . 现在我已经添加了web api(nuget包)并希望匿名访问(稍后我会添加一个api密钥) . 但我还没有设法让它发挥作用 .

我的所有WEB API控制器都在“/ api”路径下,并且没有一个具有Authorize属性 .

这是我当前的web.config(我删除了与此事无关的所有部分和碎片):

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <system.web>
    <authentication mode="Windows" />
    <httpRuntime maxRequestLength="102400" requestPathInvalidCharacters="&lt;,&gt;,*,:,&amp;,\" />
    <authorization>
      <deny users="?" />
    </authorization>
    <roleManager enabled="true" defaultProvider="AspNetSqlRoleProvider">
      <providers>
        <clear />
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="..." applicationName="..." />
      </providers>
    </roleManager>
    <profile enabled="false" defaultProvider="AspNetProfileProvider">
      <providers>
        <clear />
        <add name="AspNetProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="..." applicationName="..." />
      </providers>
      <properties></properties>
    </profile>
    <customErrors mode="RemoteOnly" defaultRedirect="~/Error/Generic">
      <error statusCode="404" redirect="~/Error/NotFound" />
      <error statusCode="403" redirect="~/Error/AccessDenied" />
    </customErrors>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <security>
      <requestFiltering allowDoubleEscaping="true"></requestFiltering>
    </security>
  </system.webServer>
  <location path="Error">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="Scripts">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="Content">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
  <location path="api">
    <system.web>
      <authorization>
        <allow users="*" />
      </authorization>
    </system.web>
  </location>
</configuration>

我的所有请求都是“HTTP错误401.2 - 未经授权”,但当我输入一些有效的Windows凭据时,请求成功执行 .

那么,如何禁用WEB API的Windows身份验证?提前致谢 .

1 回答

  • 0

    我能够在我的MVC项目属性中修复此问题并启用匿名身份验证 .

相关问题