Monday 15 June 2015

servicestack - RequiredRole, Razor2 and HTML Redirect -


I am using service stack with Razor2.

I used one of my service RequiredRole ("Admin") . What I want to do now is that if I'm coming from a browser (accept / text / html), then I have to get an HTML redirect if I do not have the required role. Instead I just get a 403 invalid role and the browser just shows a common error.

What is the way to get what I am after? I can see from the source of RequiredRoleAttribute that this does not seem to allow redirects.

  Execute public override zero (IHttpRequest REQ, IHttpResponse Res, Object Request Dto) {Base.Execute (Request, Re-Request, Request); // First check if the session (authentication) has been certified on the return; // authentication application has already closed the request (i.e. failed) var session = req.GetSession (); If return (HasAllRoles (request, session)); Res.StatusCode = (int) HttpStatusCode.Forbidden; Res.StatusDescription = "Invalid Role"; Res.EndServiceStackRequest (); }   

I have come up with a solution I have written my own retail RoleRequiredCustomAttribute This is similar As it looks, except the ServiceStack executed method:

  Execute public override zero (IHttpRequest req, IHttpResponse Res, Object request Dto) {Base. Execute (request, re-request, request); // First check if the session (authentication) has been certified on the return; // authentication application has already closed the request (i.e. failed) var session = req.GetSession (); If return (HasAllRoles (request, session)); Var htmlRedirect = HtmlRedirect ?? AuthService.HtmlRedirect; If (htmlRedirect! = Null & amp; req.ResponseContentType.MatchesContentType (ContentType.Html)) {var url = req.ResolveAbsoluteUrl (htmlRedirect); // url = url.adquaaparam ("redire", reak. AbsoluteURI); Res.RedirectToUrl (url); Return; } Res.StatusCode = (int) HttpStatusCode.Forbidden; Res.StatusDescription = "Invalid Role"; Res.EndServiceStackRequest (); }   

Should I submit a bridge request?

No comments:

Post a Comment