Search code examples
c#.netservicestackhttpserver

Calling secure ServiceStack service from within


My main service is decorated with the [Authenticate] attribute so any connection attempts (that's important) require clients authentication.

[Authenticate]
public class ServerEventsService : Service
{
...
}

Is there any way to call services methods from within the server (like in self\web host classes) AND bypass the authentication? There is no [AllowAnonymous] thing in ServiceStack as far as I know. If there is no way to bypass the authentication process - how can I authenticate such requests? Is the only way here is to create another ServiceClient in my server host?


Solution

  • Yes you can call ServiceStack Services directly like any other dependency by resolving it from the IOC and calling it directly with:

    using var service = base.ResolveService<ServerEventsService>();
    var response = service.Post(new Request { ... });
    

    Although for improved decoupling it's recommended to use the Service Gateway:

    var response = Gateway.Send(new Request { ... });
    

    Also related, see docs on being able to impersonate users for Internal Requests without requiring the users password.