por favor, Podem me ajudar ?

09/11/2011

0

Olá, sou novo por aqui, estou com um probleminha. Preciso modificar o codigo abaixo para usar com um banco de dados ao inves de XML, será que alguém sabe como fazer isso, ou pelo menos me dar uma dica de como ? Fico muito agradecido. Quem puder me ajudar, faça o mais rápido possivel, preciso com urgencia.
Obrigado.
aqui vai o código;



private bool UserExists(string UserName)
{
//Create a data set to read the XML file.
DataSet dsUsers = new DataSet();
//Use error handling in case is file missing.
try
{
//Build the Users.xml file path.
string strXMLFile = Server.MapPath(.) + \\Users.xml;
//Read the file
dsUsers.ReadXml(strXMLFile, XmlReadMode.InferSchema);
//For each row in the Users table.
foreach (DataRow rowUser in dsUsers.Tables[Users].Rows)
{
//Check for name match.
if (rowUser[name].ToString() == UserName)
{
return true;
}
}
}
catch
{
//In case of error return False.
return false;
}
// Otherwise, return false
return false;
}

private bool PasswordValid(string UserName, string Password)
{
//Create a data set to read the XML file.
DataSet dsUsers = new DataSet();
//Use error handling in case is file missing.
try
{
// Build the Users.xml file path.
string strXMLFile = Server.MapPath(.) + \\Users.xml;
// Read the file
dsUsers.ReadXml(strXMLFile, XmlReadMode.InferSchema);
// For each row in the Users table.
foreach (DataRow rowUser in dsUsers.Tables[Users].Rows)
{
// Check for name match.
if (rowUser[name].ToString() == UserName)
{
if (rowUser[password].ToString() ==
FormsAuthentication.HashPasswordForStoringInConfigFile(Password, SHA1))
{
return true;
}
}
}
}
catch
{
// In case of error return False.
return false;
}
return false;
}
Willian Carvalho

Willian Carvalho

Responder

Posts

10/11/2011

Evandro Lopes

Willian bom dia, fiz um código hiper rapido e bem simples, nao cheguei nem mesmo a testar mas o caminho é exatamente esse q fiz, nao sei dizer se por ventura o HashPasswordForStoringInConfigFile irá funcionar, porém isso são detalhes que você pode verificar.


        /// <summary>
        /// 
        /// </summary>
        /// <param name=UserName></param>
        /// <param name=Password></param>
        /// <returns></returns>
        private bool PasswordValid(string UserName, string Password)
        {
            try
            {
                //Verifica se o usuario existe passando como parametro o usuario e a senha discriptografada
                if (UserExists(UserExists, FormsAuthentication.HashPasswordForStoringInConfigFile(Password, SHA1)))
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }
            catch
            {
                // In case of error return False. 
                return false;
            }
            return false;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name=UserName></param>
        /// <param name=Password></param>
        /// <returns></returns>
        private bool UserExists(string UserName, string Password)
        {
            //Cria a conexão com banco de dados
            SqlConnection conexao = new SqlConnection(string de conexao com BD);

            try
            {
                //cria a consulta para verificar usuario e senha
                string select = string.Format(SELECT COUNT(*) as qtdUsuario FROM TABELA WHERE usuario =  AND password = , UserName, Password);

                SqlCommand comm = new SqlCommand(select, conexao);

                if (conexao.State == ConnectionState.Closed)
                {
                    conexao.Open();
                }

                using (IDataReader reader = comm.ExecuteReader())
                {
                    while (reader.Read())
                    {
                        if (reader.GetInt32(reader.GetOrdinal(qtdUsuario)) > 0)
                        {
                            return true;
                        }
                        else
                        {
                            return false;
                        }
                    }
                }
            }
            catch
            {
                //In case of error return False. 
                return false;
            }
            finally 
            {
                if (conexao.State == ConnectionState.Open())
                {
                    conexao.Close();
                }
            }
            // Otherwise, return false 
            return false;
        }
Responder

10/11/2011

Willian Carvalho

Bom dia, agradeço a ajuda do Evandro, só tive que fazer umas modificações no codigo pra adaptar ao meu projeto, muito obrigado.
Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar