Philip Johnson
Philip Johnson

Reputation: 1081

Mvc AllowAnonymous not working

I have two MVC 5 apps, both identical as far as I can tell (one was created from the other). However on one of them AllowAnonymous doesn't work.

If I register filters.Add(new System.Web.Mvc.AuthorizeAttribute()); in FilterConfig, then AllowAnonymous on a method doesn't work on my AccountController -> ResetPassword method, the system redirects to my login page.

If I remove this code and put [Authorize] on the AccountController class instead, everything works as you would expect.

I have not overridden any attributes.

So in summary, one project can handle the filters.Add, the other has to have Authorize on the controllers, because otherwise AllowAnonymous is ignored.

Any ideas?

Upvotes: 5

Views: 1244

Answers (1)

Philip Johnson
Philip Johnson

Reputation: 1081

I have now found the problem, and think it might be useful to share because when I was looking earlier there doesn't appear to be an answer to my exact problem.

The reason why I was experiencing the strange behaviour is on the project that didn't work, I had a layout page that had a couple of @Html.Action calls. These calls were to controllers that were authorised when the filter added the authorize attribute. So for AllowAnonymous to work on my method and get all the way through to displaying the page, AllowAnonymous also needed adding to the child methods that were called from Html.Action.

This was not immediately obvious because in debug, the main Action with AllowAnonymous was being called!

Hope this helps someone.

Upvotes: 7

Related Questions