asp.net mvc - Different Log on screens depending on area - MVC3 -
i have area mobile layout. have controllers in route uses normal website layout.
the problem when use [authorize(roles = "rolename")] , user isn't in role page(mobile site) gets redirected normal website login page , not mobile one.
is possible have switch between logins depending on area user trying access site?
i've tried adding following in area web.config didn't work:
<authentication mode="forms"> <forms loginurl="~/activation/login/index" timeout="2880" /> </authentication>
any suggestions?
when login action gets hit, check if on mobile device , redirect mobile login page if are.
private static string[] mobiledevices = new string[] {"iphone","ppc", "windows ce","blackberry", "opera mini","mobile","palm", "portable","opera mobi" }; public static bool ismobiledevice(string useragent) { // todo: null check useragent = useragent.tolower(); return mobiledevices.any(x => useragent.contains(x)); }
then in action:
public actionresult index() { if (mobilehelper.ismobiledevice(request.useragent)) { // send mobile route. return redirecttoaction("login", "mobileactivation"); } // otherwise normal login }
edit:
if wanted apply broadly accross application following.
create actionfilter apply anywhere this:
[attributeusage(attributetargets.class | attributetargets.method, allowmultiple = false, inherited = true)] public sealed class redirecttomobilearea : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { var rd = filtercontext.httpcontext.request.requestcontext.routedata; var currentaction = rd.getrequiredstring("action"); var currentcontroller = rd.getrequiredstring("controller"); string currentarea = rd.values["area"] string; if (!currentarea.equals("mobile", stringcomparison.ordinalignorecase) && mobilehelper.ismobiledevice(request.useragent)) { filtercontext.result = new redirecttorouteresult(new routevaluedictionary { { "controller", currentcontroller }, { "action", currentaction }, { "area", "mobile" } }); } } }
this filter take action check if mobile (and not in mobile area already) , send same action , controller in mobile area. note if go controllers of same name have register routes controller namespace see answer
then either apply filter each action like:
[redirecttomobilearea] public actionresult index() { // stuff. }
or if want everywhere create basecontroller controllers inherit , apply that:
[redirecttomobilearea] public abstract class basecontroller : controller { }
then inherit it:
public homecontroller : basecontroller { }
i havent tested of should work...
Comments
Post a Comment