為WebApi項目增加用戶注冊功能
一、增加Filter目錄:
Mvc4項目下,Filter文件夾下的InitializeSimpleMembershipAttribute.cs,其作用是用來為數據庫創建用戶、角色管理表格的。
移植到webapi項目,修改命名空間。
涉及到2個問題:
1、需要引用webmetrix.webdata程序集。在引用|添加引用|擴展,可以找到兩個程序集,選擇2.0版本。
2、需要models里的UsersContext類的聲明
這意味着我們必須將mvc4的models里的AccountModels.cs文件遷移過來
編譯通過之后,我們在Filter中InitializeSimpleMembershipAttribute.cs里面
public SimpleMembershipInitializer() 這個唯一的方法里,第一行設置斷點,看看啟動程序,會不會執行初始化工作。
結果,沒有在此斷點處停下。查看數據庫,也沒有新增用戶管理的相關表格。
這里設置斷點的目的是:
1、確保每次修改,都能正常運行
2、弄清初始化過程,是在首次程序啟動的時候執行,還是在首次使用用戶管理功能的時候執行。
3、我們先指定我們的連接字符串,替代DefaultConnection
WebSecurity.InitializeDatabaseConnection("WebLite", "UserProfile", "UserId", "UserName", autoCreateTables: true);
二、首先嘗試 注冊功能。
1、我們先將AccountController移植過來
顯然,我們只關心注冊功能,因此,修改注釋掉類里的全部內容,僅保留注冊方法的get、post方法。注釋掉引起編譯問題的語句。
編譯一次,通過。
2、我們加入注冊功能的View,同樣移植過來
修改,編譯通過
3、好了,我們現在就看看,Account控制器的注冊方法能否正常運行。
啟動程序,我們看到瀏覽器的地址欄為:
http://localhost:15946
修改為:
表示我們要執行AccountController的Register方法,注意這種訪問是"get",該方法返回View(),表示按Mvc的約定返回同名的Register.cshtml視圖。
4、很好,我們設在Filter中斷點,其作用了:說明執行注冊方法的時候,系統判斷數據庫中沒有相應表格,准備自動創建表格。
選擇繼續,出現異常:

5、這個異常什么意思?
首先,找不到連接字符串DefaultConnection,其次,這處異常出現在Web.config的45行,信息如下:
{"在應用程序配置中找不到連接名稱“DefaultConnection”,或者連接字符串為空。 (E:\\WebLite\\WebLite\\web.config line 45)"}
再次“繼續",頁面上更為清晰:
行 43: <membership defaultProvider="DefaultMembershipProvider">
行 44: <providers>
行 45: <add name="DefaultMembershipProvider" type="System.Web.Providers.DefaultMembershipProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false" maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10" applicationName="/" />
行 46: </providers>
行 47: </membership>
注意這是WebApi為了使用傳統的membership認證方式而預先准備的。
我們顯然不會使用這種方式,那么注釋掉。
重新調試,繼續異常:
"The ASP.NET Simple Membership database could not be initialized. For more information, please see
http://go.microsoft.com/fwlink/?LinkId=256588
"未啟用角色管理器功能。"
繼續:頁面呈現的錯誤信息更明顯
6、那么我們跟蹤一下
重新運行,到斷點處,每次按F10執行一條。
執行完if (!context.Database.Exists())的時候,問題出現了:
我們鼠標點Database,發現其連接字符串為:
Data Source=(localdb)\v11.0;AttachDbFilename=|DataDirectory|DefaultConnection.mdf;Initial Catalog=DefaultConnection;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFrameworkMUE
問題出在var context = new UsersContext()這里,創建的Contex....使用了默認的連接字符串
轉到userContext處,用”轉到定義"
嗯:
public class UsersContext : DbContext
{
public UsersContext()
: base("DefaultConnection")
{
}
public DbSet<UserProfile> UserProfiles { get; set; }
}
7、問題繼續出現:沒有啟用角色管理器
單純從這點來看,明顯的.....我們會考慮什么? 配置文件中的 enable role?
事實上,在配置文件中加入:
<compilation debug="true" targetFramework="4.5">
<assemblies>
<add assembly="WebMatrix.Data, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
<add assembly="WebMatrix.WebData, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"/>
</assemblies>
</compilation>
強迫執行----會啟用角色管理等等
現在繼續出現問題----那是頁面的
8、簡單的修改注冊頁面
將最下面一行注釋掉
@*@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}*@
9、運行
可以正常的注冊用戶