2013-05-18 22 views
0

我有一個名爲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代碼,而啓動時),我可以登錄。
但是,當我試圖通過角色代碼來創建我的超級管理員,我仍然得到我注意到的異常。

+0

在'InitialiseSimpleMembershipAttribute.cs'中仍然調用'WebSecurity.InitializeDatabaseConnection'嗎? – MattSull

+0

我註釋掉了這個屬性,所以上面的代碼是它唯一被調用的地方。 – twreynol

回答

1

終於找到了答案。這正是我爲提供者實例化的方式。該工作正確的方法是:

public class Init 
{ 
    const string SUPER_ADMIN_ROLE = "SuperAdmin"; 

    internal static void CheckForSuperAdmin() 
    { 
     if (!WebMatrix.WebData.WebSecurity.Initialized) 
     { 
      WebMatrix.WebData.WebSecurity.InitializeDatabaseConnection("DBRuWho", "customer", "customerId", "email", true); 
     } 
     var roles = (SimpleRoleProvider)Roles.Provider; 
     var users = (SimpleMembershipProvider)Membership.Provider; 
     if (!roles.RoleExists(SUPER_ADMIN_ROLE)) 
     { 
      roles.CreateRole(SUPER_ADMIN_ROLE); 
      string email = "[email protected]"; 
      string pswd = "[email protected]!"; 
      string credentials = API.Helpers.Misc.CreateCompanyCredential(pswd); 
      if (users.GetUser(email, false) == null) 
      { 
       Dictionary<string, object> args = new Dictionary<string, object>() 
       { 
        {"Name", email}, 
        {"CustomerToken", Guid.NewGuid()}, 
        {"Credentials", credentials}, 
        {"isActive", true} 
       }; 

       users.CreateUserAndAccount(email, pswd, args); 
       roles.AddUsersToRoles(new[] { email }, new[] { SUPER_ADMIN_ROLE }); 
      } 
     } 
    } 
} 

我發現這在一定程度上相關的帖子在這裏的S/O,我及時地丟失的鏈接(哎呀!)

我包括我需要其他屬性發送給我的CUSTOMER類,就像爲了別人的好處一樣。他們不一定是必需的。

無論如何,我做了三項改變以使其發揮作用。

1)確保您使用的Web.Matrix和Web.Matrix.data

2)的2.0版本添加以下到我的網頁。配置

<membership defaultProvider="SimpleMembershipProvider"> 
    <providers> 
    <clear /> 
    <add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" /> 
    </providers> 
</membership> 
<roleManager defaultProvider="SimpleRoleProvider" enabled="true"> 
    <providers> 
    <clear /> 
    <add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" /> 
    </providers> 
</roleManager> 

3)我做了上面列出的代碼更改。

我按照這個順序完成了所有這些,並且在#3之後開始工作。萬歲!我發佈這個以防萬一其他可憐的毫無戒心的靈魂試圖實例化新的提供者......