0

I am trying to do a key system that redirects the user to a key that he can copy and put in the textbox to verify but it is freezing when i am trying to generate the link. Here is the code.

using System;
using System.Net;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using PastebinAPI;

namespace LoginKeyPastebin
{
    public partial class Form1 : Form
    {
        string paste_url = "";
        string key = "heydude";
        public Form1()
        {
            InitializeComponent();
            textBox1.PasswordChar = '\u25CF';
            textBox1.Text = "";
        }
        static int RandomNumber(int min, int max)
        {
            Random random = new Random();
            return random.Next(min, max);
        }
        static string RandomString(int size, bool lowerCase)
        {
            StringBuilder builder = new StringBuilder();
            Random random = new Random();
            char ch;
            for (int i = 0; i < size; i++)
            {
                ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
                builder.Append(ch);
            }
            if (lowerCase)
                return builder.ToString().ToLower();
            return builder.ToString();
        }
        static string RandomPassword()
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(RandomString(4, true));
            builder.Append(RandomNumber(1000, 9999));
            builder.Append(RandomString(2, false));
            return builder.ToString();
        }
        public string GetStringFromLink(string link)
        {
            WebClient client = new WebClient();
            string reply = client.DownloadString(link);
            return reply;
        }
        private void SubmitButton_Click(object sender, EventArgs e)
        {
            string user_key = textBox1.Text;
            if (user_key == "mypassword" || user_key == key)
            {
                //this.Hide();
                //ScriptExecutor se = new ScriptExecutor();
                //se.ShowDialog();
                //this.Close();
                MessageBox.Show("You got the right key!", "Yes");
            }
            else
            {
                MessageBox.Show("It's not the key!", "NOPE");
            }
        }

        private void GetKeyButton_Click(object sender, EventArgs e)
        {
            key = RandomPassword();
            GetPastebinLink(key).GetAwaiter().GetResult();
            System.Diagnostics.Process.Start(paste_url);
        }
        public async Task GetPastebinLink(string key)
        {
            Pastebin.DevKey = "mydevkey";
            User me = await Pastebin.LoginAsync("myusername", "mypassword");
            Paste newPaste = await me.CreatePasteAsync(key, "NewPloit key testing", Language.HTML5, Visibility.Unlisted, Expiration.TenMinutes);
            paste_url = newPaste.Url;
        }
    }
}

I suspect the problem is in the GetPastebinLink() function but i dont know why it is doing it. I ran a similar code but console-based and it worked perfectly.

Can you please help me figuring out the problem and solving it?

Thanks

Wabz
  • 1
  • Hi, I wonder if the problem is you're waiting for the result of the task on the UI thread in your button click callback. Perhaps this would be of interest https://stackoverflow.com/questions/40348204/how-to-update-ui-immediately-when-running-an-async-task https://stackoverflow.com/questions/38638911/how-to-update-gui-continuously-with-async – IronMan Mar 31 '20 at 00:14
  • Make `GetKeyButton_Click` `async` and `await GetPastebinLink(...)`: which should return `Task`. `paste_url` is the result of the method call. Don't use a field. Don't use `.GetAwaiter().GetResult()` in WinForms. – Jimi Mar 31 '20 at 00:17
  • It is OK to use `GetAwaiter().GetResult()` **or** `await` in the same WinForms app. Using them together opens the gates for deadlocks. [Here](https://blog.stephencleary.com/2012/07/dont-block-on-async-code.html) is explained why. – Theodor Zoulias Mar 31 '20 at 04:46

0 Answers0