J
There has not been a good solution to this problem anywhere, and there are slaves. Created. https://github.com/vknet/vk/issues/336 I don't know how to deal with this problem in the github'e library.I've solved the problem so far:
(1) When authorizing, we ask the user if he has double authentication; (2) If not, we are authorised without her; If included, authorise with her (user who does not have it, will not enter); (3) Profit.The code looks like,C#:private async void login()
{
Func<string> security = () =>
{
try
{
Authorization.TwoFactorNextView = System.Windows.Visibility.Hidden;
Authorization.TwoFactorView = System.Windows.Visibility.Visible;
while (string.IsNullOrWhiteSpace(Authorization.SecurityCode)) ;
return Authorization.SecurityCode;
}
finally
{
Authorization.TwoFactorNextView = System.Windows.Visibility.Visible;
Authorization.TwoFactorView = System.Windows.Visibility.Hidden;
Authorization.IsAuthSuccess = System.Windows.Visibility.Visible;
}
};
try
{
Authorization.IsLoginButtonEnable = false;
var vk = new VkNet.VkApi();
if (Authorization.IsTwoFactorAuth)
{
if (Authorization.IsCaptchaNeeded)
{
await vk.AuthorizeAsync(new VkNet.ApiAuthParams
{
ApplicationId = Authorization.AppID,
Login = Authorization.Email,
Password = Authorization.Password,
Settings = Authorization.Settings,
CaptchaSid = Authorization.CaptchaSid,
CaptchaKey = Authorization.Captcha,
TwoFactorAuthorization = security
});
}
else
{
await vk.AuthorizeAsync(new VkNet.ApiAuthParams
{
ApplicationId = Authorization.AppID,
Login = Authorization.Email,
Password = Authorization.Password,
Settings = Authorization.Settings,
//TwoFactorAuthorization = security
});
}
}
else
{
if (Authorization.IsCaptchaNeeded)
{
await vk.AuthorizeAsync(new VkNet.ApiAuthParams
{
ApplicationId = Authorization.AppID,
Login = Authorization.Email,
Password = Authorization.Password,
Settings = Authorization.Settings,
CaptchaSid = Authorization.CaptchaSid,
CaptchaKey = Authorization.Captcha,
});
}
else
{
await vk.AuthorizeAsync(new VkNet.ApiAuthParams
{
ApplicationId = Authorization.AppID,
Login = Authorization.Email,
Password = Authorization.Password,
Settings = Authorization.Settings,
});
}
}
VkNet.Model.User user = new VkNet.Model.User();
addToPlaylist(user, vk);
}
catch (VkNet.Exception.CaptchaNeededException cEx)
{
Authorization.IsLoginButtonEnable = true;
Authorization.IsCaptchaEnable = System.Windows.Visibility.Visible;
Authorization.CaptchaImgUri = cEx.Img.AbsoluteUri;
Authorization.CaptchaSid = cEx.Sid;
Authorization.IsCaptchaNeeded = true;
}
catch (VkNet.Exception.VkApiAuthorizationException eAx)
{
Authorization.IsLoginButtonEnable = true;
Authorization.IsAuthSuccess = System.Windows.Visibility.Visible;
//System.Windows.MessageBox.Show("Неверное имя пользователя или пароль! Попробуйте еще раз!", "Error");
}
}
XAML:<Window
x:Class="MP3_Player.View.AuthorizationView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:custom="clr-namespace:MP3_Player.Helpers"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:i="clr-namespace:System.Windows.Interactivity;assembly=System.Windows.Interactivity"
xmlns:local="clr-namespace:MP3_Player.View"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:MP3_Player.ViewModel"
Title="Авторизация Vkontakte"
Width="400"
Height="300"
Closing="Window_Closing"
ResizeMode="NoResize"
Visibility="{Binding Path=Authorization.Auth_visible}"
WindowStyle="ToolWindow"
mc:Ignorable="d">
<Window.DataContext>
<vm:AuthorizationViewModel />
</Window.DataContext>
<Grid>
<Grid Visibility="{Binding Authorization.TwoFactorNextView}">
<Grid.ColumnDefinitions>
<ColumnDefinition />
<ColumnDefinition />
</Grid.ColumnDefinitions>
<Grid.RowDefinitions>
<RowDefinition Height="60" />
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition Height="40" />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<StackPanel
Grid.Row="3"
Grid.ColumnSpan="2"
Orientation="Horizontal">
<CheckBox
Margin="87,0,0,0"
VerticalAlignment="Center"
Content="Двухэтапная аутентификация?"
IsChecked="{Binding Path=Authorization.IsTwoFactorAuth}" />
</StackPanel>
<TextBlock
Grid.Row="5"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
FontFamily="Yu Gothic UI Semibold"
FontSize="11"
Foreground="#FFA01414"
Text="Неверное имя пользователя или пароль! Попробуйте еще раз!"
Visibility="{Binding Path=Authorization.IsAuthSuccess}" />
<StackPanel
Grid.Row="4"
Grid.ColumnSpan="2"
Orientation="Horizontal">
<Image Width="200" Source="{Binding Path=Authorization.CaptchaImgUri}" />
<StackPanel
Width="194"
Orientation="Vertical"
Visibility="{Binding Path=Authorization.IsCaptchaEnable}">
<TextBlock HorizontalAlignment="Center" Text="Введите текст с картинки" />
<TextBox Text="{Binding Path=Authorization.Captcha, Mode=TwoWay}" />
</StackPanel>
</StackPanel>
<StackPanel Grid.Row="0" Grid.ColumnSpan="2">
<StackPanel
Margin="20,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center">
<TextBlock
Margin="0,5,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="14"
Foreground="Blue"
Text="Вход в систему" />
<Line
Margin="5"
Stroke="Blue"
StrokeThickness="0.1"
X1="-10"
X2="360">
<Line.Effect>
<DropShadowEffect />
</Line.Effect>
</Line>
</StackPanel>
<StackPanel
Margin="20,0,0,0"
HorizontalAlignment="Left"
VerticalAlignment="Center"
Orientation="Horizontal">
<TextBlock Text="Приложение " />
<TextBlock Foreground="Blue" Text="MP3 Player " />
<TextBlock Text="запрашивает доступ к вашему аккаунту" />
</StackPanel>
</StackPanel>
<TextBlock
Grid.Row="1"
Grid.Column="0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="E-mail или телефон:" />
<TextBox
Grid.Row="1"
Grid.Column="1"
Margin="5"
Text="{Binding Path=Authorization.Email}" />
<TextBlock
Grid.Row="2"
Grid.Column="0"
HorizontalAlignment="Right"
VerticalAlignment="Center"
Text="Введите пароль:" />
<PasswordBox
Grid.Row="2"
Grid.Column="1"
Margin="5"
PasswordChar="*">
<i:Interaction.Behaviors>
<custom:PasswordBehavior Password="{Binding Path=Authorization.Password, Mode=TwoWay}" />
</i:Interaction.Behaviors>
</PasswordBox>
<Button
Grid.Row="6"
Grid.Column="1"
Margin="5,5,100,5"
Background="#6084A9"
Command="{Binding Path=LoginCommand}"
Content="Войти"
FontFamily="Arial"
FontSize="11"
FontWeight="ExtraBold"
Foreground="White"
IsEnabled="{Binding Path=Authorization.IsLoginButtonEnable}" />
<Button
Grid.Row="6"
Grid.Column="1"
Margin="100,5,5,5"
Background="#6084A9"
Click="Button_Click"
Content="Отмена"
FontFamily="Arial"
FontSize="11"
FontWeight="ExtraBold"
Foreground="White" />
</Grid>
<Grid Visibility="{Binding Authorization.TwoFactorView}">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Grid.ColumnSpan="2"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Text="Введите код из SMS:" />
<TextBox
Grid.Row="1"
Height="40"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Text="{Binding Path=Authorization.SecurityCode, Mode=TwoWay}" />
<Button
Grid.Row="2"
Command="{Binding Path=KeyPressedCommand}"
Content="Подтвердить" />
</Grid>
</Grid>
https://github.com/alexandrmazur96/MP3_Player I'm on this github project. If anyone knows how to deal with this issue in a more compelling manner (using this library), I would be happy to see.