sochix / TLSharp

Telegram client library implemented in C#
1k stars 380 forks source link

Cant reuse session #904

Closed CryptoTradee closed 4 years ago

CryptoTradee commented 4 years ago

Seems I need to authenticate every single time as can't reuse the auth code from a previous session.

Below is the code I was using:

FileSessionStore fileSessionStore = new FileSessionStore(new DirectoryInfo("c:\")); TLSharp.Core.TelegramClient client = new TelegramClient(ApiId, ApiHash, fileSessionStore);

        if (!client.IsConnected)    //This actually returns true if using a recent session
            await client.ConnectAsync();    //However, this has error "Invalid Packet Length"

        if (!client.IsUserAuthorized())
        {
            var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
            var code = "<code_from_telegram>"; // you can change code in debugger

            var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);

        }

        //If skip the Connect above, get error here saying "Not Connected"
        TLDialogsSlice dialogs = (TLDialogsSlice) await client.GetUserDialogsAsync();
knocte commented 4 years ago

@CheshireCaat hey by any chance did you hit this bug?

CryptoTradee commented 4 years ago

It does seem like basic functionality and obviously want to minimise the number of times need to enter the auth code from Telegram. If it's a completely new session, it works ok but has the bugs indicated above if not.

knocte commented 4 years ago

@CryptoTradee could you debug it? it certainly seems this should work or has worked in the past

CryptoTradee commented 4 years ago

Without going into the source of TLSharp, yes I've debugged as much as I can

knocte commented 4 years ago

Well if it's a TLSharp bug, it needs to be debugged inside TLSharp. Just clone it instead of referencing the nuget.

CryptoTradee commented 4 years ago

I don't know anything about how TLSharp works and so even if I clone the repository, not sure I'll get far. Can use my code above and run the first time which will send the code to your Telegram and then run it again and can see if you get the same error?

knocte commented 4 years ago

Sorry I don't have time these days to hack in TLSharp. Should be easy to figure out why it's not picking up the settings from the configured dir (C:) and its session file.

CryptoTradee commented 4 years ago

If anyone runs into this problem, can modify the code above:

    FileSessionStore fileSessionStore = new FileSessionStore(new DirectoryInfo("c:\"));
    TLSharp.Core.TelegramClient client = new TelegramClient(ApiId, ApiHash, fileSessionStore);

    await client.ConnectAsync();    //However, this has error "Invalid Packet Length"

    if (!client.IsUserAuthorized())
    {
        var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
        var code = "<code_from_telegram>"; // you can change code in debugger

        var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);
        sessionStore.Save(client.Session);

    }

    //If skip the Connect above, get error here saying "Not Connected"
    TLDialogsSlice dialogs = (TLDialogsSlice) await client.GetUserDialogsAsync();
CheshireCaat commented 4 years ago

Hello guys. Hm, this bug looks weird. I've never had this before. But i think that it's not TLSharp bug.

There is one idea - there are not enough rights to store session data in the root of C:\ drive. @CryptoTradee try to store session in other path, if you have more than one logical drive. You also can try to save session to directory like C:\Telegram\xxxx\ etc.

Let me know if it helps.

CryptoTradee commented 4 years ago

The c:\ was just for example. I was storing in a different place on my code. Actually, fixed it by adding the line: sessionStore.Save(client.Session);

after the MakeAuthAsync

knocte commented 4 years ago

Wait, maybe this should be done automatically by TLSharp? What do you think @CheshireCaat ?

CheshireCaat commented 4 years ago

@CryptoTradee, @knocte There is no need for a manual call for 'sessionStore.Save()' according to the code. Take a look:

https://github.com/sochix/TLSharp/blob/90f9305dd62ff8cb821fbe5105bb69da70eee43d/TLSharp.Core/TelegramClient.cs#L427-L433

https://github.com/sochix/TLSharp/blob/90f9305dd62ff8cb821fbe5105bb69da70eee43d/TLSharp.Core/Session.cs#L170-L173

I.e. after successfuly auth session should be automatically saved. And by my own experience it works fine. I also tried CryptoTradee code without manual session saving - it works fine too. @CryptoTradee, are you using the latest version of the library?

knocte commented 4 years ago

it seems not all methods call OnUserAuthenticated, @CryptoTradee can you tell us what method were calling in your code so that we can fix TLSharp?

CryptoTradee commented 4 years ago

Why is session expires set to int.MaxValue? Maybe the session will never expire if keep requesting otherwise when the client is stopped, the session can expire. My code is above and it seems it does call OnUserAuthenticated from MakeAuthAsync. However, without the extra save, try run my code twice and will see the second time, it doens't work when don't send the code.

CheshireCaat commented 4 years ago

@CryptoTradee, as i said before i already tested your code, it's work fine for me. Main problem with this situation it's that i can't reproduce your problem, and can't help you without more additional information.

CryptoTradee commented 4 years ago

Oh, so if you run this below code, the first time, it will send the code to your Telegram and works fine. The 2nd time, it should skip the auth code and go straight to getting the dialogs?

    FileSessionStore fileSessionStore = new FileSessionStore(new DirectoryInfo("c:\\Programming\\"));
    TLSharp.Core.TelegramClient client = new TelegramClient(ApiId, ApiHash, fileSessionStore);

     await client.ConnectAsync();    //However, this has error "Invalid Packet Length"

    if (!client.IsUserAuthorized())
    {
        var hash = await client.SendCodeRequestAsync(NumberToAuthenticate);
        var code = "<code_from_telegram>"; // you can change code in debugger

        var user = await client.MakeAuthAsync(NumberToAuthenticate, hash, code);

    }

    //If skip the Connect above, get error here saying "Not Connected"
    TLDialogsSlice dialogs = (TLDialogsSlice) await client.GetUserDialogsAsync();
CheshireCaat commented 4 years ago

I copy-paste your code to my project again. Look at your code with small fixes for reading telegram code from console - https://pastebin.com/N3SvrSdQ

As I said many times, all is OK. It works fine. But i can get dialogs only with cast to TLDialogs, not to slice.

CryptoTradee commented 4 years ago

Ok, it seems fine now with the original code. However I still get an intermitten "cannot read packet length" error. I've gotten it on my local and on Azure. What do u want me to capture to help u fix that?

knocte commented 4 years ago

First, to open a new issue to not mix things up.

knocte commented 4 years ago

Ok, it seems fine now with the original code

What was the difference?