コード例 #1
0
        private UserBE getUserFromDB(string username)
        {
            UserBE user = new UserBE();

            var connectionString = ConfigurationManager.ConnectionStrings["DemoCredentials"].ConnectionString;

            using (SqlConnection connection = new SqlConnection(connectionString))
            {
                string saltSaved = "select username, salt, pass from users where username = @username";

                using (SqlCommand command = new SqlCommand())
                {
                    command.Connection  = connection;
                    command.CommandType = CommandType.Text;
                    command.CommandText = saltSaved;
                    command.Parameters.Add("@username", SqlDbType.VarChar, 50).Value = username;

                    try
                    {
                        connection.Open();
                        using (SqlDataReader oReader = command.ExecuteReader())
                        {
                            if (oReader.Read())
                            {
                                user.user = oReader["username"].ToString();
                                user.salt = (byte[])oReader["salt"];
                                user.pass = (byte[])oReader["pass"];
                            }
                        }
                    }
                    catch (SqlException ex)
                    {
                        lblResultado.Text = ex.Message;
                    }
                    finally
                    {
                        connection.Close();
                    }
                }
            }

            return(user);
        }