diff --git a/RMWPFUserInterface/App.config b/RMWPFUserInterface/App.config index 56efbc7..e3bc1cf 100644 --- a/RMWPFUserInterface/App.config +++ b/RMWPFUserInterface/App.config @@ -1,6 +1,17 @@ - + + + + + + + + + + + + \ No newline at end of file diff --git a/RMWPFUserInterface/BootStrapper.cs b/RMWPFUserInterface/BootStrapper.cs index cb3497f..b647721 100644 --- a/RMWPFUserInterface/BootStrapper.cs +++ b/RMWPFUserInterface/BootStrapper.cs @@ -29,7 +29,8 @@ namespace RMWPFUserInterface _container .Singleton() - .Singleton(); + .Singleton() + .Singleton(); GetType().Assembly.GetTypes() .Where(type => type.IsClass) diff --git a/RMWPFUserInterface/Helpers/APIHelper.cs b/RMWPFUserInterface/Helpers/APIHelper.cs new file mode 100644 index 0000000..a289a0a --- /dev/null +++ b/RMWPFUserInterface/Helpers/APIHelper.cs @@ -0,0 +1,53 @@ +using RMWPFUserInterface.Models; +using System; +using System.Collections.Generic; +using System.Configuration; +using System.Linq; +using System.Net.Http; +using System.Net.Http.Headers; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFUserInterface.Helpers +{ + public class APIHelper : IAPIHelper + { + private HttpClient apiClient; + public APIHelper() + { + InitializeClient(); + } + private void InitializeClient() + { + string api = ConfigurationManager.AppSettings["api"]; + + apiClient = new HttpClient(); + apiClient.BaseAddress = new Uri(api); + apiClient.DefaultRequestHeaders.Accept.Clear(); + apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json")); + } + + public async Task Authenticate(string username, string password) + { + var data = new FormUrlEncodedContent(new[] + { + new KeyValuePair("grant_type", "password"), + new KeyValuePair("username", username), + new KeyValuePair("password", password), + }); + + using (HttpResponseMessage response = await apiClient.PostAsync("/Token", data)) + { + if (response.IsSuccessStatusCode) + { + var result = await response.Content.ReadAsAsync(); + return result; + } + else + { + throw new Exception(response.ReasonPhrase); + } + } + } + } +} diff --git a/RMWPFUserInterface/Helpers/IAPIHelper.cs b/RMWPFUserInterface/Helpers/IAPIHelper.cs new file mode 100644 index 0000000..13c68c7 --- /dev/null +++ b/RMWPFUserInterface/Helpers/IAPIHelper.cs @@ -0,0 +1,10 @@ +using RMWPFUserInterface.Models; +using System.Threading.Tasks; + +namespace RMWPFUserInterface.Helpers +{ + public interface IAPIHelper + { + Task Authenticate(string username, string password); + } +} \ No newline at end of file diff --git a/RMWPFUserInterface/Models/AuthenticatedUser.cs b/RMWPFUserInterface/Models/AuthenticatedUser.cs new file mode 100644 index 0000000..a91f84c --- /dev/null +++ b/RMWPFUserInterface/Models/AuthenticatedUser.cs @@ -0,0 +1,14 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace RMWPFUserInterface.Models +{ + public class AuthenticatedUser + { + public string Access_Token { get; set; } + public string UserName { get; set; } + } +} diff --git a/RMWPFUserInterface/RMWPFUserInterface.csproj b/RMWPFUserInterface/RMWPFUserInterface.csproj index 29f8aab..4b42ed7 100644 --- a/RMWPFUserInterface/RMWPFUserInterface.csproj +++ b/RMWPFUserInterface/RMWPFUserInterface.csproj @@ -47,8 +47,15 @@ ..\packages\Microsoft.Xaml.Behaviors.Wpf.1.1.19\lib\net45\Microsoft.Xaml.Behaviors.dll + + ..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll + + + + ..\packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll + ..\packages\System.Runtime.Serialization.Primitives.4.3.0\lib\net46\System.Runtime.Serialization.Primitives.dll @@ -74,7 +81,10 @@ Designer + + + @@ -123,8 +133,6 @@ - - - + \ No newline at end of file diff --git a/RMWPFUserInterface/ViewModels/LoginViewModel.cs b/RMWPFUserInterface/ViewModels/LoginViewModel.cs index d546fa4..5e37bb9 100644 --- a/RMWPFUserInterface/ViewModels/LoginViewModel.cs +++ b/RMWPFUserInterface/ViewModels/LoginViewModel.cs @@ -1,4 +1,5 @@ using Caliburn.Micro; +using RMWPFUserInterface.Helpers; using System; using System.Collections.Generic; using System.Linq; @@ -11,6 +12,12 @@ namespace RMWPFUserInterface.ViewModels { private string _userName; private string _password; + private IAPIHelper _apiHelper; + + public LoginViewModel(IAPIHelper apiHelper) + { + _apiHelper = apiHelper; + } public string UserName { get { return _userName; } @@ -49,9 +56,16 @@ namespace RMWPFUserInterface.ViewModels } } - public void LogIn() + public async Task LogIn() { - Console.WriteLine(); + try + { + var result = await _apiHelper.Authenticate(UserName, Password); + } + catch (Exception ex) + { + Console.WriteLine(ex.Message); + } } } } diff --git a/RMWPFUserInterface/packages.config b/RMWPFUserInterface/packages.config index ce49f05..8e41d12 100644 --- a/RMWPFUserInterface/packages.config +++ b/RMWPFUserInterface/packages.config @@ -2,6 +2,8 @@ + + \ No newline at end of file