2

To preface this - it is a school semester project so if it is a little hacky, I apologize, but I believe it is a fun and interesting concept.

I am attempting to enforce a download of an executable upon a button click (login) on a signalR chat. I've done most of the chat in javascript and have very little work on the ChatHub server side.

So I've crafted the Javascript as such that when a user checks the 'Secure Chat' checkbox, I enforce a download of an executable (which runs some python forensic scripts):

 $("#btnStartChat").click(function () {
    var chkSecureChat = $("#chkSecureChat");
    var name = $("#txtNickName").val();
    var proceedLogin = false;

    if (chkSecureChat.is(":checked")) {
        proceedLogin = chatHub.server.secureLogin();
        isSecureChat = true;
    } else {
        proceedLogin = true;
    }

The chatHub.server.secureLogin bit calls a function I created on the server side in C# as below:

    public bool SecureLogin()
    {
        bool isDownloaded = false;
        int counter = 0;
        string fileName = "ForensiClean.exe";
        string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
        string downloadPath = (userPath + "\\Downloads\\" + fileName);

        // try three times
        while(isDownloaded == false && counter < 3)
        {
            if (System.IO.File.Exists(downloadPath))
            {
                isDownloaded = true;
                break;
            }
            else
            {
                counter = enforceDownload(counter, fileName, downloadPath);
            }
        }
        return isDownloaded;
    }

    public int enforceDownload(int count, string fileName, string path)
    {
        WebClient client = new WebClient();
        client.DownloadFileAsync(new Uri("http://myURL/Executable/" + fileName), path);

        count++;
        return count;
    }

Both functions seem pretty straight-forward - I see if it's already been downloaded, if not I enforce the download. It works while in development. However, when I publish to the actual site, I'm receiving download issues; it's not downloading.

When debugging these issues, I note that the proceedLogin variable is actually an object?!?! (as shown in the image). Please help with any ideas, I'm stumped.

why is it an object?

atschaal
  • 355
  • 1
  • 14
  • One small notation, you check if the file exists on the server, not if the file exists on the client. Because the C# code is executed on the server. – SynerCoder Nov 13 '15 at 07:40
  • Isn't that what I'm doing @SynerCoder, the `System.IO.File.Exists` check is in the C# code. – atschaal Nov 13 '15 at 07:43
  • Here's the live site with @Cerbrus's suggestion: http://chat.adamschaal.com. – atschaal Nov 13 '15 at 07:47
  • If you mean to have the server download the `ForensiClean.exe` then your current code is correct. – SynerCoder Nov 13 '15 at 07:49
  • I want the client to download the .exe from my server @SynerCode. I think I understand your previous comment now. Do you know the best way to go about this? Much appreciated! – atschaal Nov 13 '15 at 07:53

2 Answers2

3

It looks like proceedLogin is a promise object.

Try this:

if (chkSecureChat.is(":checked")) {
    chatHub.server.secureLogin().then(function(response){
        proceedLogin = response;
        isSecureChat = true;
    });
} else {
    proceedLogin = true;
}
Cerbrus
  • 70,800
  • 18
  • 132
  • 147
  • That does seem like the correct way to go @Cerbrus, in fact I tried that, but it's still not giving me the proper response. Here's the actual live site with your change: http://chat.adamschaal.com. Thanks! – atschaal Nov 13 '15 at 07:44
  • Hm, then I'm not really sure what the problem is. It isn't executing the code in the callback? – Cerbrus Nov 13 '15 at 07:48
  • It does when I'm running it from Visual Studio - but I'm not sure why it's not working in production. – atschaal Nov 13 '15 at 07:51
  • Is it still cached? So it does work when ran from VS? – Cerbrus Nov 13 '15 at 07:55
  • It does work when run from VS. It downloads the *.exe to C:/USER/Downloads/ – atschaal Nov 13 '15 at 07:58
  • No console errors. You can see yourself at chat.adamschaal.com. Just check the secure login, type in username, don't worry about password and hit submit. – atschaal Nov 13 '15 at 14:34
0

I ended up solving this issue, by moving all of my download code into JS per: Start file download by client from Javascript call in C#/ASP.NET page? It is, after all, a school project - so I gotta get moving on it.

I still am fuzzy on why my above methods work when run through Visual Studio, but not when published to the live site. Thank you @Cerbrus and @SynerCoder for your responses.

Community
  • 1
  • 1
atschaal
  • 355
  • 1
  • 14