【问题标题】:ASP.NET MVC disable Windows IdentityASP.NET MVC 禁用 Windows 标识
【发布时间】:2015-02-02 14:14:40
【问题描述】:

我使用FormsAuthenticationTicket(带有表单)来授权具有 [Authorize] 属性的用户,当在 IIS 上允许匿名和表单身份验证都工作得很好时,但现在需要切换到 Windows 和表单 - 以禁用整个页面用于匿名访问。但是当我尝试通过 Windows 登录时,我可以像用户一样访问,因为我使用 User.Identity.IsAuthenticated 来检查用户是否登录。在这种情况下如何禁用 Windows 权限。

web.config

<authentication mode="Forms">
  <forms name="Auth" loginUrl="~/Account/Login" defaultUrl="~/" timeout="30"/>
</authentication> 

我考虑覆盖 Authorize 属性,但对 User.Identity.IsAuthenticated 没有帮助。谢谢X

UPD:与 User.Identity.Name 等同样的问题...

UPD2:我想到了一些类似这个属性的自定义属性:

public class LoggedAttribute : AuthorizeAttribute
{
    protected override bool AuthorizeCore(HttpContextBase httpContext)
    {
        return (base.AuthorizeCore(httpContext) && !IsWindows());
    }

    public static bool? _isWindows = null;

    public static bool IsWindows()
    {
        if (!_isWindowsAuth.HasValue)
        {
            if ((HttpContext.Current != null) && HttpContext.Current.User.Identity.IsAuthenticated)
            {
                _isWindows = new bool?(HttpContext.Current.User is WindowsPrincipal);
            }
            else
            {
                try
                {
                    AuthenticationSection section = (AuthenticationSection)WebConfigurationManager.OpenWebConfiguration(VirtualPathUtility.ToAbsolute("~")).GetSection("system.web/authentication");
                    _isWindows = new bool?(section.Mode == AuthenticationMode.Windows);
                }
                catch
                {
                    _isWindows = false;
                }
            }
        }
        return _isWindows.Value;
    }

}

【问题讨论】:

    标签: asp.net asp.net-mvc asp.net-membership membership-provider


    【解决方案1】:

    你想要的是这个:

    <authentication mode="Windows"/>
    

    这将在 User.Identity.Name 字段中使用 Windows 身份。但是,您应该知道,您不能真正同时使用 Windows 身份验证和匿名。您必须将任何给定资源的访问控制为非此即彼,尽管您可以指定哪些用户可以访问,哪些没有。

    【讨论】:

    • 可能你不完全理解我 - 对我来说需要使用表单身份验证(这是一个主要项目)但通过 Windows 权限隐藏所有站点,而不需要仅通过表单初始化此上下文 User.Identity.IsAuthenticated不是靠窗户。我以前没有同时使用匿名和 Windows - 现在当我将其更改为 Windows 时,User.Identity.IsAuthenticated 上下文有问题
    • @AleksP - 对不起,我只是不明白你想说什么。我不明白您使用表单身份验证但通过 Windows 权限隐藏站点是什么意思。您的意思是您希望用户必须输入他们的 Windows 凭据才能访问“表单身份验证”页面吗?如果是这样,您可能更适合将网站置于控制对其访问的反向代理服务器之后。
    • 我想逐步使用这两种身份验证 - 首先用户登录到服务器(所有页面都通过 Windows 身份验证隐藏) - 然后(在 Windows 登录后)他们看到主页(作为示例)但是如果他们有一个登录名和密码,他们可以通过 throw Login 并获得该站点的安全方面 - 但现在不可能,因为 User.Identity 由 Windows 身份验证填充 - 我如何禁用此上下文 User.Identity 以进行 Windows 身份验证但保存第一个通过 Windows 登录,然后通过表单登录
    • @AleksP - 我真的不明白你为什么需要他们拥有单独的凭据。只需根据他们的 Windows 身份授予他们基于角色的访问权限(访问他们有权访问的内容)。但正如我所说,这样做的方法是使用反向代理服务器。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多