Search code examples
servicestackregistration

ServiceStack - use customized registration?


i created a customized user table for authentication purpose. When i tried to register an user, the built-in registerservice.cs went to UserAuth so a

""ResponseStatus": {
    "ErrorCode": "InvalidCastException",
    "Message": "Unable to cast object of type 'ServiceStack.Auth.UserAuth' to type 'SOS.Api.ServiceModel.Types.User'.","

was thrown. I know i can extend registerservice.cs, but how do i use the customized .cs? in AppHost.cs Plugins.Add(new RegistrationFeature()); or something? Thanks.


Solution

  • ServiceStack's RegisterService is tied to the built-in UserAuth model, i.e:

    [DefaultRequest(typeof(Register))]
    public class RegisterService : RegisterUserAuthServiceBase {}
    

    You would need to provide your own Custom Register Service to handle your Custom Poco which you can register with:

    [DefaultRequest(typeof(Register))]
    public class CustomRegisterService : RegisterUserAuthServiceBase {}
    

    Since this is a different Register Service you don't want to enable ServiceStack's built-in RegistrationFeature:

    //Plugins.Add(new RegistrationFeature()); //don't register this plugin 
    

    You should instead register your Service manually in your AppHost with:

    this.RegisterService<CustomRegisterService>("/register");
    this.RegisterAs<RegistrationValidator, IValidator<Register>>();
    

    Use a modified copy

    Since the RegisterService is fairly lightweight another option is to take a copy of it and modify it to reference your CustomUserAuth POCO instead.