예제 #1
0
        public static ClientCredential Login(Action cancelCallback, DesktopClientSettings settings,
            bool alwaysShowDialog = false)
        {
            var crypto = Crypto.CreateDefaultCrypto();
            Profile profile;
            var profileFile = Directories.GetFullName(Constants.ProfileFileName);
            var profileDirectory = Path.GetDirectoryName(profileFile);
            if (string.IsNullOrEmpty(profileDirectory))
                throw new InvalidOperationException("The directory name is invalid (empty).");
            if (!Directory.Exists(profileDirectory))
            {
                Directory.CreateDirectory(profileDirectory);
                profile = new Profile();
            }
            else
            {
                if (File.Exists(profileFile))
                {
                    try
                    {
                        profile = Profile.Load(profileFile);
                    }
                    catch
                    {
                        profile = new Profile();
                    }
                }
                else
                {
                    profile = new Profile();
                }
            }

            ClientCredential credential = null;
            var selectedUserProfile = profile.UserProfiles.FirstOrDefault(p => p.IsSelected);
            var selectedServerProfile = profile.ServerProfiles.FirstOrDefault(p => p.IsSelected);
            if (alwaysShowDialog || selectedUserProfile == null || !selectedUserProfile.AutoLogin)
            {
                var loginForm = new FrmLogin(profile, settings, profileFile);
                var dialogResult = loginForm.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    credential = loginForm.Credential;
                }
                else
                {
                    cancelCallback();
                }
            }
            else
            {
                credential = new ClientCredential
                {
                    Password = crypto.Decrypt(selectedUserProfile.Password),
                    ServerUri = selectedServerProfile == null ? string.Empty : selectedServerProfile.Uri,
                    UserName = selectedUserProfile.UserName
                };
            }
            return credential;
        }
예제 #2
0
        public static ClientCredential Login(Action cancelCallback, DesktopClientSettings settings,
                                             bool alwaysShowDialog = false)
        {
            var     crypto = Crypto.CreateDefaultCrypto();
            Profile profile;
            var     profileFile      = Directories.GetFullName(Constants.ProfileFileName);
            var     profileDirectory = Path.GetDirectoryName(profileFile);

            if (string.IsNullOrEmpty(profileDirectory))
            {
                throw new InvalidOperationException("The directory name is invalid (empty).");
            }
            if (!Directory.Exists(profileDirectory))
            {
                Directory.CreateDirectory(profileDirectory);
                profile = new Profile();
            }
            else
            {
                if (File.Exists(profileFile))
                {
                    try
                    {
                        profile = Profile.Load(profileFile);
                    }
                    catch
                    {
                        profile = new Profile();
                    }
                }
                else
                {
                    profile = new Profile();
                }
            }

            ClientCredential credential = null;
            var selectedUserProfile     = profile.UserProfiles.FirstOrDefault(p => p.IsSelected);
            var selectedServerProfile   = profile.ServerProfiles.FirstOrDefault(p => p.IsSelected);

            if (alwaysShowDialog || selectedUserProfile == null || !selectedUserProfile.AutoLogin)
            {
                var loginForm    = new FrmLogin(profile, settings, profileFile);
                var dialogResult = loginForm.ShowDialog();
                if (dialogResult == DialogResult.OK)
                {
                    credential = loginForm.Credential;
                }
                else
                {
                    cancelCallback();
                }
            }
            else
            {
                credential = new ClientCredential
                {
                    Password  = crypto.Decrypt(selectedUserProfile.Password),
                    ServerUri = selectedServerProfile == null ? string.Empty : selectedServerProfile.Uri,
                    UserName  = selectedUserProfile.UserName
                };
            }
            return(credential);
        }