Advertisement
Guest User

tommonen

a guest
Jul 15th, 2016
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.76 KB | None | 0 0
  1. using DatabaseService.Models;
  2. using LipaRP.Core;
  3. using LipaRP.Security;
  4. using LipaRP.Services;
  5. using Prism.Commands;
  6. using Prism.Events;
  7. using System;
  8. using System.Threading;
  9. using System.Windows;
  10. using System.Windows.Controls;
  11.  
  12. namespace LipaRP.ViewModels
  13. {
  14. public class LoginWindowViewModel : ViewModelBase
  15. {
  16. public DelegateCommand<object> LoginCommand { get; set; }
  17. private readonly ILoginService _loginService;
  18. private readonly IEventAggregator _eventAggregator;
  19. public LoginWindowViewModel(ILoginService loginService, IEventAggregator eventAggregator)
  20. {
  21. _loginService = loginService;
  22. _eventAggregator = eventAggregator;
  23. LoginCommand = new DelegateCommand<object>(LoginUser);
  24. }
  25.  
  26. #region Properties
  27. private string _statusMessage;
  28. public string StatusMessage
  29. {
  30. get
  31. {
  32. return _statusMessage;
  33. }
  34. set
  35. {
  36. SetProperty(ref _statusMessage, value);
  37. }
  38. }
  39.  
  40. private string _userName;
  41. public string Username
  42. {
  43. get
  44. {
  45. return _userName;
  46. }
  47. set
  48. {
  49. SetProperty(ref _userName, value);
  50. }
  51. }
  52. #endregion
  53.  
  54. private void LoginUser(object obj)
  55. {
  56. // Passwordbox tuodaan objektina UI:sta
  57. PasswordBox passwordbox = obj as PasswordBox;
  58. string password = passwordbox.Password;
  59.  
  60. try
  61. {
  62. // Haetaan käyttäjätiedot servicestä
  63. var _user = _loginService.LoginUser(Username, password);
  64.  
  65. // Haetaan CustomPrincipal-objekti
  66. CustomPrincipal customPrincipal = Thread.CurrentPrincipal as CustomPrincipal;
  67. // Tarkastetaan, että CustomPrincipal-objekti on luotu sovelluksen käynnistyessä App.xaml.cs:ssä
  68. if(customPrincipal == null)
  69. {
  70. throw new ArgumentException("Sovelluksen CustomPrincipal objektia ei ole määritetty sovelluksen käynnistyksessä!");
  71. }
  72.  
  73. // Lisätään CustomPrincipalille käyttäjän identiteetti
  74. customPrincipal.Identity = new CustomIdentity(_user.Firstname + " "+ _user.Lastname, _user.Email);
  75.  
  76. // Viimeisenä suljetaan tämän hetkinen MainWindow
  77. Application.Current.MainWindow.Close();
  78.  
  79. }
  80. catch(Exception e)
  81. {
  82. StatusMessage = "Virhe sisäänkirjauksessa, kirjoita oikea käyttäjätunnus ja salasana";
  83. }
  84.  
  85. }
  86. }
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement