我的 MVC 4.0/Razor 站点有问题。
这是我最近继承的(尚未启动的)公共网站。所有页面的 90% 应该可供所有人使用,其余页面供超级用户使用,需要身份验证。
这是通过面向公众的页面上的 AllowAnonymous 属性处理的,实现方式如下;
public class RequireAuthenticationAttribute : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
var skipAuthorization = filterContext.ActionDescriptor.IsDefined(typeof(AllowAnonymousAttribute), true) ||
filterContext.ActionDescriptor.ControllerDescriptor.IsDefined(
typeof(AllowAnonymousAttribute), true);
if (!skipAuthorization)
base.OnAuthorization(filterContext);
}
}
现在,问题是我想要一些面向公众的网站的自定义(为了争论,让我们假设某个地方有一个“当前登录:XYZ”标签)。我尝试的是使用 User.Identity,但在所有使用 AllowAnonymous、User.Identity.Name == "" 的页面上,即使超级用户确实登录了。(如果他将 url 更改为具有身份验证的页面,他再次登录,并且 User.Identity.Name 是正确的)。
有没有办法既使用允许匿名又跟踪谁登录?