1

Is there a way to configure ASP.NET SqlMembershipProvider such that Email is optional but when provided must be unique?

I only found the requiredUniqueEmail attribute (web.config) which makes Email mandatory. Do I have to leave this out and implement checking for existing email addresses myself in the account registration process?

Or is there a nicer way to handle this scenario?

davehauser
  • 5,844
  • 4
  • 30
  • 45

1 Answers1

2

An easy, but probably risky way, is to modify the stored procedure directly to do this.

Modify aspnet_Membership_CreateUser by replacing:

IF (@UniqueEmail = 1)
BEGIN
    IF (EXISTS (SELECT *
                FROM  dbo.aspnet_Membership m WITH ( UPDLOCK, HOLDLOCK )
                WHERE ApplicationId = @ApplicationId AND LoweredEmail = LOWER(@Email)))
    BEGIN
        SET @ErrorCode = 7
        GOTO Cleanup
    END
END

with

    IF (EXISTS (SELECT *
                FROM  dbo.aspnet_Membership m WITH ( UPDLOCK, HOLDLOCK )
                WHERE ApplicationId = @ApplicationId AND LoweredEmail = LOWER(@Email)))
    BEGIN
        SET @ErrorCode = 7
        GOTO Cleanup
    END

I say it's risky because nobody would expect you to modify this and you'd better document the change well.

Greg
  • 16,540
  • 9
  • 51
  • 97