コード例 #1
0
        /// <summary>
        /// 登陆命令执行函数
        /// </summary>
        private void executeLoginCommand()
        {
            AuthenticateModel authenticateModel = new Models.AuthenticateModel()
            {
                UserNameOrEmailAddress = "admin",
                Password       = "******",
                RememberClient = true,
            };

            //var obj = Utility.HttpClientHelper.PostResponse<AjaxResponse<AuthenticateResultModel>>(
            //    Utility.ConfigHelper.GetAppSetting("ApiUri") + @"/TokenAuth/Authenticate",
            //    Utility.JsonHelper.ToJson(authenticateModel));


            //() => (Utility.Http.HttpClientHelper.PostResponse<OperationResult<PageResult<MenuModule>>>(GlobalData.ServerRootUri + "Identity/Login", Utility.JsonHelper.ToJson(LoginUser)))



            isCanExecuteLoginCommand = false;
            LoginCommand.RaiseCanExecuteChanged();



            Task.Factory.StartNew(() =>
            {
                try
                {
                    if (string.IsNullOrWhiteSpace(CurrentDataModel.Name))
                    {
                        UiMessage = "请输入用户名";
                        return;
                    }
                    if (string.IsNullOrWhiteSpace(CurrentDataModel.SecurityPassword))
                    {
                        UiMessage = "请输入密码";
                        return;
                    }
                    UiMessage = "正在核对用户名和密码...";
                    var user  = dbContext.UserDb.GetSingle(a => a.Name == CurrentDataModel.Name.Trim());
                    if (Equals(user, null))
                    {
                        UiMessage = $"未找到用户{CurrentDataModel.Name}";
                    }
                    else//找到用户,验证密码
                    {
                        if (CurrentDataModel.SecurityPassword.Trim() == Utility.Security.AESDecrypt(user.SecurityPassword))
                        {
                            UiMessage = "登陆成功,正在加载...";

                            LogHelper.Logger.Info($"{user?.Name}/{user?.NickName},登陆成功");

                            StaticData.CurrentUser = user;
                            Utility.ConfigHelper.AddAppSetting("IsRemberUserName", IsRemberUserName.ToString());
                            Utility.ConfigHelper.AddAppSetting("IsRemberUserPassword", IsRemberUserPassword.ToString());

                            if (IsRemberUserName)
                            {
                                Utility.ConfigHelper.AddAppSetting("RemberUserName", CurrentDataModel.Name);
                            }
                            else
                            {
                                Utility.ConfigHelper.AddAppSetting("RemberUserName", "");
                            }
                            if (IsRemberUserPassword)
                            {
                                Utility.ConfigHelper.AddAppSetting("RemberUserPassword", Utility.Security.AESEncrypt(CurrentDataModel.SecurityPassword.Trim()));
                            }
                            else
                            {
                                Utility.ConfigHelper.AddAppSetting("RemberUserPassword", "");
                            }

                            Application.Current.Dispatcher.Invoke(() =>
                            {
                                regionManager.RequestNavigate("ShellContentRegion", "ShellView");
                                Application.Current.Resources["TitlebarHeight"] = Utility.Windows.ResourceHelper.FindResource("TitlebarHeight2");
                                Application.Current.MainWindow.WindowState      = WindowState.Maximized;
                                Application.Current.MainWindow.ResizeMode       = ResizeMode.CanResize;
                            });
                        }
                        else
                        {
                            UiMessage = $"密码错误!";
                        }
                    }
                }
                catch (Exception ex)
                {
                    UiMessage = ex.Message;
                    LogHelper.Logger.Error($"{CurrentDataModel.Name}登陆异常!请与管理员联系!", ex);
                }
                finally
                {
                    isCanExecuteLoginCommand = true;
                    LoginCommand.RaiseCanExecuteChanged();
                }
            });
        }
コード例 #2
0
ファイル: LoginViewModel.cs プロジェクト: yuxianye/MES
        /// <summary>
        /// 登陆命令执行函数
        /// </summary>
        private void OnExecuteLoginCommand()
        {
            try
            {
                if (string.IsNullOrWhiteSpace(LoginUser.UserName))
                {
                    Message = "请输入用户名";
                    return;
                }
                if (string.IsNullOrWhiteSpace(LoginUser.PassWord))
                {
                    Message = "请输入密码";
                    return;
                }
                Message = "正在核对用户名和密码...";
#if DEBUG
                System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();
                stopwatch.Start();
#endif
                var result = Task.Factory.StartNew <OperationResult <PageResult <MenuModule> > >
                             (
                    () => (Utility.Http.HttpClientHelper.PostResponse <OperationResult <PageResult <MenuModule> > >(GlobalData.ServerRootUri + "Identity/Login", Utility.JsonHelper.ToJson(LoginUser)))
                             ).Result;
#if DEBUG
                stopwatch.Stop();
                Utility.LogHelper.Info("登陆用时(毫秒):" + stopwatch.ElapsedMilliseconds);
                Utility.LogHelper.Info("登陆内容:" + Utility.JsonHelper.ToJson(result));
#endif

                //Message = result?.Message;
                if (!Equals(result, null) && result.Successed)
                {
                    Message = "登陆成功,正在加载...";
                    GlobalData.CurrentLoginUser  = this.LoginUser;
                    GlobalData.CurrentUserModule = result.Data.Data;
                    if (result.Data.Data.Any())
                    {
                        Utility.ConfigHelper.AddAppSetting("IsRemberUserName", IsRemberUserName.ToString());
                        Utility.ConfigHelper.AddAppSetting("IsRemberUserPassword", IsRemberUserPassword.ToString());

                        if (IsRemberUserName)
                        {
                            Utility.ConfigHelper.AddAppSetting("RemberUserName", LoginUser.UserName);
                        }
                        else
                        {
                            Utility.ConfigHelper.AddAppSetting("RemberUserName", "");
                        }
                        if (IsRemberUserPassword)
                        {
                            Utility.ConfigHelper.AddAppSetting("RemberUserPassword", Utility.Secutiry.RsaHelper.Encryption(LoginUser.PassWord));
                        }
                        else
                        {
                            Utility.ConfigHelper.AddAppSetting("RemberUserPassword", "");
                        }


                        Messenger.Default.Send(LoginUser, MessengerToken.LoginSuccess);
                    }
                    else
                    {
                        Message = "未找到该用户可用的功能权限!请与管理员联系!";
                    }
                    GetToken(result.Message);
                }
                else
                {
                    //登陆失败,显示错误信息
                    Message = result == null ? "请确认与服务器连接是否正常!" : result.Message;
                }
            }
            catch (Exception ex)
            {
                Message = ex.Message;
                Utility.LogHelper.Error("登陆异常!请与管理员联系!", ex);
            }
        }