c# - Redirect users with suspended accounts without creating redirect loop -
i have subscription based mvc 2 application basic .net membership service in place (underneath custom components manage account/subscription, etc). users accounts have lapsed, or have manually suspended accounts, need able single view in system manages status of account. controller driving view protected using [authorize] attribute.
i want ensure no other views in system can accessed until user has re-activated account. in base controller (from protected controllers derive) tried modifying onactionexecuting method intercept action, check suspended account, , if it's suspended, redirect single view manages account status. puts me in infinite loop. when new action hit, onactionexecuting gets called again, , cycle keeps going.
i don't want extend [authorize] attribute, can if need be.
any other thoughts on how @ controller level?
edit: in base controller, managing redirect (that subsequently created redirect loop) modifying filtercontext.result property, setting redirecttoaction result of view in question. noticed everytime loop occurs, filtercontext.result == null. perhaps should checking against different part of filtercontext?
ok, here's solution in case helps else. there's got more elegant way this, , i'm ears if has better idea.
in basecontroller.cs:
protected override void onactionexecuting(actionexecutingcontext filtercontext) { viewdata["currentuser"] = currentuser; // public property in basecontroller if (currentuser != null && currentuser.account.status != accountstatus.active) { // if account disabled , authenticated, need allow them // account settings screen can re-activate, logoff // action. else should disabled. string[] actionwhitelist = new string[] { url.action("edit", "accountsettings", new { id = currentuser.account.id, section = "billing" }), url.action("logoff", "account") }; var allowaccess = false; foreach (string url in actionwhitelist) { // compare each of whitelisted paths raw url request context. if (url == filtercontext.httpcontext.request.rawurl) { allowaccess = true; break; } } if (!allowaccess) { filtercontext.result = redirecttoaction("edit", "accountsettings", new { id = currentuser.account.id, section = "billing" }); } } base.onactionexecuting(filtercontext); }
Comments
Post a Comment