我有一個名爲Customer的自定義表,其中CustomerId和Email作爲兩個字段(等等)。我想在MVC4網站中使用它。
我有一個名爲DBRuWho的連接字符串。 在我的App_Start \ AuthConfig.cs文件中,我有一個名爲CheckForSuperAdmin()的方法,它將以初始超級用戶爲我的表播種。mvc 4個簡單的成員資格要求初始化,即使它是
internal static void CheckForSuperAdmin()
{
if (!WebMatrix.WebData.WebSecurity.Initialized)
{
WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("DBRuWho", "customer", "customerId", "email", true);
}
var roles = new WebMatrix.WebData.SimpleRoleProvider();
var users = new WebMatrix.WebData.SimpleMembershipProvider();
if (!roles.RoleExists("SuperAdmin"))
{
roles.CreateRole("SuperAdmin");
if (users.GetUser("[email protected]", false) == null)
{
users.CreateUserAndAccount("[email protected]", "[email protected]!");
roles.AddUsersToRoles(new[] {"[email protected]"}, new[] {"SuperAdmin"});
}
}
}
當我「註冊用戶」,一個客戶行被創建作爲webpages_Membership表幹一行,他們看起來都不錯。 但是,如果我在上面的「var roles」行設置斷點並跳過該方法的其餘部分,我只能註冊一個用戶。
然後,用這個用戶創建,我無法登錄 - 我得到一個密碼不匹配。
,如果我不跳過開頭「VAR角色」的代碼,我得到一個異常: 必須調用「WebSecurity.InitializeDatabaseConnection」方法調用「WebSecurity」類的任何其他方法之前。
這很令人沮喪,因爲您可以清楚地看到我打電話給該方法。在它被調用之後,我在Watch窗口中查看它,並指出它已被初始化。
所以,我被困在這裏....我想使用這個「簡單」的會員資格,但它似乎不允許我簡單地使用它!
有什麼想法?我讀了很多文章,看起來好像我正在做的一切正常,但顯然我在這裏失去了一些東西...
更多信息:這是我的連接字符串映射到DBRuWho。
<connectionStrings>
<add name="DBRuWho" connectionString="Server=localhost;Database=ruwho;integrated security=true" providerName="System.Data.SqlClient" />
我敢肯定我是正確連接,因爲數據被插入!當我檢查!roles.RoleExists和我無法登錄時,我似乎無法通過異常。
更新 - 我刪除了我的Customer表中的所有數據以及webpages_Membership表中的所有數據,並且現在(如果我跳過!roles.RoleExists代碼,而啓動時),我可以登錄。
但是,當我試圖通過角色代碼來創建我的超級管理員,我仍然得到我注意到的異常。
在'InitialiseSimpleMembershipAttribute.cs'中仍然調用'WebSecurity.InitializeDatabaseConnection'嗎? – MattSull
我註釋掉了這個屬性,所以上面的代碼是它唯一被調用的地方。 – twreynol