Exemplo n.º 1
0
 public async Task<TunesUser> VerifyUserCredentials()
 {
     TunesUser tunesUser = null;
     Windows.Security.Credentials.PasswordVault vault = new Windows.Security.Credentials.PasswordVault();
     try
     {
         await Task.Run(() =>
         {
             var userName = (string)Windows.Storage.ApplicationData.Current.RoamingSettings.Values["username"];
             if (!string.IsNullOrEmpty(userName))
             {
                 var passwordCredential = vault.Retrieve(PasswordVaultResourceName, userName);
                 if (passwordCredential != null)
                 {
                     tunesUser = this.User = new TunesUser
                     {
                         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 tunesUser;
 }
Exemplo n.º 2
0
        public async Task<TunesUser> SignInUser(string userName, string password, bool useSecureLogin)
        {
            TunesUser tunesUser = null;
            if (!string.IsNullOrEmpty(userName) && !string.IsNullOrEmpty(password))
            {
                var client = this.GetHttpClient(useSecureLogin);
                try
                {
                    this.TokenResponse = await client.RequestResourceOwnerPasswordAsync(userName, password);
                    if (this.TokenResponse != null)
                    {
                        if (this.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)
                        {
                            tunesUser = this.User = new TunesUser
                            {
                                UserName = userName,
                                Password = vault.Retrieve(PasswordVaultResourceName, passwordCredential.UserName).Password,
                                UseSecureLogin = useSecureLogin
                            };
                        }
                    }
                }
                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 tunesUser;
        }