Ejemplo n.º 1
0
        //[Authorize(Roles ="Admin")]
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser
                {
                    UserName    = model.UserName,
                    Email       = model.Email,
                    FirstName   = model.FirstName,
                    LastName    = model.LastName,
                    Age         = model.Age,
                    HomeAddress = model.HomeAddress,
                    PhoneNumber = model.PhoneNumber
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    var IsAdmin = await UserManager.IsInRoleAsync(user.Id, "Admin");


                    // For more information on how to enable account confirmation and password reset please visit https://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>");

                    using (var context = new BookWormContext())
                    {
                        var roleManager = new RoleManager <IdentityRole>(new RoleStore <IdentityRole>(context));
                        //var UserManager = new UserManager<ApplicationUser>(new UserStore<ApplicationUser>(context));

                        var role        = context.Roles.SingleOrDefault(r => r.Name == "Admin");
                        var usersInRole = role.Users;
                        var AdminList   = roleManager.FindByName("Admin");
                        if (usersInRole.Count <= 0)
                        {
                            //meaning no admin yet... then register the user as an admin
                            await UserManager.AddToRoleAsync(user.Id, "Admin");
                        }
                        else
                        {
                            //admin space occupied.. register as "Registered User"
                            await UserManager.AddToRoleAsync(user.Id, "Registered");
                        }
                    }
                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    return(RedirectToAction("Index", "Home"));
                }
                AddErrors(result);
            }

            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Ejemplo n.º 2
0
 public DepartmentsController(BookWormContext context)
 {
     _context = context;
 }
Ejemplo n.º 3
0
 //Instance of the Database (_context)
 public HomeController()
 {
     _context = new BookWormContext();
 }
Ejemplo n.º 4
0
 public DocumentController()
 {
     _context = new BookWormContext();
 }
Ejemplo n.º 5
0
 public RoleController()
 {
     context = new BookWormContext();
 }
Ejemplo n.º 6
0
 public EmployeesController(BookWormContext context)
 {
     _context = context;
 }