I created a model that references MemberhipUser, but also allows you to create and edit users.
namespace MyProject.Models { public class AccountUser { [Key, DatabaseGenerated(DatabaseGeneratedOption.None)] public virtual Guid UserId { get; set; } [Display(Name="User Name")] public virtual string UserName { get; set; } [Display(Name = "E-mail")] public virtual string Email { get; set; } public virtual string Password { get; set; } [Display(Name = "Approved")] public virtual bool IsApproved { get; set; } public AccountUser() { } public AccountUser(string UID) { UserId = new Guid(UID); Initialize(); } public AccountUser(Guid UID) { UserId = UID; Initialize(); } public virtual MembershipUser User { get {
With this built-in, I created a controller with my default context data so that it can create forests for me. Then I removed the Context from the controller.
namespace MyProject.Controllers { [Authorize] public class AccountUserController : Controller { public ViewResult Index() { var memberList = Membership.GetAllUsers(); var model = new List<AccountUser>(); foreach (MembershipUser user in memberList) { model.Add(new AccountUser(user.ProviderUserKey.ToString())); } return View(model); } public ViewResult Details(Guid id) { AccountUser accountuser = new AccountUser(id); return View(accountuser); } public ActionResult Create() { return View(); } [HttpPost] public ActionResult Create(AccountUser myUser) { if (ModelState.IsValid) { Membership.CreateUser(myUser.UserName, myUser.Password, myUser.Email); return RedirectToAction("Index"); } return View(myUser); } public ActionResult Edit(Guid id) { AccountUser accountuser = new AccountUser(id); return View(accountuser); } [HttpPost] public ActionResult Edit(AccountUser accountuser) { if (ModelState.IsValid) { return RedirectToAction("Index"); } return View(accountuser); } public ActionResult Delete(Guid id) { AccountUser accountuser = new AccountUser(id); return View(accountuser); } [HttpPost, ActionName("Delete")] public ActionResult DeleteConfirmed(Guid id) { AccountUser accountuser = new AccountUser(id); Membership.DeleteUser(accountuser.User.UserName); return RedirectToAction("Index"); } protected override void Dispose(bool disposing) {
The views should be simple enough, but here is one for consistency
@model MyProject.Models.AccountUser @{ ViewBag.Title = "Create"; } <h2>Create</h2> <script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script> <script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script> @using (Html.BeginForm()) { @Html.ValidationSummary(true) <fieldset> <legend>AccountUser</legend> <div class="editor-label"> @Html.LabelFor(model => model.UserName) </div> <div class="editor-field"> @Html.EditorFor(model => model.UserName) @Html.ValidationMessageFor(model => model.UserName) </div> <div class="editor-label"> @Html.LabelFor(model => model.Password) </div> <div class="editor-field"> @Html.PasswordFor(model => model.Password) @Html.ValidationMessageFor(model => model.Password) </div> <div class="editor-label"> @Html.LabelFor(model => model.Email) </div> <div class="editor-field"> @Html.EditorFor(model => model.Email) @Html.ValidationMessageFor(model => model.Email) </div> <p> <input type="submit" value="Create" /> </p> </fieldset> } <div> @Html.ActionLink("Back to List", "Index") </div>
It was definitely a little tricky, mostly with the right model, allowing you to read from membership and also get a reasonable set of created views. I actually made most of them manually, but it will save you time. Please note that I skipped editing the password or roles, but if you get to this, you should not be too far away.
The following links were helpful:
source share