Robin
Robin

Reputation: 485

How should I code registration with 3 different user types in asp.net MVC5 EF6?

I want to have 3 different types of users but I am confused how to do it in new Identity in MVC5 with EF6.

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

RegisterViewModel.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;
        }
    }

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>

ApplicationUser class

public class ApplicationUser : IdentityUser {

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

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

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

Upvotes: 1

Views: 211

Answers (1)

Brendan Green
Brendan Green

Reputation: 11904

You can use claims in place of roles: Using Claims in ASP.NET Identity

You can apply the claims during the registration process, or as part of an administration function.

Here is how I used it in my application - when a specific type of user registers, I add a claim for their account type (this is in the standard ASP.NET Identity Register() action):

await UserManager.AddClaimAsync(user.Id, new Claim(ClaimTypes.Role, "MyRole"));

All I need to do now is add the Authorize attribute to my controllers to grant access to my user types:

[Authorize(Roles = "MyRole")]

Upvotes: 1

Related Questions