gpt4 book ai didi

c# - ASP.NET MVC5 : Turn off Windows Authentication on a single page

转载 作者:行者123 更新时间:2023-11-30 14:27:16 25 4
gpt4 key购买 nike

我的网站(带有 Entity Framework 的 ASP.NET MVC5)使用 Windows 身份验证系统。它非常有用,使我能够通过搜索 Active Directory 获得有关用户的更多信息。我可以使用电子邮件等信息自动填写表单字段,...

Web.config:

[...]
<authentication mode="Windows" />
<authorization>
<deny users="?" />
</authorization>
[...]

我有一个 Powershell 脚本(做另一项工作),它必须访问网站的一个页面才能发送一些信息。我需要提供对脚本的访问权限,无需任何身份验证(仅为此操作/页面关闭 Windows 身份验证)。我最初使用的是属性 AllowAnonymous,它没有效果

我的 Controller 中的操作:

[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] and the new IAuthenticationFilterAllowAnonymous 属性似乎在添加新过滤器的 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 身份验证。

我修改的 Action :

[AllowAnonymous]
[CustomAuthenticationAttribute]
public ActionResult ReceiveData(string scriptVersion, string content) {
[...]
}

我不是专家,如果您能帮助我找到解决方案并理解它,那就太好了。提前致谢,对于英语错误,我深表歉意,这不是我的母语。

最佳答案

我就是这样做的:

  1. 运行 notepad作为管理员
  2. 打开%WINDIR%\System32\inetsrv\config\applicationHost.config
  3. 另存为 %WINDIR%\System32\inetsrv\config\applicationHost.config.bak用于备份目的
  4. 找到以下字符串:

    <section name="anonymousAuthentication" overrideModeDefault="Deny" />
  5. 替换DenyAllow
  6. 将文件另存为 %WINDIR%\System32\inetsrv\config\applicationHost.config
  7. 转到您的 Web 应用程序文件夹
  8. 打开web.config
  9. 找到 <configuration>
  10. 将此粘贴​​到您的 <configuration> 中部分:

    <!-- ANONYMOUS ACCESS FOR POWERSHELL SCRIPT -->
    <location path="ReceiveData.aspx">
    <system.webServer>
    <security>
    <authentication>
    <anonymousAuthentication enabled="true" />
    </authentication>
    </security>
    </system.webServer>
    </location>
  11. 保存文件
  12. 回收应用程序池,100% 确保 IIS 重新读取 web.config

更新:

I have modifed the .config file. Is it only available for local IIS?

是的,此方法使用 IIS 配置匿名身份验证。我无法帮助编写代码,因为我不是开发人员。也许您应该等待具有实际 C# 知识的人来回答。

Do I must to perform this change on my "non-development" environment?

是的,您必须在 %WINDIR%\System32\inetsrv\config\applicationHost.config 中更改全局设置能够覆盖 anonymousAuthentication在每个应用程序中 web.config .

About the code: I work with MVC, so i don't have any .aspx pages. I changed the path to MyController/ReceiveDate, is it correct?

可能吧。 <location path=>必须是“相对虚拟路径”,即 "root relative virtual path to the script or path for the current request"

I don't have "recycle the app pool" because I really don't know how to do this : I work with the IIS Visual Studio.

不需要回收应用程序池,如果 IIS 检测到 web.config,IIS 应该自行回收它变化。但它很少不发生或发生时有明显的延迟。

After testing my PowerShell script with your solution, I get an error 500.19.

什么是 HRESULT错误代码?也许您在 web.config 中输入了错误的内容?详情请看这篇文章:"HTTP Error 500.19" error when you open an IIS 7.0 Webpage

关于c# - ASP.NET MVC5 : Turn off Windows Authentication on a single page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33345989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com