[英]ASP.NET MVC5 : Turn off Windows Authentication on a single page
我的网站(带有实体框架的 ASP.NET MVC5)使用 Windows 身份验证系统。 它非常有用,使我能够通过搜索 Active Directory 来获取有关用户的更多信息。 我可以使用电子邮件等信息自动填写表单字段...
网页配置:
[...]
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
[...]
我有一个 Powershell 脚本(执行另一项工作),它必须访问站点的一个页面才能发送一些信息。 我需要在没有任何身份验证的情况下提供对脚本的访问权限(仅为此操作/页面关闭 Windows 身份验证)。 我首先使用了属性 AllowAnonymous,但没有效果。
我的控制器中的操作:
[AllowAnonymous]
public ActionResult ReceiveData(string scriptVersion, string content)
{
try
{
if (scriptVersion != null && content != null)
{
HttpUtility.UrlDecode(content);
Xxxxx.UpdatexxxxxxServer(content); // I just replaced the name of my project
return new HttpStatusCodeResult(200, "OK");
}
return new HttpStatusCodeResult(403, "You're not allowed to do this.");
}
catch(Exception e)
{
return new HttpStatusCodeResult(400, "Unable to execute this request on the DB. Detailed error: " + e.Message);
}
}
Powershell 脚本(仅供参考,无需为此问题理解它):
$url = "http://localhost:3349/Xxxxxx/ReceiveData"
$parameters = "scriptVersion=2&content=$([System.Web.HttpUtility]::UrlEncode($(Get-Content "$scriptFile")))"
$http_request = New-Object -ComObject Msxml2.XMLHTTP
$http_request.open('POST', $url, $false)
$http_request.setRequestHeader("Content-type", "application/x-www-form-urlencoded")
$http_request.setRequestHeader("Content-length", $parameters.length)
$http_request.setRequestHeader("Connection", "close")
$http_request.send($parameters)
当我的脚本到达发送方法时,他要求我提供凭据,而我不想每次都提供凭据(而且我不想将我的凭据放入脚本中)。 这就是为什么我希望在此操作上禁用 Windows 身份验证。
我做了一些研究,试图找到任何可行的解决方案。 我看到了这个主题: MVC 5.0 [AllowAnonymous] 和新的 IAuthenticationFilter AllowAnonymous 属性似乎在添加新过滤器的 MVC5 中不起作用。 这就是我尝试添加自定义过滤器的原因:
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
var user = filterContext.HttpContext.User;
if (user == null || !user.Identity.IsAuthenticated)
{
filterContext.Result = new HttpUnauthorizedResult();
}
}
}
我将它添加到 FilterConfig 并将 CustomAuthenticationAttribute 放在我的 Action ReceiveData 上。 没有更好的结果......所有操作都在此过滤器中进行,但我无法在我的操作上禁用 Windows 身份验证。
我修改的动作:
[AllowAnonymous]
[CustomAuthenticationAttribute]
public ActionResult ReceiveData(string scriptVersion, string content) {
[...]
}
我不是专家,如果你能帮我找到解决方案并理解它会很酷。 预先感谢并为英语错误感到抱歉,这不是我的母语。
我就是这样做的:
notepad
%WINDIR%\\System32\\inetsrv\\config\\applicationHost.config
%WINDIR%\\System32\\inetsrv\\config\\applicationHost.config.bak
以备备份找到以下字符串:
<section name="anonymousAuthentication" overrideModeDefault="Deny" />
Deny
替换为Allow
%WINDIR%\\System32\\inetsrv\\config\\applicationHost.config
web.config
<configuration>
部分将其粘贴到您的<configuration>
部分中:
<!-- ANONYMOUS ACCESS FOR POWERSHELL SCRIPT --> <location path="ReceiveData.aspx"> <system.webServer> <security> <authentication> <anonymousAuthentication enabled="true" /> </authentication> </security> </system.webServer> </location>
web.config
我修改了 .config 文件。 它是否仅适用于本地 IIS?
是的,这种方法使用 IIS 来配置匿名身份验证。 我无法帮助编写代码,因为我不是开发人员。 也许您应该等待具有实际 C# 知识的人来回答。
我是否必须在我的“非开发”环境中执行此更改?
是的,您必须更改%WINDIR%\\System32\\inetsrv\\config\\applicationHost.config
全局设置才能覆盖每个应用程序web.config
anonymousAuthentication
。
关于代码:我使用 MVC,所以我没有任何 .aspx 页面。 我将路径更改为 MyController/ReceiveDate,是否正确?
大概。 <location path=>
必须是“相对虚拟路径”,即“脚本的根相对虚拟路径或当前请求的路径”
我没有“回收应用程序池”,因为我真的不知道该怎么做:我使用 IIS Visual Studio。
不需要回收应用程序池,IIS 应该自行回收它,如果它检测到web.config
更改。 但很少它不会发生或发生明显的延迟。
使用您的解决方案测试我的 PowerShell 脚本后,我收到错误 500.19。
错误的HRESULT
代码是什么? 也许你在web.config
打错了一些东西? 有关详细信息,请参阅此文章:打开 IIS 7.0 网页时出现“HTTP 错误 500.19”错误
除了location
标记外,请确保将runAllManagedModulesForAllRequests
设置为true
。
完整的 web.config 更改以启用对测试文件夹的访问:
<configuration>
<system.web>
<authorization>
<allow users="*" />
<deny users="?" />
</authorization>
</system.web>
<location path="test”>
<system.webServer>
<security>
<authentication>
<anonymousAuthentication enabled="true" />
</authentication>
</security>
</system.webServer>
</location>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true" />
</system.webServer>
</configuration>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.