How can I update user data form session in ServiceStack?

雨燕双飞 提交于 2019-12-10 17:08:02

问题


Very simple question: I have a session object in my Service:

var session = this.GetSession(); //IAuthSession
   if (!session.IsAuthenticated)

I can modify some values in the session class (e.g. Permissions) based on the parameters passed to the service; then I want to save them. How?

The direct way of doing it: create a UserAuth object, popolate it with all the fields from IAuthSession, get the IDbConnectionFactory, save it.

Surely there is a faster and better way, but I was not able to find it!

More generally, how can I switch between IAuthSession anf UserAuth? I.e., given a IAuthSession object, how can I obtain a UserAuth object, modify it, and persist the modifications?

I have read this question on how to append metadata to a user login info, but something is still missing.

Once you have added what you need, how do you save it? (I doubt you just add the metadata to both session and UserAuth, and then you use IDbConnectionFactory to save the latter; there must be a better way!)


回答1:


Old question but worth answering.

The UserAuthRepository being used should have an implementation of the UpdateUserAuth method that can be called to save the UserAuth changes

UpdateUserAuth(UserAuth existingUser, UserAuth newUser, string password)

Another easier way would be to just call the RegisterService using PUT which will update the existing registered user for you.

/// <summary>
/// Update an existing registraiton
/// </summary>
public object Put(Register request)
{
   return Post(request);
}

The service call would be something similar to this:

using (var authService = base.ResolveService<RegisterService>())
{
   var authResponse = authService.Put(
       new Register {
           UserName = session.UserName ?? session.Email,
           Email = session.Email,
           etc...
       });

   if (authResponse is IHttpError)
     throw (Exception)authResponse;
}


来源:https://stackoverflow.com/questions/17813438/how-can-i-update-user-data-form-session-in-servicestack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!