如何仅在ASP.NET核心子路径上使用Windows身份验证? [英] How to use Windows authentication on ASP.NET Core subpath only?
本文介绍了如何仅在ASP.NET核心子路径上使用Windows身份验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试仅在ASP.NET Core MVC应用程序中配置子路由上的Windows身份验证。
我的问题是当我添加
services.AddAuthentication().AddNegotiate()
我收到一个错误
无法在直接支持Windows身份验证的服务器上使用协商身份验证处理程序。
这导致我添加了web.config
,正如文档所解释的:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<location path="." inheritInChildApplications="false">
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="false" />
<windowsAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
</configuration>
错误就消失了。但是,现在对每个请求都弹出Windows身份验证。
我尝试将位置路径更改为.testendpoint
,但这会在基本路径上抛出原始错误。
那么,是否可以以及如何使/testendpoint
仅要求Windows身份验证,并且应用程序的其余部分将与我在ASP.NET Core应用程序中配置的任何其他身份验证一起工作?
推荐答案
为了通过Windows身份验证保护某个页面/操作方法,请在操作方法Authorize
属性中指定相应的身份验证方案。
[Authorize(AuthenticationSchemes = IISServerDefaults.AuthenticationScheme)]
public IActionResult UsingWindowsAuthentication()
确保在您的网站上启用了Windows authentication
。为了使用其他身份验证方案,例如个人帐户,还启用了匿名身份验证。
不得使用Windows身份验证的控制器和/或操作方法指定了默认方案。
例如,对于使用个人帐户身份验证类型作为默认身份验证方法的ASP.NET Core MVC
项目,即Identity.Application
。
[Authorize(AuthenticationSchemes = "Identity.Application")]
public IActionResult Index()
有关如何设置和配置多个身份验证方案的信息,请参阅documentation。
这篇关于如何仅在ASP.NET核心子路径上使用Windows身份验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文