【问题标题】:IIS Conditional authentication with the Windows Authentication module带有 Windows 身份验证模块的 IIS 条件身份验证
【发布时间】:2014-04-17 09:57:32
【问题描述】:

我有一种不寻常的情况,在这种情况下不需要身份验证,但在某些情况下通过 Windows 身份验证学习用户 ID 会很有用。

为了给出一些上下文,我希望能够在用户代理匹配某些条件时要求 Windows 身份验证,但在其他条件下不需要身份验证。仅对 asp.net 和 iis 有一些了解,我怀疑我错过了一种简单的方法来实现这一点。到目前为止,我已经考虑编写一个模块来检查用户代理,然后在满足条件时添加 WindowsAuthenticationModule - 但我不知道如何做到这一点。

对用户代理的价值进行身份验证或不进行身份验证的最佳方式有什么建议吗?

【问题讨论】:

    标签: asp.net iis windows-authentication


    【解决方案1】:

    如果您将 IIS 设置为使用 Windows 身份验证,您应该能够执行以下代码 sn-p 之类的操作。

    但是,正如您可能从代码中的 cmets 猜到的那样,我建议您不要这样做。用户代理很容易被欺骗 - 因此您基于它执行的任何身份验证检查也可以很容易地被绕过。几乎所有在 http 标头中遇到的 任何东西 也是如此(例如,基于 http 引用者的身份验证也是一个坏主意)。

    string windowsUserName = null;
    
    var currentContext = System.Web.HttpContext.Current;
    
    //NOT SECURE - easily spoofed!
    if (currentContext.Request.UserAgent == "Some special user agent")
    {
        if (!currentContext.User.Identity.IsAuthenticated 
            || currentContext.User.Identity.AuthenticationType != "Windows")
        {
            throw new SecurityException(@"You are not authorized, but you can easily 
    hack this application by modifying the user agent that you send to the server.")
        }
    
        windowsUserName = User.Identity.Name;
    }
    

    简而言之,即使上述方法有效,也不要这样做。您确实需要彻底重新考虑如何验证您的应用程序。

    如果,正如您在问题的第一句话中所指出的,这纯粹是信息性的,那么 可能没问题(例如,如果它只是用于调试目的)。但是,它 是合适的,例如用于审核或限制对任何资源的访问,并且您必须非常小心,不要在任何真正的安全上下文中重用此代码。

    【讨论】:

    • 我没有错过基于请求标头的安全决策有多弱这一点。这是一个内容剪裁问题,而不是身份验证问题。我的问题被 stackoverflow 编辑器严重歪曲,以至于错过了我问题的初衷。伪代码...if special-user-agent then: if not authenticated then: - respond with http 401 by "invoking" the built-in WindowsAuthenticationModule else: - serve the content, tailored to the user-id else: - serve the content
    • .. 问题就变成了,如何在处理程序或模块中调用/要求/注入内置的身份验证模块。 WindowsAuthenticationModule 不允许子类。我认为如果可以将其子类化,就可以完成我的要求。顺便说一下内森,感谢您的回答。
    猜你喜欢
    • 2020-12-03
    • 1970-01-01
    • 1970-01-01
    • 2020-11-11
    • 1970-01-01
    • 2011-07-22
    • 2011-06-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多