I am relatively new to ASP.NET MVC and I am encountering the following piece of code very often:
[Authorize]
public ActionResult Index()
{
if (Request.IsAuthenticated)
// ...
}
Is the if-statement really necessary? Can somebody please explain the differences between the two?
The if check should not be needed. The [Authorize] attribute does that, in fact it does more by checking role membership as well. Check out the implementation of AuthorizeAttribute on GitHub to see how it works under the covers.
Authorize can check role membership. The if is redundant in this case as well.
That isn't a common pattern for ASP.NET MVC, you should ask who wrote the code.
This will just make testing a bit harder.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With