How to pass a Company to CustomAuthAuthRepository?

in SS 4.0.40 a UserAuth has a Company property and I’m trying to populate that field when a user registers.

I have a CustomNHibernateAuthRepository and on the CreateUserAuth I can’t figure it out, what’s the best way to get the Company field.

The needed property can be in FromData or Request.Header but my CustomAuthSession is only invoked after the UserAuth is at this point the new UserAuth is not yet in the database…

from what I can see, the flow is:

  • AuthUserSession.OnCreated
  • (does user exist and more validations)
  • IUserAuthRepository.CreateUserAuth
  • AuthUserSession.OnRegistered

in the last call (AuthUserSession.OnRegistered) the user is not yet in the database, and for such I can’t update it with the Company (as from the Session I have access to the Request)…

How can I add the Company property into the newly created user?

and by the way, the json response I get is:

{
  "userId": "0",
  "sessionId": null,
  "userName": null,
  "referrerUrl": null,
  "responseStatus": {
    "errorCode": null,
    "message": null,
    "stackTrace": null,
    "errors": null
  },
  "meta": null
}

Is there a way to make the returned data contain the UserId (it should be my Guid as a string)?

The only way I could accomplish this, and I’m sure is not the best solution was:

  • in the IUserAuthRepository.CreateUserAuth call, assign the first company ID (System Company)
  • when authenticate for the first time, under AuthUserSession.TryAuthenticate I see if the Email address provided (as username) exists in the UserAuth table with the System Company Id and if yes, then I update it with the correct one (passed by custom header).

It’s not elegant, but it does work (unless the same usermail is created multiple times without never authenticate :frowning:

I’m not overly familiar with NHibernate myself, but once the authProvider.CreateUserAuth function has been called, the UserAuth should be persisted.

This would follow behaviour (as far as I understand) for the other implementations of the IAuthProvider interface. Eg,

OrmLite:

Redis:

RavenDB:

Another way to have custom logic on registration would be to register your own IAuthEvents class and override OnRegistered method. This is fired after session.OnRegistered and has access to the Request, session and RegisterService instance. You can resolve your IUserAuthRepository from the registration service instance and update the user.

Is this the response from a successful registration? Can you provide more info on when this happens or a simple project reproducing the problem?

but when the Save is invoked, it’s only a set, because when I debug line by line, and I pass the Save method as

var nhSession = GetCurrentSessionFn(sessionFactory);
nhSession.Save(new CustomUserAuthNHibernate(newUser));

I still have no data in the database, so I’m assuming there’s a wait, and only when the session is about to be closed, the transaction is commit.

only when the action is complete I have data in the database… not even in the AuthUserSession.OnRegistered I have data in the db… hence the issue on passing data into the Repository :frowning:

and yes I always get the same object when using /register:

I would love to have it filled up with custom data, hence my other question

I’m not really experienced with nHibernate, but reading common problems it seems that a lot of the control of persistence is put back onto the consuming application, control over FlushMode and use of ‘2nd level cache’ seem like points that might be causing problems/confusion.

Happy to try and help though. Are you using FluentNHibernate.Cfg? Would you be able to provide your config code? Specifically how you are constructing the ISessionFactory?

In the OnRegistered event hook, could you call session.Transaction.Commit()? Again, I’m not sure what is considered 'best practice" in regards to NHibernate but my guess would be configuration or maybe (as is suggested here http://stackoverflow.com/questions/478827/nhibernate-update-not-working) an exception is being ignored somewhere that might be causing you to get null response from the registration call.