Ejemplo n.º 1
0
        public async Task <ActionResult> Register(EmpRegisterModel model)
        {
            if (ModelState.IsValid)
            {
                string hashPwd = UserManager.PasswordHasher.HashPassword(model.emppwd);
                var    user    = new ApplicationUser
                {
                    UserName      = model.empaccount,
                    Email         = model.EMPEMAIL,
                    PhoneNumber   = model.emptel,
                    NickName      = model.NickName,
                    PasswordHash  = hashPwd,
                    SecurityStamp = Guid.NewGuid().ToString()
                };
                using (var scope = new TransactionScope(TransactionScopeAsyncFlowOption.Enabled))
                {
                    try
                    {
                        var result = await UserManager.CreateAsync(user);

                        if (result.Succeeded)
                        {
                            EmpService empService = new EmpService();
                            model.empaccountstate = "0";
                            model.emppwd          = hashPwd;
                            empService.Create(model);

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

                            //角色名稱
                            var roleName = "Admin";

                            //判斷角色是否存在
                            if (RoleManager.RoleExists(roleName) == false)
                            {
                                //角色不存在,建立角色
                                var role = new IdentityRole(roleName);
                                await RoleManager.CreateAsync(role);
                            }
                            //將使用者加入該角色
                            await UserManager.AddToRoleAsync(user.Id, roleName);

                            scope.Complete();
                            return(RedirectToAction("Index", "Home", new { area = "Admin" }));
                        }
                        AddErrors(result);
                    }
                    catch (DbEntityValidationException ex)
                    {
                        logger.Error(GetEntityErrorMsg(ex));
                    }
                    catch (Exception ex)
                    {
                        logger.Error(ex.Message);
                        scope.Dispose();
                    }
                }
                #region 測試Rollback
                //var appDbContext = HttpContext.GetOwinContext().Get<ApplicationDbContext>();
                //using (var identitydbContextTransaction = appDbContext.Database.BeginTransaction())
                //{
                //    try
                //    {
                //        var result = await UserManager.CreateAsync(user, model.emppwd);
                //        if (result.Succeeded)
                //        {
                //            await SignInManager.SignInAsync(user, isPersistent: false, rememberBrowser: false);

                //            //角色名稱
                //            var roleName = "Admin";

                //            //判斷角色是否存在
                //            if (RoleManager.RoleExists(roleName) == false)
                //            {
                //                //角色不存在,建立角色
                //                var role = new IdentityRole(roleName);
                //                await RoleManager.CreateAsync(role);
                //            }
                //            //將使用者加入該角色
                //            await UserManager.AddToRoleAsync(user.Id, roleName);

                //            identitydbContextTransaction.Commit();
                //            return RedirectToAction("Index", "Account", new { area = "Admin" });
                //        }
                //        AddErrors(result);
                //    }
                //    catch (Exception)
                //    {
                //        identitydbContextTransaction.Rollback();
                //    }
                //}
                #endregion
            }
            return(View(model));
        }