How to add 2 user types for registration on asp.net mvc 5 identity

asp.net-mvc-5 entity-framework-6

Question

I some how managed to add first name,last name and email properties for registration for my open source booking system but I want 2 user types. the default user type should be clients and second type should be Hostlers. The options can be shown by dropdown or radio buttons. i would like know if it is ok to have 2 registration form? Will it create any conflicts? Or will it just be waste of time to code 2 forms for 2 similar job? Should the user type be saved as user roles or should be done as user type and use role for only 3rd user type(website sole admin or owners registered internally)

my modification to default asp.net identity's ApplicationUser class

public class ApplicationUser : IdentityUser { [Required] public string FirstName { get; set; }

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

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

here is register.cshtml (showing only what i added)

//  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>

here is 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);
        }

thank you in advance

1
0
6/7/2014 7:13:20 PM

Popular Answer

Personally I would use "Roles" to define what access each has. This is what roles are for.

Think about it... what if one of your hoteliers also wants to book rooms as a user?

So roles might be "Can book rooms", "Can list hotels", etc. But ultimately you'll have to figure out how best to manage this. It's likely that you'll control which users are hoteliers via your admin account right?

1
6/8/2014 5:52:57 PM


Related Questions





Related

Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow