【发布时间】:2014-03-26 23:32:25
【问题描述】:
我有一个超级简单的身份验证属性,我正在尝试在 ASP.NET MVC 5 应用程序中实现它,但遇到了一些麻烦。我希望全局应用该属性,但控制器中的特定操作除外(例如登录表单和主页)。
我尝试使用[OverrideAuthentication] 属性来装饰动作,但没有成功。它给了我一个重定向循环错误,因为应用程序仍在登录表单上运行身份验证,并不断尝试一遍又一遍地重定向回登录表单。
有没有其他人看到过这种行为?知道我在这里塞了什么吗?
例如,我创建了一个当前未实现的超级简单过滤器:
public class BasicAuthenticationAttribute
: ActionFilterAttribute, IAuthenticationFilter
{
public void OnAuthentication(AuthenticationContext filterContext)
{
throw new NotImplementedException();
}
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
{
throw new NotImplementedException();
}
}
如果我这样装饰我的控制器/动作:
[BasicAuthentication]
public class AccountController : Controller
{
[HttpGet]
[OverrideAuthentication]
public ActionResult Login()
{
return View();
}
}
当我导航到登录操作时,我得到一个未实现的异常,即使该操作根本不应该运行身份验证代码。我是否误解了覆盖应该如何工作?
【问题讨论】:
-
您不希望 [AllowAnonymous] 反对登录操作吗?
-
不,AllowAnonymous 是一个授权属性,它在身份验证已经发生之后发生,表示用户被验证为“匿名”类型的用户并被允许查看此页面。我所追求的是说根本不应该进行身份验证(我确定覆盖过滤器应该这样做)
-
你确定你的属性名是[OverrideAuthentication]???
标签: asp.net-mvc asp.net-mvc-5 custom-attributes