How to get HttpContext at servicestack.net

I can’t switch to all new service providers, authentication, etc. So, I am running a hybrid script and it works fine.

To get the current user in the service, I do this:

private IPrincipal CurrentUser() { var context = HttpContext.Current; if (context != null) { var user = context.User; if (user != null) { if (!user.Identity.IsAuthenticated) return null; return user; } } return null; } 

Is there an alternative / best way to get the current HTTP context directly from the service? I would prefer not to use HttpContext.Current if I don't need to?

+4
source share
1 answer

This is an alternative way ... It goes through ServiceStack to get the OriginalRequest , which will be an ASP.NET request (although it may be an HttpListener HttpRequest if it is not used in an ASP.NET application). Not sure if this is better, but you no longer have HttpContext.Current inside your service code.

 public class MyService : Service { public object Get(MyRequest request) { var originalRequest = this.Request.OriginalRequest as System.Web.HttpRequest; var user = originalRequest.RequestContext.HttpContext.User; // more code... } } 
+8
source

Source: https://habr.com/ru/post/1481988/


All Articles