2

I have IdentityServer and my Api running as two separate applications. I am using default ui and everything works so far but I would like to create user in my Api application whenever there is successful user registration in IS so I have my business logic in Api project and authentication logic in IS project. I would then map IS user by his sub to my Api user on each request.

The idea is I would call "create user" endpoint on my Api from IS whenever a new user is created but I don't know how to catch this event.

I tried to create custom event sink as described here but there is no registration event. I also found a place where the user is being created in default ui here but that is as far as I was able to go.

So my question is: How to run code whenever a user is created in Identity server project?

Tom Halson
  • 380
  • 3
  • 12
Hnus
  • 912
  • 2
  • 9
  • 24
  • as i see, `UserManager` in Asp.Net Identity (that's not Identity server) does not raise any events to subscribe. probably underlying `UserStore` does, i haven't checked. anyway probably the two simplest solutions could be either to override the user manager as suggested [here](https://stackoverflow.com/a/31322732/1426685) and in your `public virtual async Task CreateAsync(TUser user)` override after successfully calling the base, use `HttpClient` to call your API, or just do the same on the UI level at the point you found. – d_f Jun 21 '19 at 13:24
  • 1
    using api call over http does not guarantee you 100% successful replication. you have to handle possible exceptions correspondingly, possibly using message queuing, or... just leave another chance for a user to get created in your api, right when he comes there within a jwt. – d_f Jun 21 '19 at 13:35
  • @d_f That is a very good point. I guess I could write some kind of middleware which would check on every request if I need to create user in my api DB. That way even if he is coming from another authentication provider like google I can still create record for him in my DB. Is it good approach or is there a better way? – Hnus Jun 21 '19 at 17:44
  • at least such "on the fly" approach works in production in thousands online stores etc. for enterprise the "replication" way is more preferable in general, as usually it drives more data, than support default identity schema. but, as i understand, that's not your case, so... it depends and why not : ) – d_f Jun 21 '19 at 20:36
  • @d_f I just did quick implementation of the middleware and it works fairly well but I am curious what is exactly the "replication way"? I am working just on hobby project so I don't mind if using middleware is not the fastest option. I also like the fact that I can keep `api` and `identity` projects separated each accessing only its own db. Thank you for your help. – Hnus Jun 23 '19 at 11:10
  • 1
    under the "replication way" I mean sending backchannel messages from system to system using message queuing, sync framework or similar technologies. glad, you are all done! :) – d_f Jun 23 '19 at 12:15

0 Answers0