asp.net mvc - Global Custom Authorize Attribute with Excluded Atrribute -
i have 2 attributes :
public class anonymousallowedattribute : authorizeattribute { } public class actionauthorizeattribute : authorizeattribute { public override void onauthorization(authorizationcontext filtercontext) { bool skipauthorization = filtercontext.actiondescriptor.isdefined(typeof(anonymousallowedattribute), true) || filtercontext.actiondescriptor.controllerdescriptor.isdefined(typeof(anonymousallowedattribute), true); if(!skipauthorization) base.onauthorization(filtercontext); } bool customecheck() { bool result = //my checks return result; } } i define actionauthorizeattribute global attribute.
so need 3 items:
1- if did not log in(!user.identity.isauthenticated): go login page accounts/login. must mention login action marked anonymousallowedattribute.
2- if log in (user.identity.isauthenticated) , action or controller have anonymousallowedattribute authorize true (don't need authorization).
3- if log in (user.identity.isauthenticated) , action haven't anonymousallowedattribute return customecheck() method
i try second 1 override onauthorization() method see.
and third 1 followings:
protected override bool authorizecore(system.web.httpcontextbase httpcontext){ if(!httpcontext.user.identity.isauthenticated) return false; return customecheck(); } but when did not log in return:
iis 7.5 error details:
http error 401.0 - unauthorized
with url: http://myproject/accounts/login?returnurl=%2f
where problem? how can implement actionauthorizeattribute achieve 3 goals?
update
i find answer : problem : anonymousallowedattribute need inherit attribute rather authorizeattribute.
the problem is: anonymousallowedattribute need inherit attribute rather authorizeattribute.
when anonymousallowedattribute inherit authorizeattribute need authorize create reduce authorization!!
Comments
Post a Comment