public SignInFlyoutViewModel(IAccountService accountService, IAlertMessageService alertMessageService, IResourceLoader resourceLoader)
        {
            _accountService = accountService;
            _alertMessageService = alertMessageService;
            _resourceLoader = resourceLoader;
            if (accountService != null)
            {
                _lastSignedInUser = _accountService.SignedInUser;
            }

            SignInCommand = DelegateCommand.FromAsyncHandler(SignInAsync, CanSignIn);
        }
        public SignOutFlyoutViewModel(IAccountService accountService, INavigationService navigationService)
        {
            _accountService = accountService;
            _navigationService = navigationService;
            if (_accountService != null)
            {
                _userInfo = _accountService.SignedInUser;
            }

            if (_userInfo != null)
            {
                UserName = _userInfo.UserName;
            }

            SignOutCommand = new DelegateCommand(SignOut, CanSignOut);
        }
        public AccountService(IIdentityService identityService, ISessionStateService sessionStateService, ICredentialStore credentialStore)
        {
            _identityService = identityService;
            _sessionStateService = sessionStateService;
            _credentialStore = credentialStore;
            if (_sessionStateService != null)
            {
                if (_sessionStateService.SessionState.ContainsKey(SignedInUserKey))
                {
                    _signedInUser = _sessionStateService.SessionState[SignedInUserKey] as UserInfo;
                }

                if (_sessionStateService.SessionState.ContainsKey(UserNameKey))
                {
                    _userName = _sessionStateService.SessionState[UserNameKey].ToString();
                }

                if (_sessionStateService.SessionState.ContainsKey(PasswordKey))
                {
                    _password = _sessionStateService.SessionState[PasswordKey].ToString();
                }
            }
        }
 public void RaiseUserChanged(UserInfo newUserInfo, UserInfo oldUserInfo)
 {
     UserChanged(this, new UserChangedEventArgs(newUserInfo, oldUserInfo));
 }
 public UserChangedEventArgs(UserInfo newUserInfo, UserInfo oldUserInfo)
 {
     NewUserInfo = newUserInfo;
     OldUserInfo = oldUserInfo;
 }
 private void RaiseUserChanged(UserInfo newUserInfo, UserInfo oldUserInfo)
 {
     var handler = UserChanged;
     if (handler != null)
     {
         handler(this, new UserChangedEventArgs(newUserInfo, oldUserInfo));
     }
 }
        public void SignOut()
        {
            var previousUser = _signedInUser;
            _signedInUser = null;
            _userName = null;
            _password = null;

            _sessionStateService.SessionState.Remove(SignedInUserKey);
            _sessionStateService.SessionState.Remove(UserNameKey);
            _sessionStateService.SessionState.Remove(PasswordKey);

            // remove user from the CredentialStore, if any
            _credentialStore.RemoveSavedCredentials(PasswordVaultResourceName);

            RaiseUserChanged(_signedInUser, previousUser);
        }
        public async Task<bool> SignInUserAsync(string userName, string password, bool useCredentialStore)
        {
            try
            {
                var result = await _identityService.LogOnAsync(userName, password);
                UserInfo previousUser = _signedInUser;
                _signedInUser = result.UserInfo;

                // Save SignedInUser in the StateService
                _sessionStateService.SessionState[SignedInUserKey] = _signedInUser;

                // Save username and password in state service
                _userName = userName;
                _password = password;
                _sessionStateService.SessionState[UserNameKey] = userName;
                _sessionStateService.SessionState[PasswordKey] = password;
                if (useCredentialStore)
                {
                    // Save credentials in the CredentialStore
                    _credentialStore.SaveCredentials(PasswordVaultResourceName, userName, password);

                    // Documentation on managing application data is at http://go.microsoft.com/fwlink/?LinkID=288818&clcid=0x409
                }

                if (previousUser == null)
                {
                    // Raise use changed event if user logged in
                    RaiseUserChanged(_signedInUser, previousUser);
                }
                else if (_signedInUser != null && _signedInUser.UserName != previousUser.UserName)
                {
                    // Raise use changed event if user changed
                    RaiseUserChanged(_signedInUser, previousUser);
                }

                return true;
            }
            catch (Exception)
            {
            }

            return false;
        }