Added Model For Displaying LoggedInUsed and Catched Api call

This commit is contained in:
s459315 2022-07-22 16:45:33 +02:00
parent aa98988db1
commit 4e6ec2c41b
13 changed files with 208 additions and 9 deletions

View File

@ -4,19 +4,21 @@ using System.Web.Http;
using RMDataManagerLibrary.Models; using RMDataManagerLibrary.Models;
using System.Web; using System.Web;
using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity;
using System.Linq;
namespace RMDataManager.Controllers namespace RMDataManager.Controllers
{ {
[Authorize] [Authorize]
public class UserController : ApiController public class UserController : ApiController
{ {
public List<UserModel> GetById() [HttpGet]
public UserModel GetById()
{ {
string userId = RequestContext.Principal.Identity.GetUserId(); string userId = RequestContext.Principal.Identity.GetUserId();
UserData data = new UserData(); UserData data = new UserData();
return data.GetUserById(userId); return data.GetUserById(userId).First();
} }
} }
} }

View File

@ -1,4 +1,5 @@
using RMWPFUserInterface.Models; using RMWPFInterfaceLibrary.Models;
using RMWPFUserInterface.Models;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Configuration; using System.Configuration;
@ -8,14 +9,16 @@ using System.Net.Http.Headers;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
namespace RMWPFUserInterface.Helpers namespace RMWPFInterfaceLibrary.Api
{ {
public class APIHelper : IAPIHelper public class APIHelper : IAPIHelper
{ {
private HttpClient apiClient; private HttpClient apiClient;
public APIHelper() private ILoggedInUserModel _loggedInUser;
public APIHelper(ILoggedInUserModel loggedInUser)
{ {
InitializeClient(); InitializeClient();
_loggedInUser = loggedInUser;
} }
private void InitializeClient() private void InitializeClient()
{ {
@ -49,5 +52,32 @@ namespace RMWPFUserInterface.Helpers
} }
} }
} }
public async Task GetLogedInUserInfo(string token)
{
apiClient.DefaultRequestHeaders.Clear();
apiClient.DefaultRequestHeaders.Accept.Clear();
apiClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
apiClient.DefaultRequestHeaders.Add("Authorization", $"Bearer {token}");
using (HttpResponseMessage response = await apiClient.GetAsync("api/User"))
{
if (response.IsSuccessStatusCode)
{
var result = await response.Content.ReadAsAsync<LoggedInUserModel>();
_loggedInUser.CreatedDate = result.CreatedDate;
_loggedInUser.EmailAddress = result.EmailAddress;
_loggedInUser.FirstName = result.FirstName;
_loggedInUser.Id = result.Id;
_loggedInUser.LastName = result.LastName;
_loggedInUser.Token = token;
}
else
{
throw new Exception(response.ReasonPhrase);
}
}
}
} }
} }

View File

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

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

@ -0,0 +1,14 @@
using System;
namespace RMWPFInterfaceLibrary.Models
{
public interface ILoggedInUserModel
{
DateTime CreatedDate { get; set; }
string EmailAddress { get; set; }
string FirstName { get; set; }
string Id { get; set; }
string LastName { get; set; }
string Token { get; set; }
}
}

View File

@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RMWPFInterfaceLibrary.Models
{
public class LoggedInUserModel : ILoggedInUserModel
{
public string Token { get; set; }
public string Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string EmailAddress { get; set; }
public DateTime CreatedDate { get; set; }
}
}

View File

@ -0,0 +1,36 @@
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
// General Information about an assembly is controlled through the following
// set of attributes. Change these attribute values to modify the information
// associated with an assembly.
[assembly: AssemblyTitle("RMWPFInterfaceLibrary")]
[assembly: AssemblyDescription("")]
[assembly: AssemblyConfiguration("")]
[assembly: AssemblyCompany("")]
[assembly: AssemblyProduct("RMWPFInterfaceLibrary")]
[assembly: AssemblyCopyright("Copyright © 2022")]
[assembly: AssemblyTrademark("")]
[assembly: AssemblyCulture("")]
// Setting ComVisible to false makes the types in this assembly not visible
// to COM components. If you need to access a type in this assembly from
// COM, set the ComVisible attribute to true on that type.
[assembly: ComVisible(false)]
// The following GUID is for the ID of the typelib if this project is exposed to COM
[assembly: Guid("10b4a580-f9cf-4483-bf8b-02c5b20f182d")]
// Version information for an assembly consists of the following four values:
//
// Major Version
// Minor Version
// Build Number
// Revision
//
// You can specify all the values or you can default the Build and Revision Numbers
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]
[assembly: AssemblyVersion("1.0.0.0")]
[assembly: AssemblyFileVersion("1.0.0.0")]

View File

