Search code examples
c#.netasp.net-web-apistormpath

PreRegistrationHandler not failing on Task error code


I am trying to implement validation on a custom data attribute through the PreRegistrationHandler. This works great when it passes validation.

However, when it does not, it creates the account anyway. When validation does not pass, I return a Task result code of one which should make it fail.

PreRegistrationHandler = (context, cancellationToken) =>
        {
            var passesvalidation = DoesPassValidation(context); // evals to false
            if (!passesvalidation)
            {
                return Task.FromResult(1); //should fail and not create account, but does anyway
            }
            return Task.FromResult(0); // passes
        }

Is there an undocumented return code that I should be using? I've checked here: https://docs.stormpath.com/dotnet/aspnet/latest/registration.html#pre-registration-handler


Solution

  • This functionality works as of version 0.9.0 of the Stormpath ASP.NET plugin.

    The syntax for failing validation inside of the PreRegistrationHandler is:

    PreRegistrationHandler = (context, cancellationToken) =>
    {
        var passesvalidation = DoesPassValidation(context); // evals to false
        if (!passesvalidation)
        {
            context.Result = new PreRegistrationResult()
            {
                Success = false,
                ErrorMessage = "No way, José!" // optional validation message
            };
    
            return Task.FromResult(1); // returned value doesn't matter
        }
    
        return Task.FromResult(0); // without setting context.Result, success is assumed
    }