I use ServiceStack without the built-in auth and session services.
I use the attribute as a request filter to collect user information (id and token), either from a cookie or request header or string parameter. You can provide this information after a user logs in. You add a new cookie in response and enter information about the identifier and token on the client side when rendering the view, so you can use http headers and request parameters for links.
public class AuthenticationAttribute : Attribute, IHasRequestFilter { public void RequestFilter(IHttpRequest request, IHttpResponse response, object dto) { var userAuth = new UserAuth { }; if(!string.IsNullOrWhiteSpace(request.GetCookieValue("auth")) { userAuth = (UserAuth)request.GetCookieValue("auth"); } else if (!string.IsNullOrEmpty(request.Headers.Get("auth-key")) && !string.IsNullOrEmpty(request.Headers.Get("auth-id"))) { userAuth.Id = request.Headers.Get("id"); userAuth.Token = request.Headers.Get("token"); } authenticationService.Authenticate(userAuth.Id, userAuth.token); } public IHasRequestFilter Copy() { return new AuthenticationAttribute(); } public int Priority { get { return -3; } }
If the user is not logged in, I am redirecting him at this moment.
My project supports SPA. If the user uses the API using xmlhttprequests, the authentication material is executed with headers. I add this information about AngularJS when the page loads and reuse it for all requests (partial views, api consumption, etc.). ServiceStack is a powerful tool for this type of thing, you can easily configure the AngularJS application and the ServiceStack viewer to work side by side, check all requests, globalize your application, etc.
If you do not have cookies and javascript is not called, you can support authentication without cookies if you always generate links that pass the identifier and token as request parameters and pass them through a hidden form input, for example.
Guilherme cardoso
source share