예제 #1
0
        /// <summary>
        /// 新增員工
        /// </summary>
        /// <param name="emp"></param>
        /// <returns></returns>
        public int Create(EmpRegisterModel emp)
        {
            try
            {
                var config = new MapperConfiguration(cfg =>
                {
                    cfg.CreateMap <EmpRegisterModel, EMPLOYEE>();
                });
                IMapper mapper = config.CreateMapper();
                var     dest   = mapper.Map <EmpRegisterModel, EMPLOYEE>(emp);

                Guid   userGuid     = System.Guid.NewGuid();
                byte[] dataAndGuid  = ASCIIEncoding.ASCII.GetBytes(emp.emppwd + userGuid.ToString());
                SHA512 sha512       = new SHA512CryptoServiceProvider();
                string resultSha512 = Convert.ToBase64String(sha512.ComputeHash(dataAndGuid));
                //dest.emppwd = resultSha512;
                dest.empguid = userGuid.ToString();

                using (AllShowEntities db = new AllShowEntities())
                {
                    db.EMPLOYEE.Add(dest);
                    return(db.SaveChanges());
                }
            }
            catch (DbEntityValidationException)
            {
                throw;
            }
            catch (Exception)
            {
                throw;
            }
        }
예제 #2
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));
        }
예제 #3
0
 /// <summary>
 /// 新增員工
 /// </summary>
 /// <param name="emp"></param>
 /// <returns></returns>
 public int Create(EmpRegisterModel emp)
 {
     return(empDao.Create(emp));
 }