davehauser
davehauser

Reputation: 5944

ASP.NET SqlMembershipProvider: Unique but not required email?

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?

Upvotes: 1

Views: 886

Answers (1)

Greg
Greg

Reputation: 16680

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.

Upvotes: 2

Related Questions