ASP.NET SqlMembershipProvider:唯一但不是必需的电子邮件?

时间:2010-10-12 22:00:38

标签: asp.net-membership sqlmembershipprovider

有没有办法配置ASP.NET SqlMembershipProvider,以便Email是可选的,但提供时必须是唯一的?

我只找到强制要求发送电子邮件的requiredUniqueEmail属性(web.config)。我是否必须将其保留,并在帐户注册过程中自行检查现有的电子邮件地址?

或者有更好的方法来处理这种情况吗?

1 个答案:

答案 0 :(得分:2)

一种简单但可能有风险的方法是直接修改存储过程来执行此操作。

通过替换:

修改aspnet_Membership_CreateUser
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

    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

我说它有风险,因为没人会指望你修改它,你最好记录好这个变化。