Exemplo n.º 1
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            using (var context = new ApplicationDbContext())
            {
                if (ModelState.IsValid)
                {
                    var user = new ApplicationUser {
                        UserName = model.Email, Email = model.Email
                    };
                    var result = await UserManager.CreateAsync(user, model.Password);

                    var roleStore   = new RoleStore <IdentityRole>(context);
                    var roleManager = new RoleManager <IdentityRole>(roleStore);

                    var userStore   = new UserStore <ApplicationUser>(context);
                    var userManager = new UserManager <ApplicationUser>(userStore);
                    //if ((await userManager.FindByEmailAsync(model.Email) == null))
                    //{
                    if (int.Parse(Session["type"].ToString()) == 1)
                    {
                        userManager.AddToRole(user.Id, "user");
                        var obj    = new AddTitleAndQuestions();
                        var status = obj.AssignCompanyForUsers(user.Id, int.Parse(Session["CId"].ToString()));
                    }
                    if (int.Parse(Session["type"].ToString()) == 2)
                    {
                        userManager.AddToRole(user.Id, "admin");
                        var obj    = new AddTitleAndQuestions();
                        var status = obj.IsAssignRole(user.Id);
                    }
                    // }
                    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 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>");

                        if (int.Parse(Session["type"].ToString()) == 2)
                        {
                            return(RedirectToAction("MasterPage", "Base"));
                        }
                        else
                        {
                            return(RedirectToAction("Index", "Home"));
                        }
                    }
                    AddErrors(result);
                }
            }
            // If we got this far, something failed, redisplay form
            return(View(model));
        }
Exemplo n.º 2
0
        public ActionResult Register(int t)
        {
            Session["type"] = t;
            if (t == 1)
            {
                var userID = User.Identity.GetUserId();
                var obj    = new AddTitleAndQuestions();
                Session["CId"] = int.Parse(obj.getCompanyIdforAcc(userID));
            }

            return(View());
        }
Exemplo n.º 3
0
        public async Task <JsonResult> RegisterAllAsync(List <RegisterMutlipleUsers> model)
        {
            var successful = new List <RegisterMutlipleUsers>();
            var failed     = new List <RegisterMutlipleUsers>();
            var userID     = User.Identity.GetUserId();
            var obj        = new AddTitleAndQuestions();

            Session["CId"] = int.Parse(obj.getCompanyIdforAcc(userID));
            foreach (var eachuser in model)
            {
                using (var context = new ApplicationDbContext())
                {
                    var user = new ApplicationUser {
                        UserName = eachuser.Email, Email = eachuser.Email
                    };
                    var result = await UserManager.CreateAsync(user, eachuser.Password);

                    var roleStore   = new RoleStore <IdentityRole>(context);
                    var roleManager = new RoleManager <IdentityRole>(roleStore);
                    var userStore   = new UserStore <ApplicationUser>(context);
                    var userManager = new UserManager <ApplicationUser>(userStore);
                    userManager.AddToRole(user.Id, "user");
                    var status = obj.AssignCompanyForUsers(user.Id, int.Parse(Session["CId"].ToString()));
                    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 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>");
                        var a = new RegisterMutlipleUsers();
                        a.Email = eachuser.Email;
                        successful.Add(a);
                    }
                    else
                    {
                        var a = new RegisterMutlipleUsers();
                        a.Email = eachuser.Email;
                        failed.Add(a);
                    }
                }
            }
            return(Json(new { SuccessfullyRegistered = successful, FailedToRegister = failed }, JsonRequestBehavior.AllowGet));
        }