Wired up login form to API

Connected the login form button to the Authentication endpoint (/token). Gets back bearer token or exception if failed
This commit is contained in:
s459315 2022-07-08 17:31:58 +02:00
parent ad7cb45908
commit ab827d849c
8 changed files with 120 additions and 7 deletions

View File

@ -1,6 +1,17 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="api" value="https://localhost:44372/" />
</appSettings>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<runtime>
<assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
<dependentAssembly>
<assemblyIdentity name="Newtonsoft.Json" publicKeyToken="30ad4fe6b2a6aeed" culture="neutral" />
<bindingRedirect oldVersion="0.0.0.0-13.0.0.0" newVersion="13.0.0.0" />
</dependentAssembly>
</assemblyBinding>
</runtime>
</configuration>

View File

@ -29,7 +29,8 @@ namespace RMWPFUserInterface
_container
.Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>();
.Singleton<IEventAggregator, EventAggregator>()
.Singleton<IAPIHelper, APIHelper>();
GetType().Assembly.GetTypes()
.Where(type => type.IsClass)

View File

@ -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<AuthenticatedUser> Authenticate(string username, string password)
{
var data = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("grant_type", "password"),
new KeyValuePair<string, string>("username", username),
new KeyValuePair<string, string>("password", password),
});
using (HttpResponseMessage response = await apiClient.PostAsync("/Token", data))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<AuthenticatedUser>();
return result;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
}
}

View File

@ -0,0 +1,10 @@
using RMWPFUserInterface.Models;
using System.Threading.Tasks;
namespace RMWPFUserInterface.Helpers
{
public interface IAPIHelper
{
Task<AuthenticatedUser> Authenticate(string username, string password);
}
}

View File

@ -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; }
}
}

View File

@ -47,8 +47,15 @@
<Reference Include="Microsoft.Xaml.Behaviors, Version=1.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.Xaml.Behaviors.Wpf.1.1.19\lib\net45\Microsoft.Xaml.Behaviors.dll</HintPath>
</Reference>
<Reference Include="Newtonsoft.Json, Version=13.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.13.0.1\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http.Formatting, Version=5.2.9.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
<HintPath>..\packages\Microsoft.AspNet.WebApi.Client.5.2.9\lib\net45\System.Net.Http.Formatting.dll</HintPath>
</Reference>
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Runtime.Serialization.Primitives, Version=4.1.2.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL">
<HintPath>..\packages\System.Runtime.Serialization.Primitives.4.3.0\lib\net46\System.Runtime.Serialization.Primitives.dll</HintPath>
@ -74,7 +81,10 @@
<SubType>Designer</SubType>
</ApplicationDefinition>
<Compile Include="BootStrapper.cs" />
<Compile Include="Helpers\APIHelper.cs" />
<Compile Include="Helpers\IAPIHelper.cs" />
<Compile Include="Helpers\PasswordBoxHelper.cs" />
<Compile Include="Models\AuthenticatedUser.cs" />
<Compile Include="ViewModels\LoginViewModel.cs" />
<Compile Include="ViewModels\ShellViewModel.cs" />
<Compile Include="Views\LoginView.xaml.cs">
@ -123,8 +133,6 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<Folder Include="Models\" />
</ItemGroup>
<ItemGroup />
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -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);
}
}
}
}

View File

@ -2,6 +2,8 @@
<packages>
<package id="Caliburn.Micro" version="4.0.210" targetFramework="net472" />
<package id="Caliburn.Micro.Core" version="4.0.210" targetFramework="net472" />
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.9" targetFramework="net472" />
<package id="Microsoft.Xaml.Behaviors.Wpf" version="1.1.19" targetFramework="net472" />
<package id="Newtonsoft.Json" version="13.0.1" targetFramework="net472" />
<package id="System.Runtime.Serialization.Primitives" version="4.3.0" targetFramework="net472" />
</packages>