Beispiel #1
0
        private void OnLogin(object obj)
        {
            IsBusy_Login  = true;
            LoginAble     = false;
            FailedMessage = string.Empty;
            string md5Password = HashHelper.ComputeStringMd5(Password);

            if (IsRememberPwd)
            {
                ShareWareSettings.Default.Password = Password;
                ShareWareSettings.Default.UserName = UserName;
                ShareWareSettings.Default.Save();
            }

            try
            {
                if (_hasLogin)
                {
                    _client.Logout();
                    _client.Close();

                    IsBusy_Login = false;
                    if (LogoutSuccess != null)
                    {
                        LogoutSuccess(this, null);
                    }
                }
                else
                {
                    Task task = new Task(() =>
                    {
                        _callBack = new CallBack(this);
                        _client   = new ShareServiceClient(new InstanceContext(_callBack));
                        _client.ClientCredentials.UserName.UserName = UserName;
                        _client.ClientCredentials.UserName.Password = HashHelper.ComputeStringMd5(Password);

                        _client.InnerChannel.Closing += InnerChannel_Closing;

                        try
                        {
                            _id = _client.Login(GetFirstMac());

                            if (_id < 0)
                            {
                                if (LoginFailed != null)
                                {
                                    LoginFailed(this, new ModelEventArgs(ModelEventType.ConnectMeesage)
                                    {
                                        FailedMessage = "用户名或密码错误"
                                    });
                                }
                                return;
                            }

                            if (LoginSuccess != null)
                            {
                                //LoginSuccess(this, null);
                                ExecuteEventAsync(this, null, LoginSuccess, null);
                            }

                            IsBusy_Login = false;
                            LoginAble    = true;
                        }
                        catch (FaultException)
                        {
                        }
                        catch (Exception)
                        {
                            ErrorOccur(this, new ModelEventArgs(ModelEventType.ConnectMeesage)
                            {
                                FailedMessage = "连接服务器出错"
                            });
                            IsBusy_Login = false;
                            LoginAble    = true;
                            return;
                        }
                    });
                    task.Start();
                }
            }
            catch (Exception e)
            {
                if (ErrorOccur != null)
                {
                    ErrorOccur(this, new ModelEventArgs(ModelEventType.Exception)
                    {
                        ModelException = e
                    });
                }
            }
        }