@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<Project ToolsVersion="15.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<Import Project="$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props" Condition="Exists('$(MSBuildExtensionsPath)\$(MSBuildToolsVersion)\Microsoft.Common.props')" />
<PropertyGroup>
<Configuration Condition=" '$(Configuration)' == '' ">Debug</Configuration>
<Platform Condition=" '$(Platform)' == '' ">AnyCPU</Platform>
<ProjectGuid>{10B4A580-F9CF-4483-BF8B-02C5B20F182D}</ProjectGuid>
<OutputType>Library</OutputType>
<AppDesignerFolder>Properties</AppDesignerFolder>
<RootNamespace>RMWPFInterfaceLibrary</RootNamespace>
<AssemblyName>RMWPFInterfaceLibrary</AssemblyName>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<Deterministic>true</Deterministic>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<DebugSymbols>true</DebugSymbols>
<DebugType>full</DebugType>
<Optimize>false</Optimize>
<OutputPath>bin\Debug\</OutputPath>
<DefineConstants>DEBUG;TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
<Optimize>true</Optimize>
<OutputPath>bin\Release\</OutputPath>
<DefineConstants>TRACE</DefineConstants>
<ErrorReport>prompt</ErrorReport>
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Newtonsoft.Json, Version=6.0.0.0, Culture=neutral, PublicKeyToken=30ad4fe6b2a6aeed, processorArchitecture=MSIL">
<HintPath>..\packages\Newtonsoft.Json.6.0.4\lib\net45\Newtonsoft.Json.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Configuration" />
<Reference Include="System.Core" />
<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.Xml.Linq" />
<Reference Include="System.Data.DataSetExtensions" />
<Reference Include="Microsoft.CSharp" />
<Reference Include="System.Data" />
<Reference Include="System.Net.Http" />
<Reference Include="System.Xml" />
</ItemGroup>
<ItemGroup>
<Compile Include="Api\APIHelper.cs" />
<Compile Include="Api\IAPIHelper.cs" />
<Compile Include="Models\AuthenticatedUser.cs" />
<Compile Include="Models\ILoggedInUserModel.cs" />
<Compile Include="Models\LoggedInUserModel.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Microsoft.AspNet.WebApi.Client" version="5.2.9" targetFramework="net472" />
<package id="Newtonsoft.Json" version="6.0.4" targetFramework="net472" />
</packages>

View File

@ -1,4 +1,6 @@
using Caliburn.Micro; using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFInterfaceLibrary.Models;
using RMWPFUserInterface.Helpers; using RMWPFUserInterface.Helpers;
using RMWPFUserInterface.ViewModels; using RMWPFUserInterface.ViewModels;
using System; using System;
@ -30,6 +32,7 @@ namespace RMWPFUserInterface
_container _container
.Singleton<IWindowManager, WindowManager>() .Singleton<IWindowManager, WindowManager>()
.Singleton<IEventAggregator, EventAggregator>() .Singleton<IEventAggregator, EventAggregator>()
.Singleton<ILoggedInUserModel, LoggedInUserModel>()
.Singleton<IAPIHelper, APIHelper>(); .Singleton<IAPIHelper, APIHelper>();
GetType().Assembly.GetTypes() GetType().Assembly.GetTypes()

View File

@ -81,8 +81,6 @@
<SubType>Designer</SubType> <SubType>Designer</SubType>
</ApplicationDefinition> </ApplicationDefinition>
<Compile Include="BootStrapper.cs" /> <Compile Include="BootStrapper.cs" />
<Compile Include="Helpers\APIHelper.cs" />
<Compile Include="Helpers\IAPIHelper.cs" />
<Compile Include="Helpers\PasswordBoxHelper.cs" /> <Compile Include="Helpers\PasswordBoxHelper.cs" />
<Compile Include="Models\AuthenticatedUser.cs" /> <Compile Include="Models\AuthenticatedUser.cs" />
<Compile Include="ViewModels\LoginViewModel.cs" /> <Compile Include="ViewModels\LoginViewModel.cs" />
@ -133,6 +131,11 @@
<ItemGroup> <ItemGroup>
<None Include="App.config" /> <None Include="App.config" />
</ItemGroup> </ItemGroup>
<ItemGroup /> <ItemGroup>
<ProjectReference Include="..\RMWPFInterfaceLibrary\RMWPFInterfaceLibrary.csproj">
<Project>{10b4a580-f9cf-4483-bf8b-02c5b20f182d}</Project>
<Name>RMWPFInterfaceLibrary</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" /> <Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project> </Project>

View File

@ -1,4 +1,5 @@
using Caliburn.Micro; using Caliburn.Micro;
using RMWPFInterfaceLibrary.Api;
using RMWPFUserInterface.Helpers; using RMWPFUserInterface.Helpers;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
@ -91,6 +92,10 @@ namespace RMWPFUserInterface.ViewModels
{ {
ErrorMessage = ""; ErrorMessage = "";
var result = await _apiHelper.Authenticate(UserName, Password); var result = await _apiHelper.Authenticate(UserName, Password);
// Capture more information
await _apiHelper.GetLogedInUserInfo(result.Access_Token);
} }
catch (Exception ex) catch (Exception ex)
{ {

View File

@ -11,6 +11,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMWPFUserInterface", "RMWPF
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMDataManagerLibrary", "RMDataManagerLibrary\RMDataManagerLibrary.csproj", "{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}" Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMDataManagerLibrary", "RMDataManagerLibrary\RMDataManagerLibrary.csproj", "{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}"
EndProject EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "RMWPFInterfaceLibrary", "RMWPFInterfaceLibrary\RMWPFInterfaceLibrary.csproj", "{10B4A580-F9CF-4483-BF8B-02C5B20F182D}"
EndProject
Global Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU Debug|Any CPU = Debug|Any CPU
@ -35,6 +37,10 @@ Global
{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU {6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Debug|Any CPU.Build.0 = Debug|Any CPU
{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU {6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Release|Any CPU.ActiveCfg = Release|Any CPU
{6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Release|Any CPU.Build.0 = Release|Any CPU {6669F7DC-4B07-497F-BDEE-5333DB3EDBF4}.Release|Any CPU.Build.0 = Release|Any CPU
{10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Debug|Any CPU.Build.0 = Debug|Any CPU
{10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Release|Any CPU.ActiveCfg = Release|Any CPU
{10B4A580-F9CF-4483-BF8B-02C5B20F182D}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection EndGlobalSection
GlobalSection(SolutionProperties) = preSolution GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE HideSolutionNode = FALSE