ASP.NET MVC5:在单个页面上关闭Windows身份验证

Mar*_*tin 3 c# asp.net authentication asp.net-mvc powershell

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

Web.config文件:

[...]
<authentication mode="Windows" />
<authorization>
  <deny users="?" />
</authorization>
[...]
Run Code Online (Sandbox Code Playgroud)

我有一个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);
        }            

    }
Run Code Online (Sandbox Code Playgroud)

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)
Run Code Online (Sandbox Code Playgroud)

当我的脚本到达send方法时,他向我询问凭据,我不想每次都提供凭据(而且我不想将我的凭据放在脚本中).这就是我希望在此Action上禁用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

我在FilterConfig上添加了它,并将CustomAuthenticationAttribute放在我的Action ReceiveData上.没有更好的结果 ......所有操作都在此过滤器中,但我无法在我的操作上禁用Windows身份验证.

我的修改动作:

[AllowAnonymous]
[CustomAuthenticationAttribute]
public ActionResult ReceiveData(string scriptVersion, string content) {
    [...]
}
Run Code Online (Sandbox Code Playgroud)

我不是专家,如果你能帮助我找到解决方案并理解它,那将会很酷.在此先感谢并抱歉英语错误,这不是我的母语.

bea*_*ker 6

这就是我做到的方式:

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

    <section name="anonymousAuthentication" overrideModeDefault="Deny" />
    
    Run Code Online (Sandbox Code Playgroud)
  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>
    
    Run Code Online (Sandbox Code Playgroud)
  11. 保存存档
  12. 回收应用程序池,以确保IIS重新读取 web.config

更新:

我修改了.config文件.它只适用于本地IIS吗?

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

我是否必须在"非开发"环境中执行此更改?

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

关于代码:我使用MVC,所以我没有任何.aspx页面.我将路径更改为MyController/ReceiveDate,是否正确?

大概.<location path=>必须是"相对虚拟路径",这是"当前请求的脚本或路径的根相对虚拟路径"

我没有"回收应用程序池",因为我真的不知道如何做到这一点:我使用IIS Visual Studio.

不需要回收应用程序池,如果IIS检测到web.config更改,则应自行回收它.但很少发生或发生明显的延迟.

在使用您的解决方案测试我的PowerShell脚本后,出现错误500.19.

什么是HRESULT错误的代码?也许你错误的输入了什么web.config?有关详细信息,请参阅此文章:打开IIS 7.0网页时出现"HTTP Error 500.19"错误