如何在asp.net mvc 5身份上添加2个用户类型进行注册

时间:2014-06-07 19:06:13

标签: asp.net-mvc-5 entity-framework-6

我有些如何设法为我的开源预订系统添加名字,姓氏和电子邮件属性,但我需要2种用户类型。默认用户类型应为客户端,第二类型应为Hostlers。可以通过下拉列表或单选按钮显示选项。我想知道2张登记表是否可以?它会造成任何冲突吗?或者只是浪费时间为2个类似的工作编写2个表格? 是将用户类型保存为用户角色还是应该作为用户类型完成,并且仅针对第三种用户类型使用角色(网站唯一管理员或内部注册的所有者)

我对默认asp.net身份的ApplicationUser类的修改

public class ApplicationUser:IdentityUser     {         [需要]         public string FirstName {get;组; }

    [Required]
    public string LastName { get; set; }

    [Required]
    public string Email { get; set; }
}

这里是register.cshtml (仅显示我添加的内容)

//  new properties 
    <div class="form-group">
        @Html.LabelFor(m => m.FirstName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.FirstName, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.LastName, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.LastName, new { @class = "form-control" })
        </div>
    </div>

    <div class="form-group">
        @Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
        <div class="col-md-10">
            @Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
        </div>
    </div>

这里是regiViewModels.cs

public class RegisterViewModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

        [Required]
        [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
        [DataType(DataType.Password)]
        [Display(Name = "Password")]
        public string Password { get; set; }

        [DataType(DataType.Password)]
        [Display(Name = "Confirm password")]
        [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
        public string ConfirmPassword { get; set; }

        // for first name
        [Required]
        [Display(Name = "First name")]
        public string FirstName { get; set; }
        //for last name
        [Required]
        [Display(Name = "Last name")]
        public string LastName { get; set; }
        //for email
        [Required]
        [StringLength(60, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 5)]
        [Display(Name = "Email")]
        public string Email { get; set; }

        // Return a pre-poulated instance of AppliationUser:
        public ApplicationUser GetUser()
        {
            var user = new ApplicationUser()
            {
                UserName = this.UserName,
                FirstName = this.FirstName,
                LastName = this.LastName,
                Email = this.Email

            };
            return user;
        }
    }

AccountController.cs

// POST: /Account/Register
        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser() { 
                    UserName = model.UserName,
                    FirstName = model.FirstName,
                    LastName = model.LastName,
                    Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);
                if (result.Succeeded)
                {
                    await SignInAsync(user, isPersistent: false);
                    return RedirectToAction("Index", "Home");
                }
                else
                {
                    AddErrors(result);
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

提前谢谢

1 个答案:

答案 0 :(得分:1)

我个人会使用“角色”来定义每个访问权限。这就是角色的用途。

考虑一下......如果您的酒店经营者还希望以用户身份预订客房,该怎么办?

所以角色可能是“可以预订房间”,“可以列出酒店”等等。但最终你必须弄清楚如何最好地管理这个。您可能会通过管理员帐户控制哪些用户是酒店经营者?