我是否应该始终IsAuthenticated
在控制器上的操作中检查用户的属性,即使操作或整个控制器要求用户通过[Authorize]
属性登录?
这真的有必要还是只是一个好习惯?
例子:
[Authorize]
public class MyEntityController : Controller
{
public ActionResult Index()
{
if (WebSecurity.IsAuthenticated)
{
var result = from p in _db.MyEntity
where p.UserId.Equals(WebSecurity.CurrentUserId)
select new MyEntityViewModel
{
Id = p.Id,
Date = p.Date,
Description = p.Description,
Count = p.MyOtherEntity.Count(),
Username = WebSecurity.CurrentUserName
};
return View(result);
}
return View();
}
}