public async Task<User> AuthenticateAsync(string userName, string password, bool useSecureLogin = false) { User user = null; if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password)) { var client = this.GetHttpClient(useSecureLogin); try { TokenResponse = await client.RequestResourceOwnerPasswordAsync(userName, password); if (TokenResponse != null) { if (TokenResponse.IsError) { throw new UnauthorizedAccessException(this.m_strUnauthorizedAccessExceptionMessage); } Windows.Storage.ApplicationData.Current.RoamingSettings.Values["username"] = userName; Windows.Storage.ApplicationData.Current.RoamingSettings.Values["usesecurelogin"] = useSecureLogin; Windows.Security.Credentials.PasswordVault vault = new Windows.Security.Credentials.PasswordVault(); PasswordCredential passwordCredential = new PasswordCredential(PasswordVaultResourceName, userName, password); vault.Add(passwordCredential); if (passwordCredential != null) { user = new User { UserName = userName, UseSecureLogin = useSecureLogin }; m_settingsService.User = user; } } } catch (UnauthorizedAccessException) { throw new UnauthorizedAccessException(this.m_strUnauthorizedAccessExceptionMessage); } catch (Exception exception) { NullReferenceException nullReferenceException = exception as NullReferenceException; if (nullReferenceException != null) { //there could be a nullreference exception at account change when the login is encrypted. throw new UnauthorizedAccessException(this.m_strEncryptedLoginException); } throw exception; } } else { throw new UnauthorizedAccessException(this.m_strUnauthorizedAccessExceptionMessage); } return user; }
public async Task<User> VerifyUserCredentialsAsync() { User user = m_settingsService.User; PasswordVault vault = new PasswordVault(); try { await Task.Run(() => { var userName = m_settingsService.User?.UserName; if (!string.IsNullOrEmpty(userName)) { var passwordCredential = vault.Retrieve(PasswordVaultResourceName, userName); if (passwordCredential != null) { user = new User { UserName = userName, Password = vault.Retrieve(PasswordVaultResourceName, passwordCredential.UserName).Password, UseSecureLogin = Windows.Storage.ApplicationData.Current.RoamingSettings.Values["usesecurelogin"] != null ? (bool)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["usesecurelogin"] : false }; } } }); } catch { } return user; }