ASP.NET MVC Global authentication and AllowAnonymous
up vote
1
down vote
favorite
I'm fairly new to ASP.NET MVC and trying to understand global Authentication Filters and anonymous access.
I have a custom AuthenticationFilter as so:
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
public void OnAuthentication(AuthenticationContext filterContext)
//Do some stuff
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
!user.Identity.IsAuthenticated)
filterContext.Result = new HttpUnauthorizedResult();
...and I've added it in my FilterConfig.cs to become global, as so:
filters.Add(new CustomAuthorizationAttribute());
When the app loads, it gets stuck in a circular loop - the login page just keeps redirecting.
The login page has the AllowAnonymous attribute. AllowAnonymous skips the AuthorizationAttribute step, so understandably doesn't impact the Authentication Filter.
My questions are:
1) If the Authentication scope is to verify the user, surely AllowAnonymous should bypass the AuthenticationFilter step instead? Once you've passed authentication, you're no longer anonymous...please can someone explain what I'm missing?
2) If I want to lock down a site, am I on the right track to have a global Authentication Filter? If so, how do I get out of this infinite loop and allow the login page to be shown?
Thanks!
c# asp.net asp.net-mvc authentication asp.net-mvc-5
New contributor
add a comment |
up vote
1
down vote
favorite
I'm fairly new to ASP.NET MVC and trying to understand global Authentication Filters and anonymous access.
I have a custom AuthenticationFilter as so:
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
public void OnAuthentication(AuthenticationContext filterContext)
//Do some stuff
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
!user.Identity.IsAuthenticated)
filterContext.Result = new HttpUnauthorizedResult();
...and I've added it in my FilterConfig.cs to become global, as so:
filters.Add(new CustomAuthorizationAttribute());
When the app loads, it gets stuck in a circular loop - the login page just keeps redirecting.
The login page has the AllowAnonymous attribute. AllowAnonymous skips the AuthorizationAttribute step, so understandably doesn't impact the Authentication Filter.
My questions are:
1) If the Authentication scope is to verify the user, surely AllowAnonymous should bypass the AuthenticationFilter step instead? Once you've passed authentication, you're no longer anonymous...please can someone explain what I'm missing?
2) If I want to lock down a site, am I on the right track to have a global Authentication Filter? If so, how do I get out of this infinite loop and allow the login page to be shown?
Thanks!
c# asp.net asp.net-mvc authentication asp.net-mvc-5
New contributor
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I'm fairly new to ASP.NET MVC and trying to understand global Authentication Filters and anonymous access.
I have a custom AuthenticationFilter as so:
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
public void OnAuthentication(AuthenticationContext filterContext)
//Do some stuff
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
!user.Identity.IsAuthenticated)
filterContext.Result = new HttpUnauthorizedResult();
...and I've added it in my FilterConfig.cs to become global, as so:
filters.Add(new CustomAuthorizationAttribute());
When the app loads, it gets stuck in a circular loop - the login page just keeps redirecting.
The login page has the AllowAnonymous attribute. AllowAnonymous skips the AuthorizationAttribute step, so understandably doesn't impact the Authentication Filter.
My questions are:
1) If the Authentication scope is to verify the user, surely AllowAnonymous should bypass the AuthenticationFilter step instead? Once you've passed authentication, you're no longer anonymous...please can someone explain what I'm missing?
2) If I want to lock down a site, am I on the right track to have a global Authentication Filter? If so, how do I get out of this infinite loop and allow the login page to be shown?
Thanks!
c# asp.net asp.net-mvc authentication asp.net-mvc-5
New contributor
I'm fairly new to ASP.NET MVC and trying to understand global Authentication Filters and anonymous access.
I have a custom AuthenticationFilter as so:
public class CustomAuthenticationAttribute : ActionFilterAttribute, IAuthenticationFilter
public void OnAuthentication(AuthenticationContext filterContext)
//Do some stuff
public void OnAuthenticationChallenge(AuthenticationChallengeContext filterContext)
!user.Identity.IsAuthenticated)
filterContext.Result = new HttpUnauthorizedResult();
...and I've added it in my FilterConfig.cs to become global, as so:
filters.Add(new CustomAuthorizationAttribute());
When the app loads, it gets stuck in a circular loop - the login page just keeps redirecting.
The login page has the AllowAnonymous attribute. AllowAnonymous skips the AuthorizationAttribute step, so understandably doesn't impact the Authentication Filter.
My questions are:
1) If the Authentication scope is to verify the user, surely AllowAnonymous should bypass the AuthenticationFilter step instead? Once you've passed authentication, you're no longer anonymous...please can someone explain what I'm missing?
2) If I want to lock down a site, am I on the right track to have a global Authentication Filter? If so, how do I get out of this infinite loop and allow the login page to be shown?
Thanks!
c# asp.net asp.net-mvc authentication asp.net-mvc-5
c# asp.net asp.net-mvc authentication asp.net-mvc-5
New contributor
New contributor
edited yesterday
New contributor
asked Nov 9 at 16:43
Jason
63
63
New contributor
New contributor
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Jason is a new contributor. Be nice, and check out our Code of Conduct.
Jason is a new contributor. Be nice, and check out our Code of Conduct.
Jason is a new contributor. Be nice, and check out our Code of Conduct.
Jason is a new contributor. Be nice, and check out our Code of Conduct.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229925%2fasp-net-mvc-global-authentication-and-allowanonymous%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown