diff --git a/TLSharp.Core/TelegramClient.cs b/TLSharp.Core/TelegramClient.cs index e5b0819f..98e0c5ee 100644 --- a/TLSharp.Core/TelegramClient.cs +++ b/TLSharp.Core/TelegramClient.cs @@ -207,6 +207,21 @@ public bool IsUserAuthorized() return request.Response.PhoneCodeHash; } + public async Task ResendCodeRequestAsync(string phoneNumber, string phoneCodeHash, CancellationToken token = default(CancellationToken)) + { + if (String.IsNullOrWhiteSpace(phoneNumber)) + throw new ArgumentNullException(nameof(phoneNumber)); + + if(String.IsNullOrWhiteSpace(phoneCodeHash)) + throw new ArgumentNullException(nameof(phoneCodeHash)); + + var request = new TLRequestResendCode() { PhoneNumber = phoneNumber, PhoneCodeHash = phoneCodeHash }; + + await RequestWithDcMigration(request, token).ConfigureAwait(false); + + return request.Response.PhoneCodeHash; + } + public async Task MakeAuthAsync(string phoneNumber, string phoneCodeHash, string code, CancellationToken token = default(CancellationToken)) { if (String.IsNullOrWhiteSpace(phoneNumber)) diff --git a/TLSharp.Tests.NUnit/Test.cs b/TLSharp.Tests.NUnit/Test.cs index 40e54ac6..b208a0bf 100644 --- a/TLSharp.Tests.NUnit/Test.cs +++ b/TLSharp.Tests.NUnit/Test.cs @@ -21,6 +21,12 @@ public async override Task AuthUser() await base.AuthUser(); } + [Test] + public async override Task AuthUserByResendCode() + { + await base.AuthUserByResendCode(); + } + [Test] public override async Task SendMessageTest() { diff --git a/TLSharp.Tests.VS/TLSharpTestsVs.cs b/TLSharp.Tests.VS/TLSharpTestsVs.cs index 8f6c58dc..b7877aab 100644 --- a/TLSharp.Tests.VS/TLSharpTestsVs.cs +++ b/TLSharp.Tests.VS/TLSharpTestsVs.cs @@ -20,6 +20,12 @@ public override async Task AuthUser() await base.AuthUser(); } + [TestMethod] + public override async Task AuthUserByResendCode() + { + await base.AuthUserByResendCode(); + } + [TestMethod] public override async Task SendMessageTest() { diff --git a/TLSharp.Tests/TLSharpTests.cs b/TLSharp.Tests/TLSharpTests.cs index 6acde729..4e2c00fa 100644 --- a/TLSharp.Tests/TLSharpTests.cs +++ b/TLSharp.Tests/TLSharpTests.cs @@ -155,6 +155,44 @@ public virtual async Task AuthUser() Assert.IsTrue(client.IsUserAuthorized()); } + public virtual async Task AuthUserByResendCode() + { + var client = NewClient(); + + await client.ConnectAsync(); + + var hash = await client.SendCodeRequestAsync(NumberToAuthenticate); + + var resendHash = await client.ResendCodeRequestAsync(NumberToAuthenticate, hash); + + var code = CodeToAuthenticate; // you can change code in debugger too + + if (String.IsNullOrWhiteSpace(code)) + { + throw new Exception("CodeToAuthenticate is empty in the app.config file, fill it with the code you just got now by SMS/Telegram"); + } + + TLUser user = null; + try + { + user = await client.MakeAuthAsync(NumberToAuthenticate, resendHash, code); + } + catch (CloudPasswordNeededException ex) + { + var passwordSetting = await client.GetPasswordSetting(); + var password = PasswordToAuthenticate; + + user = await client.MakeAuthWithPasswordAsync(passwordSetting, password); + } + catch (InvalidPhoneCodeException ex) + { + throw new Exception("CodeToAuthenticate is wrong in the app.config file, fill it with the code you just got now by SMS/Telegram", + ex); + } + Assert.IsNotNull(user); + Assert.IsTrue(client.IsUserAuthorized()); + } + public virtual async Task SendMessageTest() { NumberToSendMessage = ConfigurationManager.AppSettings[nameof(NumberToSendMessage)];