允许在 ApiControllers 上匿名应用
Allows anonymous application on ApiControllers
我正在使用 C# 和 .NET Framework 4.5.1 中的 Web Api 服务开发 ASP.NET MVC 5 应用程序。
我在 Web.config
上启用了 <authentication mode="Windows" />
并在 ASP.NET MVC 控制器上添加了以下内容:
[Authorize(Roles = @"MyDomain\MyRole")]
public class ReportController : Controller
但我不需要 Windows 在 ApiController
上进行身份验证。如何启用对 ApiController
的匿名访问?
要为网络执行此操作api,您可以在 Web.Config
中使用以下内容
<location path="api">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
这是说对于 api 控制器,允许任何用户(用 ? 表示)。如果您想做特定的控制器,可能值得将其更改为更具体。
它还假定您拒绝所有匿名用户的访问并且您的 web.config 身份验证是这样的
<system.web> <authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization> </system.web>
但是如果你想在 MVC 控制器上允许它,你可以使用
[AllowAnonymous] 属性。
相关:
Use Anonymous authentication in MVC4 on single controller when the whole application uses Windows Authenticaion
Disable Windows Authentication for WebAPI
Windows Authentication for ASP.NET MVC 4 - how it works, how to test it
补充阅读:
我正在使用 C# 和 .NET Framework 4.5.1 中的 Web Api 服务开发 ASP.NET MVC 5 应用程序。
我在 Web.config
上启用了 <authentication mode="Windows" />
并在 ASP.NET MVC 控制器上添加了以下内容:
[Authorize(Roles = @"MyDomain\MyRole")]
public class ReportController : Controller
但我不需要 Windows 在 ApiController
上进行身份验证。如何启用对 ApiController
的匿名访问?
要为网络执行此操作api,您可以在 Web.Config
中使用以下内容<location path="api">
<system.web>
<authorization>
<allow users="?"/>
</authorization>
</system.web>
</location>
这是说对于 api 控制器,允许任何用户(用 ? 表示)。如果您想做特定的控制器,可能值得将其更改为更具体。
它还假定您拒绝所有匿名用户的访问并且您的 web.config 身份验证是这样的
<system.web> <authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization> </system.web>
但是如果你想在 MVC 控制器上允许它,你可以使用 [AllowAnonymous] 属性。
相关: Use Anonymous authentication in MVC4 on single controller when the whole application uses Windows Authenticaion
Disable Windows Authentication for WebAPI
Windows Authentication for ASP.NET MVC 4 - how it works, how to test it
补充阅读: