【发布时间】: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