From 46265783977e3bc7adfa82f1ce701dee599a6307 Mon Sep 17 00:00:00 2001 From: Rockford Lhotka Date: Sun, 17 May 2020 21:33:25 -0500 Subject: [PATCH] Update for Blazor wasm RC --- .../Client/Pages/Login.razor | 14 ++--- .../Shared/CredentialValidator.cs | 56 +++++++++++++++++++ .../Shared/CustomIdentity.cs | 33 ----------- .../Shared/UserCredentials.cs | 5 ++ .../BlazorCslaExample.csproj | 8 +-- .../Pages/PersonEditMvvm.razor | 8 +-- 6 files changed, 76 insertions(+), 48 deletions(-) create mode 100644 BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CredentialValidator.cs delete mode 100644 BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CustomIdentity.cs diff --git a/BlazorCslaAuthentication/BlazorCslaAuthentication/Client/Pages/Login.razor b/BlazorCslaAuthentication/BlazorCslaAuthentication/Client/Pages/Login.razor index 520efa4..5c6064d 100644 --- a/BlazorCslaAuthentication/BlazorCslaAuthentication/Client/Pages/Login.razor +++ b/BlazorCslaAuthentication/BlazorCslaAuthentication/Client/Pages/Login.razor @@ -47,13 +47,13 @@ else private async void VerifyCredentials() { - var identity = await DataPortal.FetchAsync(vm.Model); - var baseidentity = new ClaimsIdentity(identity.AuthenticationType); - baseidentity.AddClaim(new Claim(ClaimTypes.Name, identity.Name)); - if (identity.Roles != null) - foreach (var item in identity.Roles) - baseidentity.AddClaim(new Claim(ClaimTypes.Role, item)); - var principal = new System.Security.Claims.ClaimsPrincipal(baseidentity); + var userInfo = await DataPortal.FetchAsync(vm.Model); + var identity = new ClaimsIdentity(userInfo.AuthenticationType); + identity.AddClaim(new Claim(ClaimTypes.Name, userInfo.Name)); + if (userInfo.Roles != null) + foreach (var item in userInfo.Roles) + identity.AddClaim(new Claim(ClaimTypes.Role, item)); + var principal = new System.Security.Claims.ClaimsPrincipal(identity); userService.CurrentUser = principal; StateHasChanged(); nav.NavigateTo("/"); diff --git a/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CredentialValidator.cs b/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CredentialValidator.cs new file mode 100644 index 0000000..fcff9b9 --- /dev/null +++ b/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CredentialValidator.cs @@ -0,0 +1,56 @@ +using System; +using Csla; + +namespace BlazorCslaAuthentication.Shared +{ + /// + /// Verifies the supplied user credentials and + /// returns information about the verified user. + /// + [Serializable] + public class CredentialValidator : ReadOnlyBase + { + public static readonly PropertyInfo NameProperty = RegisterProperty(nameof(Name)); + public string Name + { + get => GetProperty(NameProperty); + private set => LoadProperty(NameProperty, value); + } + + public static readonly PropertyInfo AuthenticationTypeProperty = RegisterProperty(nameof(AuthenticationType)); + public string AuthenticationType + { + get => GetProperty(AuthenticationTypeProperty); + private set => LoadProperty(AuthenticationTypeProperty, value); + } + + public static readonly PropertyInfo> RolesProperty = RegisterProperty>(nameof(Roles)); + public Csla.Core.MobileList Roles + { + get => GetProperty(RolesProperty); + private set => LoadProperty(RolesProperty, value); + } + + [Fetch] + private void Fetch(UserCredentials credentials) + { + // validate credentials here + if (!string.IsNullOrWhiteSpace(credentials.Username)) + { + Name = credentials.Username; + AuthenticationType = "Custom"; + Roles = new Csla.Core.MobileList + { + "StandardUser", + "PersonCreator" + }; + } + else + { + Name = string.Empty; + AuthenticationType = string.Empty; + Roles = new Csla.Core.MobileList(); + } + } + } +} diff --git a/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CustomIdentity.cs b/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CustomIdentity.cs deleted file mode 100644 index 5bcdba6..0000000 --- a/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/CustomIdentity.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using Csla; - -namespace BlazorCslaAuthentication.Shared -{ - [Serializable] - public class CustomIdentity : Csla.Security.CslaIdentityBase - { - [Fetch] - private void Fetch(UserCredentials credentials) - { - // validate credentials here - if (!string.IsNullOrWhiteSpace(credentials.Username)) - { - Name = credentials.Username; - IsAuthenticated = true; - AuthenticationType = "Custom"; - Roles = new Csla.Core.MobileList - { - "StandardUser", - "PersonCreator" - }; - } - else - { - Name = string.Empty; - IsAuthenticated = false; - AuthenticationType = string.Empty; - Roles = new Csla.Core.MobileList(); - } - } - } -} diff --git a/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/UserCredentials.cs b/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/UserCredentials.cs index c2fca1b..39b9264 100644 --- a/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/UserCredentials.cs +++ b/BlazorCslaAuthentication/BlazorCslaAuthentication/Shared/UserCredentials.cs @@ -4,6 +4,11 @@ namespace BlazorCslaAuthentication.Shared { + /// + /// Responsible for getting user credentials + /// from the user and acting as criteria for + /// the CredentialValidator type. + /// [Serializable] public class UserCredentials : BusinessBase { diff --git a/BlazorCslaExample/BlazorCslaExample/BlazorCslaExample.csproj b/BlazorCslaExample/BlazorCslaExample/BlazorCslaExample.csproj index d659da8..bbe6a42 100644 --- a/BlazorCslaExample/BlazorCslaExample/BlazorCslaExample.csproj +++ b/BlazorCslaExample/BlazorCslaExample/BlazorCslaExample.csproj @@ -5,11 +5,11 @@ - - + + - - + + diff --git a/BlazorCslaExample/BlazorCslaExample/Pages/PersonEditMvvm.razor b/BlazorCslaExample/BlazorCslaExample/Pages/PersonEditMvvm.razor index 832aaf9..34a8955 100644 --- a/BlazorCslaExample/BlazorCslaExample/Pages/PersonEditMvvm.razor +++ b/BlazorCslaExample/BlazorCslaExample/Pages/PersonEditMvvm.razor @@ -27,10 +27,10 @@ else - - - - + + + +