How can I update user data form session in Service

2019-06-26 07:20发布

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条回答
该账号已被封号
2楼-- · 2019-06-26 08:11

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;
}
查看更多
登录 后发表回答