Asp.net-Membership
如何使用 ASP.Net 會員提供程序添加額外的欄位?
我有一個使用 ASP.NET 成員資格提供程序的基本應用程序。預設情況下,您可以使用一些欄位,例如使用者名、密碼、記住我。
如何添加到asp.net 會員提供程序,以便我可以在註冊部分添加一個附加欄位“地址”並將其保存到數據庫中?
在帳戶模型中創建使用者時,我目前看到以下內容:
public MembershipCreateStatus CreateUser(string userName, string password, string email) { if (String.IsNullOrEmpty(userName)) { throw new ArgumentException("Value cannot be null or empty.", "userName"); } if (String.IsNullOrEmpty(password)) { throw new ArgumentException("Value cannot be null or empty.", "password"); } if (String.IsNullOrEmpty(email)) { throw new ArgumentException("Value cannot be null or empty.", "email"); } MembershipCreateStatus status; _provider.CreateUser(userName, password, email, null, null, true, null, out status); return status; }在創建使用者功能中,我還想保存使用者的“地址”。
在register.aspx我添加了以下內容:
<div class="editor-label"> <%: Html.LabelFor(m => m.Address) %> </div> <div class="editor-field"> <%: Html.TextAreaFor(m => m.Address) %> </div>有任何想法嗎?
正如 jwsample 所指出的,您可以為此使用 Profile Provider。
或者,您可以創建自己的表來儲存其他使用者相關資訊。這需要做更多的工作,因為您需要創建自己的表並創建程式碼以在這些表中獲取和保存數據,但我發現以這種方式使用自定義表可以提供更大的靈活性和更多的可維護性而不是 Profile Provider(特別是預設的 Profile Provider,SqlProfileProvider,它以低效、非規範化的方式儲存配置文件數據)。
查看本教程,我將在其中介紹此過程:儲存其他使用者資訊。