Beispiel #1
0
        public async Task ChangePassword(MyUserAdapterModel myUserAdapterModel, string newPassword,
                                         string ip)
        {
            CleanTrackingHelper.Clean <SystemEnvironment>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
            SystemEnvironment systemEnvironment = await context.SystemEnvironment
                                                  .OrderBy(x => x.Id)
                                                  .FirstOrDefaultAsync();

            string encodePassword =
                PasswordHelper.GetPasswordSHA(myUserAdapterModel.Salt, newPassword);

            myUserAdapterModel.Password = encodePassword;
            var myUser = Mapper.Map <MyUser>(myUserAdapterModel);

            CleanTrackingHelper.Clean <MyUser>(context);

            #region 更新下次要變更密碼的時間
            if (systemEnvironment.EnableCheckPasswordAge)
            {
                myUser.ForceChangePasswordDatetime = DateTime.Now
                                                     .AddDays(systemEnvironment.PasswordAge);
            }
            myUser.ForceChangePassword = false;
            #endregion

            context.Entry(myUser).State = EntityState.Modified;
            await context.SaveChangesAsync();

            if (systemEnvironment.EnablePasswordHistory == true)
            {
                MyUserPasswordHistory myUserPasswordHistory = new MyUserPasswordHistory()
                {
                    MyUserId = myUser.Id,
                    IP       = ip,
                    Password = myUser.Password,
                    ChangePasswordDatetime = DateTime.Now,
                };

                await context.AddAsync(myUserPasswordHistory);

                await context.SaveChangesAsync();

                while (true)
                {
                    #region 只會記錄下系統指定的變更密碼數量 systemEnvironment.PasswordHistory
                    var myUserPasswordHistories = await context.MyUserPasswordHistory
                                                  .Where(x => x.MyUserId == myUser.Id)
                                                  .OrderBy(x => x.ChangePasswordDatetime)
                                                  .ToListAsync();

                    if (myUserPasswordHistories.Count > systemEnvironment.PasswordHistory)
                    {
                        var first = myUserPasswordHistories.First();
                        context.Remove(first);
                        await context.SaveChangesAsync();

                        continue;
                    }
                    else
                    {
                        break;
                    }
                    #endregion
                }
            }
            CleanTrackingHelper.Clean <SystemEnvironment>(context);
            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
        }
Beispiel #2
0
        private async Task 建立使用者紀錄Async()
        {
            #region 建立使用者紀錄

            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
            #region 取得各種需要的角色
            var menuRole開發者 = await context.MenuRole
                              .AsNoTracking()
                              .FirstOrDefaultAsync(x => x.Name == MagicHelper.開發者功能表角色);

            var menuRole系統管理員 = await context.MenuRole
                                .AsNoTracking()
                                .FirstOrDefaultAsync(x => x.Name == MagicHelper.系統管理員功能表角色);

            var menuRole使用者 = await context.MenuRole
                              .AsNoTracking()
                              .FirstOrDefaultAsync(x => x.Name == MagicHelper.使用者功能表角色);

            #endregion

            #region 建立 開發者
            var myUser = new MyUser()
            {
                Account                     = $"god",
                Name                        = $"開發者",
                MenuRoleId                  = menuRole開發者.Id,
                Status                      = true,
                Salt                        = Guid.NewGuid().ToString(),
                ForceLogoutDatetime         = DateTime.Now.AddDays(-1),
                ForceChangePassword         = false,
                ForceChangePasswordDatetime = DateTime.Now.AddDays(42),
                LoginFailTimes              = 0,
                LoginFailUnlockDatetime     = DateTime.Now.AddDays(-1),
                LastLoginDatetime           = DateTime.Now,
                Email                       = "*****@*****.**",
            };

            myUser.Salt     = Guid.NewGuid().ToString();
            myUser.Password =
                PasswordHelper.GetPasswordSHA(myUser.Salt + "Vulcan", "abc");

            context.Add(myUser);
            await context.SaveChangesAsync();

            MyUserPasswordHistory myUserPasswordHistoryAdapterModel = new MyUserPasswordHistory()
            {
                MyUserId = myUser.Id,
                IP       = "",
                Password = myUser.Password,
                ChangePasswordDatetime = DateTime.Now,
            };

            await context.AddAsync(myUserPasswordHistoryAdapterModel);

            await context.SaveChangesAsync();

            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
            #endregion

            #region 建立 系統管理員
            var adminMyUser = new MyUser()
            {
                Account                     = $"{MagicHelper.系統管理員帳號}",
                Name                        = $"系統管理員 {MagicHelper.系統管理員帳號}",
                MenuRoleId                  = menuRole系統管理員.Id,
                Status                      = true,
                Salt                        = Guid.NewGuid().ToString(),
                ForceLogoutDatetime         = DateTime.Now.AddDays(-1),
                ForceChangePassword         = false,
                ForceChangePasswordDatetime = DateTime.Now.AddDays(42),
                LoginFailTimes              = 0,
                LoginFailUnlockDatetime     = DateTime.Now.AddDays(-1),
                LastLoginDatetime           = DateTime.Now,
                Email                       = "*****@*****.**",
            };
            var adminRawPassword = "******";
            adminMyUser.Password =
                PasswordHelper.GetPasswordSHA(adminMyUser.Salt, adminRawPassword);

            context.Add(adminMyUser);
            await context.SaveChangesAsync();

            myUserPasswordHistoryAdapterModel = new MyUserPasswordHistory()
            {
                MyUserId = adminMyUser.Id,
                IP       = "",
                Password = adminMyUser.Password,
                ChangePasswordDatetime = DateTime.Now,
            };

            await context.AddAsync(myUserPasswordHistoryAdapterModel);

            await context.SaveChangesAsync();

            CleanTrackingHelper.Clean <MyUser>(context);
            CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
            #endregion

            #region 建立 使用者
            foreach (var item in MagicHelper.使用者帳號)
            {
                var itemMyUser = await context.MyUser
                                 .AsNoTracking()
                                 .FirstOrDefaultAsync(x => x.Name == item);

                if (itemMyUser == null)
                {
                    itemMyUser = new MyUser()
                    {
                        Account                     = $"{item}",
                        Name                        = $"使用者 {item}",
                        MenuRoleId                  = menuRole使用者.Id,
                        Status                      = true,
                        Salt                        = Guid.NewGuid().ToString(),
                        ForceLogoutDatetime         = DateTime.Now.AddDays(-1),
                        ForceChangePassword         = false,
                        ForceChangePasswordDatetime = DateTime.Now.AddDays(42),
                        LoginFailTimes              = 0,
                        LoginFailUnlockDatetime     = DateTime.Now.AddDays(-1),
                        LastLoginDatetime           = DateTime.Now,
                        Email                       = "*****@*****.**",
                    };
                    var userRawPassword = "******";
                    itemMyUser.Password =
                        PasswordHelper.GetPasswordSHA(itemMyUser.Salt, userRawPassword);

                    context.Add(itemMyUser);
                    await context.SaveChangesAsync();

                    myUserPasswordHistoryAdapterModel = new MyUserPasswordHistory()
                    {
                        MyUserId = itemMyUser.Id,
                        IP       = "",
                        Password = itemMyUser.Password,
                        ChangePasswordDatetime = DateTime.Now,
                    };
                    await context.AddAsync(myUserPasswordHistoryAdapterModel);

                    await context.SaveChangesAsync();

                    CleanTrackingHelper.Clean <MyUser>(context);
                    CleanTrackingHelper.Clean <MyUserPasswordHistory>(context);
                }
            }
            #endregion
            #endregion
        }