Access to AuthSession on the Client after Authentication in ServiceStack Services

I got a little confused in the session documentation, so let's say I'm already sending authentication data from the client side and retrieve ss-id and ss-pid as follows:

var client = new JsonServiceClient("http://somewhere/theAPI/"); var response = client.Post(new Auth() {UserName = "myuser", Password = "password123"}); var myCookie= client.CookieContainer.GetCookies(new Uri("http://somewhere/theAPI")); 

how can I get AuthSession information exactly like last name, email address, etc. from the utility program? Do I need to store it somewhere else, like on a memcache server, and extract from this?

or do I need to create my client side authentication? and just use the API to retrieve the data?

+4
source share
1 answer

Assuming you already created a custom AuthUserSession, for example:

 /// <summary> /// Create your own strong-typed Custom AuthUserSession where you can add additional AuthUserSession /// fields required for your application. The base class is automatically populated with /// User Data as and when they authenticate with your application. /// </summary> public class CustomUserSession : AuthUserSession { public string CustomId { get; set; } } 

And you registered your own AuthUserSession when configuring the AuthFeature plugin, for example:

 public override void Configure(Container container) { //Register all Authentication methods you want to enable for this web app. Plugins.Add(new AuthFeature( () => new CustomUserSession(), //Use your own typed Custom UserSession type new IAuthProvider[] { new CredentialsAuthProvider(), //HTML Form post of UserName/Password credentials // and any other auth providers you need })); } 

Then you can provide this data to the client in the service you are creating. SocialBotstrapApi provides access to the current session information on the server as follows: Use it as a model to create a UserAuth service that returns information only for the current user.

 public abstract class AppServiceBase : Service { private CustomUserSession userSession; protected CustomUserSession UserSession { get { return base.SessionAs<CustomUserSession>(); } } } [Route("/userauths")] public class UserAuths { public int[] Ids { get; set; } } public class UserAuthsResponse { public UserAuthsResponse() { this.Users = new List<User>(); this.UserAuths = new List<UserAuth>(); this.OAuthProviders = new List<UserOAuthProvider>(); } public CustomUserSession UserSession { get; set; } public List<User> Users { get; set; } public List<UserAuth> UserAuths { get; set; } public List<UserOAuthProvider> OAuthProviders { get; set; } } //Implementation. Can be called via any endpoint or format, see: http://servicestack.net/ServiceStack.Hello/ public class UserAuthsService : AppServiceBase { public object Any(UserAuths request) { var response = new UserAuthsResponse { UserSession = base.UserSession, Users = Db.Select<User>(), UserAuths = Db.Select<UserAuth>(), OAuthProviders = Db.Select<UserOAuthProvider>(), }; response.UserAuths.ForEach(x => x.PasswordHash = "[Redacted]"); response.OAuthProviders.ForEach(x => x.AccessToken = x.AccessTokenSecret = x.RequestTokenSecret = "[Redacted]"); if (response.UserSession != null) response.UserSession.ProviderOAuthAccess.ForEach(x => x.AccessToken = x.AccessTokenSecret = x.RequestTokenSecret = "[Redacted]"); return response; } } 
+5
source

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


All Articles