From my understanding is that when a user creates a new account in ASP.NET MVC, the method to handle this would be the Register method located in the AccountController class of any MVC Solution. This is how the current method of my class looks like:
// GET: /Account/Register [AllowAnonymous] public ActionResult Register() { return View(); } // // POST: /Account/Register [HttpPost] [AllowAnonymous] [ValidateAntiForgeryToken] public async Task<ActionResult> Register(RegisterViewModel model) { if (ModelState.IsValid) { var user = new ApplicationUser { UserName = model.Email, Email = model.Email }; var result = await UserManager.CreateAsync(user, model.Password); if (result.Succeeded) { await SignInManager.SignInAsync(user, isPersistent:false, rememberBrowser:false); // For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=320771 // Send an email with this link // string code = await UserManager.GenerateEmailConfirmationTokenAsync(user.Id); // var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: Request.Url.Scheme); // await UserManager.SendEmailAsync(user.Id, "Confirm your account", "Please confirm your account by clicking <a href=\"" + callbackUrl + "\">here</a>"); return RedirectToAction("Index", "Home"); } AddErrors(result); } // If we got this far, something failed, redisplay form return View(model); } However from what I noticed is that there is no code concerning dealing with Roles. My goal is to set a role for the first user to be created and make it an Admin role. All users that would register after that should differ from that role. How could I approach this in this code?
Edit: Would I have to use my Seed method of Migrations\Configuration.cs in order to achieve this instead? If so, how should my Seed method need to look like?
RoleId, thus adding a role as string format to a user would be strange? From what I noticed is that the interim table called AspNetUserRoles uses a propertyUserIdandRoleId. Knowing this I think a role has already to be set and assigned an Id accordingly I thought a User (who has aUserIdassinged), should get aRoleIdassigned? @RuardvanElburgUser(instead of a role with a null value) when a new user is created, so I can make my application working around authorization. Concerning the string part, I was not sure if that is a "secure" way of approaching role assignment but the latter part of your comment made sense.