为什么没有调用CustomeMembershipProvider?

时间:2014-02-17 17:51:58

标签: c# asp.net-mvc asp.net-mvc-4 simplemembership

查看多个SO q / a 我做了以下事情:

public class CustomMembershipProvider : SimpleMembershipProvider
{

    public override string CreateUserAndAccount(string userName, string password, IDictionary<string, object> values)
    {
     // code to insert data into UserAccounts and UserProfiles tables 
     // using simple sqlconnections
    }
}

数据库表如下:

CREATE TABLE UserAccounts
(
UserNumericId INT NOT NULL IDENTITY PRIMARY KEY,
LoginName VARCHAR(10) NOT NULL,
Password VARCHAR(100) NOT NULL,
)
CREATE TABLE UserProfiles
(
UserNumericId INT NOT NULL PRIMARY KEY,
DisplayName VARCHAR(100) NOT NULL,
PrimaryEmail VARCHAR(80) NOT NULL UNIQUE,
)

Web Config

<membership defaultProvider="CustomMembershipProvider">
  <providers>
    <clear/>
    <add name="CustomMembershipProvider" type="Helpers.CustomMembershipProvider, assembly" />
  </providers>
</membership>

在global.asax Lazyinitialzation中使用

public class SimpleMembershipInitializer
{
    public SimpleMembershipInitializer()
    {
        if (!WebSecurity.Initialized)
            WebSecurity.InitializeDatabaseConnection("ApplicationServices", "UserAccounts", "UserAccountId", "LoginName", autoCreateTables: false);
    }
}

当我从我的Register控制器操作中调用以下语句

WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new { DisplayName= model.DisplayName, PrimaryEmail = model.PrimaryEmail});

我的自定义提供程序中的断点不会受到影响。有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

您已在SimpleMmebershipProvider中覆盖了错误的方法。 CreateUserAndAccount有几种重载方法。如果您查看source code for WebSecurity,则可以看到它使用此签名 [SimpleMembershipProvider.CreateUserAndAccount Method (String, String, Boolean, IDictionary)]来调用它。以下是WebSecurity.CreateUserAndAccount的代码:

    public static string CreateUserAndAccount(string userName, string password, object propertyValues = null, bool requireConfirmationToken = false)
    {
        ExtendedMembershipProvider provider = VerifyProvider();
        Debug.Assert(provider != null); // VerifyProvider checks this

        IDictionary<string, object> values = null;
        if (propertyValues != null)
        {
            values = new RouteValueDictionary(propertyValues);
        }

        return provider.CreateUserAndAccount(userName, password, requireConfirmationToken, values);
    }