Ejemplo n.º 1
0
 public ActionResult Register()
 {
     using (DataAccessLayer.JobFinderDbContext db = new DataAccessLayer.JobFinderDbContext())
     {
         ViewBag.UserType = new SelectList(db.AspNetRoles.Where(m => m.Name != "SuperAdmin" && m.Name != "Admin").ToList(), "Name", "Name");
         return(View());
     }
 }
Ejemplo n.º 2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new ApplicationUser {
                    UserName = model.UserName, Email = model.Email, UserType = model.UserType
                };                                                                                                            // Part 9 10
                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>");


                    // Part 21
                    //-----------------------------
                    await UserManager.AddToRoleAsync(user.Id, model.UserType); // This function add a user to a role.

                    //-----------------------------

                    // Part 24 {https://stackoverflow.com/questions/30369157/user-isinrole-return-false}
                    //----------------------------
                    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);

                    await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);

                    //---------------------------

                    // MM "Token Module: - Set the cookie of the beaerer token of our WebApi project".
                    //---------------------------------------------------

                    HttpCookie BearerToken = Custom.Token.GetToken(new LoginViewModel {
                        Email = model.Email, Password = model.Password
                    });
                    if (BearerToken == null)
                    {
                        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ApplicationCookie);
                        ModelState.AddModelError("", "A Problem in login (token didn't generated), try again.");
                        return(View(model));
                    }

                    Response.Cookies.Add(BearerToken);

                    //---------------------------------------------------


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

            // If we got this far, something failed, redisplay form
            using (DataAccessLayer.JobFinderDbContext db = new DataAccessLayer.JobFinderDbContext())
            {
                ViewBag.UserType = new SelectList(db.AspNetRoles.Where(m => m.Name != "SuperAdmin" && m.Name != "Admin").ToList(), "Name", "Name");
                return(View(model));
            }
        }