Exemple #1
0
        public async Task <ActionResult> UpdateSettings(Settings settings)
        {
            var user = await UserManager.FindByIdAsync(User.Identity.GetUserId <int>());

            if (UserManager.PasswordHasher.VerifyHashedPassword(user.PasswordHash, settings.oldpassword)
                == PasswordVerificationResult.Success)
            {
                tbl_worker worker     = db.tbl_worker.Where(w => w.asp_user_Id == user.Id).FirstOrDefault();
                bool       isModified =
                    settings.UserName != user.UserName || settings.Email != user.Email ? true : false;
                if (user.UserName != settings.UserName)
                {
                    user.UserName = settings.UserName;
                }
                if (user.Email != settings.Email)
                {
                    user.Email = settings.Email;
                }
                if (isModified)
                {
                    var result = await UserManager.UpdateAsync(user);

                    if (!result.Succeeded)
                    {
                        return(Json(result.Errors, JsonRequestBehavior.AllowGet));
                    }
                }

                if (!String.IsNullOrEmpty(settings.newpassword))
                {
                    var result = await UserManager.ChangePasswordAsync(User.Identity.GetUserId <int>(), settings.oldpassword, settings.newpassword);

                    if (result.Succeeded)
                    {
                        if (user != null)
                        {
                            await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                        }
                    }
                }
                else
                {
                    if (user != null)
                    {
                        await SignInManager.SignInAsync(user, isPersistent : false, rememberBrowser : false);
                    }
                }
                return(Json("Success", JsonRequestBehavior.AllowGet));
            }
            else
            {
                return(Json("Invalid Password.", JsonRequestBehavior.AllowGet));
            }
        }
Exemple #2
0
        public async Task <ActionResult> Register(RegisterViewModel model)
        {
            if (ModelState.IsValid)
            {
                var user = new JobUser {
                    UserName = model.UserName, Email = model.Email
                };
                var result = await UserManager.CreateAsync(user, model.Password);

                if (result.Succeeded)
                {
                    if (model.IsWorker)
                    {
                        tbl_worker worker = new tbl_worker();
                        worker.asp_user_Id = user.Id;
                        db.tbl_worker.Add(worker);
                        await db.SaveChangesAsync();

                        try
                        {
                            await UserManager.AddToRoleAsync(user.Id, "Worker");
                        }
                        catch
                        {
                            tbl_asp_role role = new tbl_asp_role();
                            role.Name = "Worker";
                            db.tbl_asp_role.Add(role);
                            await db.SaveChangesAsync();

                            await UserManager.AddToRoleAsync(user.Id, "Worker");
                        }
                    }
                    else
                    {
                        tbl_company company = new tbl_company();
                        company.asp_user_Id = user.Id;
                        db.tbl_company.Add(company);
                        await db.SaveChangesAsync();

                        try
                        {
                            await UserManager.AddToRoleAsync(user.Id, "Company");
                        }
                        catch
                        {
                            tbl_asp_role role = new tbl_asp_role();
                            role.Name = "Company";
                            db.tbl_asp_role.Add(role);
                            await db.SaveChangesAsync();

                            await UserManager.AddToRoleAsync(user.Id, "Company");
                        }
                    }
                    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>");

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

            // If we got this far, something failed, redisplay form
            return(View(model));
        }