Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Authorize and Request.IsAuthenticated

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?

like image 965
meilke Avatar asked Jun 12 '26 01:06

meilke


2 Answers

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.

like image 139
vcsjones Avatar answered Jun 15 '26 06:06

vcsjones


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.

like image 41
Daniel A. White Avatar answered Jun 15 '26 04:06

Daniel A. White