javascript - Restrict view access -
i working project make use of asp.net mvc 5 , asp.net web api. basically, pure javascript ajax call used access web api (instead of mvc controller) in separate project. return current user id , role saved session using sessionstorage.setitem (not httpcontext.current.session). , entire point of controller inside mvc returning view only.
the challenge facing redirect user 404 page without flickering should user not authenticated view. currently, sessionstorage.getitem used in determining whether user authenticated in particular cshtml file. since javascript solution, flicker , shows page before being redirect 404.
i know mvc 5 come authorization attribute controller, seems not applicable in situation. furthermore, structure of project fixed , not allow change sessionstorage.setitem httpcontext.current.session.
given restriction above, seems not able found out solution this.
note: final outcome return 404 page before page being rendered if user not authenticated. hope can on this.
you can achieve attributes
public class checksession : actionfilterattribute { public override void onactionexecuting(actionexecutingcontext filtercontext) { //if cookie not exist, redirect 404 in error controller if(!httpcontext.current.request.cookies.allkeys.contains("mycookie")) filtercontext.result = new redirecttorouteresult(new routevaluedictionary() { { "controller", "error" }, { "action", "some404view" } }); base.onactionexecuting(filtercontext); } }
and decorate action
[checksession()] public actionresult someaction() { }
Comments
Post a Comment