The use case is to not allow a login until the user has validated his/hers email. I’d want to redirect the user to another page accessible to anonymous users asking to validate the email address, or request another validation email.
Both using AuthEvents and session.OnAuthenticated I can be notified of the authentication event, but have no way to Veto it.
Looking at the source I also see that there’s no way to break out of the event calls, and afterwards the session is saved.
I’d prefer not to change every auth provider to include this feature. Also, I’d prefer to do this on authentication to avoid running the check on all requests.
Okay, just tried ValidateFn and it is executed very early in the authentication process, so I only have the username, and would have to fetch the user by hand. It might be workable, but it’s a bit of extra work, that will be duplicated afterwards.
Yeah AuthenticateService.ValidateFn is a pre-auth validation filter, there’s also the AuthProvider.CustomValidationFilter on each AuthProvider which may be more appropriate since it allows you to apply post validation to an Authenticated UserSession where you’ll have access to the full AuthContext.
Where you could return a redirect url response to an error page.
Also something else that maybe useful is the UserAuth.LockedDate which allows you to lock a users account. So you could automatically lock all new user accounts and then unlock them when they validate their email.
@mythz, thanks. I think I got this working with ValidateFn. Looking at CustomValidationFilter it seems a like a better way to go. But from what I can see CredentialsAuthProvider doesn’t use it. It overrides OnAuthenticated but doesn’t call base and check for return, nor use the filter.
Also, looking at DigestAuth it looks like it has the same issue, and there’s a session.IsAuthenticated = true; missing near line 138?
Using UserAuth.LockedDate would make it a bit confusing, because we might lock the user due other reasons. If needed we could add a LockedReason and go that way, but I’d prefer to use a check on authentication phase instead of changing state on the user.
Scratch that, ValidateFn won’t work. Since it runs before auth, I don’t even know if the user was correctly authenticated, and would have to do that myself. Means that it runs even if auth failed.
I’ll try subclassing CredentialsAuthProvider and perhaps DigestAuthProvider to take the CustomValidationFilter into consideration, see if that works.