What's the equivalent of HttpContext.Current.User in an HttpListener-hosted service?

1.1k Views Asked by At

I've written a custom attribute for ServiceStack that has the following code in it:

public override void Execute(IHttpRequest request, IHttpResponse response, object requestDto) {
    HttpContext.Current.User = GetUserFromOAuth2BearerToken(request);
}

This works beautifully when hosting in IIS, but when using the built-in HttpListener host, HttpContext.Current returns null.

What's the correct syntax to set, and get, the security principal associated with the current request/response when using HttpListener?

1

There are 1 best solutions below

1
On

I am not sure the 'security principal/System.Security.Principal' is available with the current request/response when using AppHostHttpListenerBase.

You could pull get the User inforamation doing something like...

var sessionId = request.Cookies["ss-id"].ToString();
var user = request.GetCacheClient().Get<AuthUserSession>(sessionId);