asp.net mvc - MVC 5 Custom ActionFilter not working -
i'm trying implement basic authentication service using question template.
for reason action filter never applied controllers , have no idea why.
my controller:
[basicauthenticationmanager("username", "password")] public class datacontroller : apicontroller { private dataentities db = new dataentities(); // get: api/data //[basicauthenticationmanager] public ihttpactionresult getvwdata() { return json(db.vwdata); } }
my filter:
public class basicauthenticationmanager : actionfilterattribute { protected string username { get; set; } protected string password { get; set; } public basicauthenticationmanager(string username, string password) { this.username = username; this.password = password; } public override void onactionexecuting(actionexecutingcontext filtercontext) { var req = filtercontext.httpcontext.request; var auth = req.headers["authorization"]; if (!string.isnullorempty(auth)) { var credentials = asciiencoding.ascii.getstring(convert.frombase64string(auth.substring(6))).split(':'); var user = new { name = credentials[0], pass = credentials[1] }; if (user.name == username && user.pass == password) return; } filtercontext.result = new httpunauthorizedresult(); } }
i don't error messaage , filter recogniced code autocompletion.
inherit system.web.http.authorizeattribute instead of actionfilterattribute. can override isauthorized method.
protected override bool isauthorized(system.web.http.controllers.httpactioncontext actioncontext) { //auth logic //return whether user authorized or not. }
Comments
Post a Comment