Advertisement
seld1

VkAvaloniaAuth

Apr 21st, 2021
644
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.46 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Diagnostics;
  4. using System.Linq;
  5. using System.Reactive;
  6. using System.Text;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using ReactiveUI;
  10.  
  11.  
  12. namespace VKGroupPostsView.ViewModels
  13. {
  14.    public class VkLoginViewModel:ViewModelBase
  15.     {
  16.         private bool _LoginPanelVisible = true;
  17.         private bool _TwoFactorCodePanelVisible = false;
  18.         public ulong AppID = 6121396;
  19.         public string LoginText { get; set; }
  20.         public string PasswordText { get; set; }
  21.         public string? TwoAuthCodeText { get; set; } = null;
  22.         public bool TwoFactorCodePanelVisible {
  23.             get => _TwoFactorCodePanelVisible;
  24.             set
  25.             {
  26.                 this.RaiseAndSetIfChanged(ref _TwoFactorCodePanelVisible, value);
  27.             }
  28.         }
  29.         public bool LoginPanelVisible {
  30.             get => _LoginPanelVisible;
  31.             set
  32.             {
  33.                 this.RaiseAndSetIfChanged(ref _LoginPanelVisible, value);
  34.             }
  35.         }
  36.         public bool SendCode = false;
  37.        
  38.         public ReactiveCommand<Unit, Unit> AuthCommand { get; }
  39.         public ReactiveCommand<Unit, Unit> SendCodeCommand { get; }
  40.  
  41.         public VkLoginViewModel()
  42.         {
  43.               AuthCommand = ReactiveCommand.Create(()=>
  44.               {
  45.                   Auth();
  46.               });
  47.               SendCodeCommand = ReactiveCommand.Create(() =>
  48.               {
  49.                     SendCode = true;
  50.               });
  51.         }
  52.         public async void Auth()
  53.         {
  54.             VkNet.VkApi vkApi = new VkNet.VkApi();
  55.           var auth=  vkApi.AuthorizeAsync(new VkNet.Model.ApiAuthParams()
  56.             {
  57.                 ApplicationId = AppID,
  58.                 Login = LoginText,
  59.                 Password = PasswordText,
  60.                
  61.                 TwoFactorSupported = true,
  62.                 TwoFactorAuthorization = new Func<string?>(() =>
  63.                 {
  64.                     LoginPanelVisible = false;
  65.                     TwoFactorCodePanelVisible = true;
  66.                     Thread.Sleep(300);
  67.                    
  68.                     if(SendCode&TwoAuthCodeText!=null)
  69.                     {                
  70.                         string? code = TwoAuthCodeText;
  71.                         TwoAuthCodeText=null;
  72.                         SendCode = false;
  73.                         return code;
  74.                     }
  75.                       return "";
  76.                 })
  77.             }).GetAwaiter();
  78.             auth.OnCompleted(() =>
  79.             {
  80.                 try
  81.                 {
  82.                     auth.GetResult();
  83.                     Debug.WriteLine("AUTH OK");
  84.                  }
  85.                 catch (Exception ex) {
  86.                     Debug.WriteLine(ex.Message);
  87.                 }
  88.  
  89.             });
  90.            
  91.            
  92.         }
  93.     }
  94. }
  95.  
  96.  
  97. Window
  98.  
  99. <UserControl xmlns="https://github.com/avaloniaui"
  100.              xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
  101.              xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
  102.              xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
  103.                xmlns:vm="using:VKGroupPostsView.ViewModels"
  104.              mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
  105.              x:Class="VKGroupPostsView.Views.VkLoginView">
  106.     <UserControl.DataContext>
  107.         <vm:VkLoginViewModel></vm:VkLoginViewModel>
  108.     </UserControl.DataContext>
  109.     <Grid>
  110.         <TextBlock Margin="10"  Text="Авторизация" HorizontalAlignment="Center"></TextBlock>
  111.         <StackPanel IsVisible="{Binding  LoginPanelVisible}" Margin="50">
  112.             <TextBlock HorizontalAlignment="Center"  Text="Логин"></TextBlock>
  113.             <TextBox  Margin="0,20,0,0" Text="{Binding LoginText}"></TextBox>
  114.             <TextBlock HorizontalAlignment="Center" Margin="0,20,0,0" Text="Пароль"></TextBlock>
  115.             <TextBox Margin="0,20,0,0" Text="{Binding PasswordText}"></TextBox>
  116.             <Button Command="{Binding AuthCommand}" HorizontalAlignment="Center" Margin="20"> Войти</Button>
  117.         </StackPanel>
  118.         <StackPanel IsVisible="{ Binding TwoFactorCodePanelVisible}" Margin="50">
  119.             <TextBlock HorizontalAlignment="Center"  Text="Введите код авторизации"></TextBlock>
  120.             <TextBox  Margin="0,20,0,0" Text="{Binding TwoAuthCodeText}"></TextBox>
  121.             <Button Command="{Binding SendCodeCommand}" HorizontalAlignment="Center" Margin="20"> отправить</Button>
  122.         </StackPanel>
  123.     </Grid>
  124. </UserControl>
  125.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement