Advertisement
Guest User

Untitled

a guest
May 23rd, 2018
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.27 KB | None | 0 0
  1. // Сетка с полями авторизации
  2.  
  3. <Grid FocusManager.FocusedElement="{Binding ElementName=UsernameTxtBox}">
  4.         <materialDesign:Card VerticalAlignment="Center" HorizontalAlignment="Center" Width="200" Height="200">
  5.             <Grid>
  6.                 <Border Background="#3A404D"
  7.                         BorderThickness="0,1,0,0">
  8.                     <StackPanel Grid.Row="1" Orientation="Vertical" HorizontalAlignment="Center"
  9.                                 VerticalAlignment="Top" Margin="0,10,0,0">
  10.                         <TextBox x:Name="UsernameTxtBox" Width="150" Margin="0,5"
  11.                                  materialDesign:HintAssist.Hint="Username"  
  12.                                  Text="{Binding UserName, UpdateSourceTrigger=PropertyChanged}">
  13.                             <TextBox.InputBindings>
  14.                                 <KeyBinding Command="{Binding LoginCommand}" Key="Return"/>
  15.                             </TextBox.InputBindings>
  16.                         </TextBox>
  17.                         <PasswordBox x:Name="Password" Width="150" Margin="0,5"
  18.                                  materialDesign:HintAssist.Hint="Password"  
  19.                                  local:PasswordBoxAssistant.BoundPassword="{Binding Path=Password, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
  20.                                  local:PasswordBoxAssistant.BindPassword="true">
  21.                             <PasswordBox.InputBindings>
  22.                                 <KeyBinding Command="{Binding LoginCommand}" Key="Return"/>
  23.                             </PasswordBox.InputBindings>
  24.                         </PasswordBox>
  25.                         <Button Content="Login" Margin="0,10,0,0"
  26.                                 Command="{Binding LoginCommand}"/>
  27.                        
  28.                         <Button Content="Create an account" Margin="0,10,0,0"
  29.                                 Style="{StaticResource AccountButton}"
  30.                                 Command="{Binding RegisterPageCommand}"/>
  31.                     </StackPanel>
  32.                 </Border>
  33.             </Grid>
  34.         </materialDesign:Card>
  35.     </Grid>
  36.  
  37. // авторизация
  38. private async Task<bool> Login()
  39.         {
  40.             try
  41.             {
  42.                 var user = new UserModel();
  43.                 var users = new List<User>();
  44.                 var chats = new List<ChatModel>();
  45.  
  46.                 using (var database = new MessengerContext())
  47.                 {
  48.                     chats = database.Chats.ToList();
  49.  
  50.                     user = await database.Users.FirstOrDefaultAsync(u => u.Name == UserName && u.Passhash == Password);
  51.  
  52.                     user.LastLogin = DateTime.Now.ToString();
  53.                     user.LastIP = IPGetter.GetLocalIPAddress();
  54.                     database.Entry(user).State = EntityState.Modified;
  55.  
  56.                     Email = user.Email;
  57.  
  58.                     if (user.ProfilePicture != null)
  59.                     {
  60.                         BitmapImage image = new BitmapImage();
  61.                         image.BeginInit();
  62.                         image.StreamSource = new MemoryStream(user.ProfilePicture);
  63.                         image.EndInit();
  64.  
  65.                         ImagePhoto = image;
  66.                     }
  67.  
  68.                     await database.SaveChangesAsync();
  69.                 }
  70.  
  71.                 if (users != null && user != null)
  72.                 {
  73.                     var names = chats.Select(c => c.Name);
  74.                     users = await chatService.LoginAsync(user.Name, Avatar());
  75.  
  76.                     users.ForEach(u =>
  77.                     {
  78.                         if (names.Contains(u.Name))
  79.                         {
  80.                             Participants.Add(new Participant
  81.                             {
  82.                                 Name = u.Name,
  83.                                 Photo = user.ProfilePicture
  84.                             });
  85.                         }
  86.                     });
  87.  
  88.                     UserMode = UserModes.Chat;
  89.                     IsLoggedIn = true;
  90.                 }
  91.  
  92.                 return true;
  93.             }
  94.             catch
  95.             {
  96.                 dialogService.ShowNotification("Correct your input!");
  97.  
  98.                 return false;
  99.             }
  100.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement