【问题标题】:ASP.NET MVC5 : Turn off Windows Authentication on a single pageASP.NET MVC5:在单个页面上关闭 Windows 身份验证
【发布时间】:2016-01-25 13:35:52
【问题描述】:

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

Web.config:

[...]
<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] and the new 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) {
    [...]
}

我不是专家,如果您能帮我找到解决方案并理解它,那就太好了。在此先感谢并为英语错误深表歉意,这不是我的母语。

【问题讨论】:

  • [AllowAnonymous] 应该可以工作您是否将任何 AuthorizeAttribute 放在控制器级别??
  • @too_cool 我想你的意思是AuthorizeAttribute?
  • @DavidG 谢谢指出!!..
  • @too_cool 是的,我愿意,但似乎并没有更好。我通过 AllowAnonymousAttribute 了解了 MVC4 和 5 之间的区别:bartwullems.blogspot.de/2015/06/… 但是公开的解决方案对我不起作用。

标签: c# asp.net asp.net-mvc powershell authentication


【解决方案1】:

我就是这样做的:

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

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

    <!-- 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

更新:

我已经修改了 .config 文件。是否仅适用于本地 IIS?

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

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

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

关于代码:我使用 MVC,所以我没有任何 .aspx 页面。一世 把路径改成了MyController/ReceiveDate,对吗?

大概吧。 &lt;location path=&gt; 必须是“相对虚拟路径”,即"root relative virtual path to the script or path for the current request"

我没有“回收应用程序池”,因为我真的不知道如何 这样做:我使用 IIS Visual Studio。

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

使用您的解决方案测试我的 PowerShell 脚本后,我收到错误消息 500.19。

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

【讨论】:

  • 我修改了 .config 文件。它仅适用于本地 IIS 吗?我是否必须在我的“非开发”环境中执行此更改?关于代码:我使用 MVC,所以我没有任何 .aspx 页面。我将路径更改为 MyController/ReceiveDate,是否正确?我没有“回收应用程序池”,因为我真的不知道该怎么做:我使用 IIS Visual Studio。使用您的解决方案测试我的 PowerShell 脚本后,我收到错误 500.19。这是关于你给我的代码:它不能覆盖父配置(?)。看来我必须回收以应用更改。但是怎么做呢?
  • 我使用这个解决方案来回收应用程序池:bbdevnetwork.com/blogs/… 但没有变化:该部分被锁定在父配置上。
  • @Martin 是的,看来这种方式不适合你。我已经根据您的 cmets 更新了我的答案,但看起来您确实需要基于代码的解决方案,而不是 IIS 配置级别的解决方案。
  • 感谢您的所有回答。错误 500.19 是关于我刚刚在 IIS 配置文件中更改的配置。添加到 web.config 的代码无法覆盖此父配置,因为它已被锁定。我不能给你确切的信息,因为它不是英文的:/
【解决方案2】:

除了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>

来源:Set up a public page in a website protected by Windows Authentication

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-12
    • 1970-01-01
    相关资源
    最近更新 更多