在ASP.Net MVC 3用户管理 [英] User Management in ASP.Net MVC 3

查看:202
本文介绍了在ASP.Net MVC 3用户管理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有使用ASP.Net MVC 3项目,现在我想让用户管理。
我想让它像这样的: http://mrgsp.md:8080/awesome/user

i have a project using ASP.Net MVC 3, and now i want to make user management. i want to make it like this one : http://mrgsp.md:8080/awesome/user

如何使用户管理?

非常感谢

推荐答案

我创建了一个模型,它引用了不过的MembershipUser还可以用于创建和编辑用户。

I created a Model which references the MembershipUser but also allows for creating and editing 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; }

        /* contructors based on string GUID or actual */
        public AccountUser() { }
        public AccountUser(string UID)
        {
            UserId = new Guid(UID);
            Initialize();
        }

        public AccountUser(Guid UID)
        {
            UserId = UID;
            Initialize();
        }

        /* loads Membership User into model to access other properties */
        public virtual MembershipUser User
        {
            get
            {
                // note that I don't have a test for null in here, 
                // but should in a real case.
                return Membership.GetUser(UserId);
            }
        }

        /* do this once when opening a user instead of every time you access one of these three *
         * as well as allow override when editing / creating                                    */
        private void Initialize()
        {
            UserName = User.UserName;
            Email = User.Email;
            IsApproved = User.IsApproved;
        }

    }
}

通过这个内置,我创建了一个控制器,带我的默认数据上下文允许它为我创造脚手架。然后,我删除了从控制器的上下文。

With this built, I created a Controller with my default data Context to allow it to create scaffolding 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)
        {
            //db.Dispose();
            base.Dispose(disposing);
        }
    }
}

该视图都应该是pretty直线前进,但这里有一个一致性

The Views should all be pretty straight forward, but here's 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 overall, mostly with getting the Model correct to allow you to read in from Membership as well as get a reasonable set of Views created. I actually did most of them by hand, but this will save you some time. Note that I omitted editing Password or Roles, but if you get this far, you shouldn't be too far off.

下面的联系是有益的:

  • Alternative User management in ASP.NET MVC
  • http://joymonscode.blogspot.com/2012/01/creating-simple-aspnet-mvc-3-razor.html

这篇关于在ASP.Net MVC 3用户管理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