asp.net mvc 4 - user isn't authenticated in custom authorize attribute -
i've made own authorize attribute, , looks like
public class redirectauthorize : authorizeattribute { protected override void handleunauthorizedrequest(authorizationcontext filtercontext) { if (!filtercontext.httpcontext.user.identity.isauthenticated) { filtercontext.result = new redirecttorouteresult(new routevaluedictionary(new { controller = "notexist" })); } } }
so if user isn't authenticated, want them redirected notexist
controller. i've debugged , seems unauthorized users in if clause, correct. i've tried doing logged in users, , in if clause wrong.
i dont understand why happening. makes me hesitate whether log-in didnt work. right way of logging user in?
formsauthentication.setauthcookie(acc.username, false);
i've never made log-in system in asp.net mvc before, please tell me i'm doing wrong.
edit:
it seems default [authorized] attribute isn't working either... think problem lays in log in:
[httppost] public actionresult login(user acc) { if(modelstate.isvalid) { if (validate(acc.username, acc.password)) { formsauthentication.setauthcookie(acc.username, false); return redirecttoaction("index", "system"); } } modelstate.addmodelerror("incorrectdetails", "wrong details. please try again."); return view(acc); }
the custom authorize attribute looks correct.
since setting cookie guess not using built-in membership provider.
if set cookie yourself, need read auth cookie , set identity , principal objects on each request. otherwise, httpcontext.user.identity.isauthenticated false, seems experiencing.
Comments
Post a Comment