c# - Is it possible to disable authentication Filter on one action in an MVC 5 controller? -
[authenticateuser] public class homecontroller : controller { // // get: /home/ public actionresult index() { homecoming view(); } [allowanonymous] public actionresult list() { homecoming view(); } }
how remove authentication action named list? please advise....
my custom filter coding follow.. have inherited filterattribute phone call well. please advise regarding
public class authenticateuserattribute: filterattribute, iauthenticationfilter { public void onauthentication(authenticationcontext context) { if (this.isanonymousaction(context)) { } if (user == "user") { // nil } else { context.result = new httpunauthorizedresult(); // mark unauthorized } } public void onauthenticationchallenge(authenticationchallengecontext context) { if (context.result == null || context.result httpunauthorizedresult) { context.result = new redirecttorouteresult("default", new system.web.routing.routevaluedictionary{ {"controller", "home"}, {"action", "list"}, {"returnurl", context.httpcontext.request.rawurl} }); } } }
the below code generate error message : error 1 best overloaded method match 'mvc5features.filters.authenticateuserattribute.isanonymousaction(system.web.mvc.authorizationcontext)' has invalid arguments c:\users\kirupananthan.g\documents\visual studio 2013\projects\mvc5features\mvc5features\filters\authenticateuserattribute.cs 16 17 mvc5features error 2 argument 1: cannot convert 'system.web.mvc.filters.authenticationcontext' 'system.web.mvc.authorizationcontext' c:\users\kirupananthan.g\documents\visual studio 2013\projects\mvc5features\mvc5features\filters\authenticateuserattribute.cs 16 40 mvc5features
if (this.isanonymousaction(context))
since custom filter, can extend handle allowanonymous
(if don't want utilize allowanonymous, yoy can create own f.e. noauthentication):
public class authenticateuser : iauthenticationfilter { public void onauthentication(authenticationcontext filtercontext) { if (this.isanonymousaction(filtercontext)) { return; } // code } private bool isanonymousaction(authenticationcontext filtercontext) { homecoming filtercontext.actiondescriptor .getcustomattributes(inherit: true) .oftype<allowanonymousattribute>() //or attr. want .any(); } }
c# asp.net .net asp.net-mvc asp.net-mvc-5
No comments:
Post a Comment