Skip to content

Commit

Permalink
ADor
Browse files Browse the repository at this point in the history
 Added a new component called Component1.razor which is defined in the Dgmjr.Blazor.Components library.

ADDED: Component1.razor.css
- Added a new CSS file called Component1.razor.css that styles the Component1 component.

ADDED: Dgmjr.Blazor.Components.csproj
- Added a new project file called Dgmjr.Blazor.Components.csproj.

ADDED: EnumerationSelectList.cs
- Added a new class called EnumerationSelectList<TEnumeration, TIEnumeration> which is a dropdown list component.

ADDED: ExampleJsInterop.cs
- Added a new class called ExampleJsInterop that provides an example of how JavaScript functionality can be wrapped in a .NET class.

ADDED: LICENSE.md Added a new license file called LICENSE.md.

ADDED: UnknownYesNoSelectList.cs
- Added a new class called UnknownYesNoSelectList which is a dropdown list component with options for unknown, yes, and no.

ADDED: _Imports.razor
- Added a new file called _Imports.razor that imports the Microsoft.AspNetCore.Components.Web namespace.

ADDED: icon.png
- Added a new image file called icon.png.

ADDED: wwwroot/background.png
- Added a new image file called background.png in the wwwroot folder.

ADDED: wwwroot/exampleJs.js
- Added a JavaScript file called exampleJsInterop.js in wwwroot folder.

UPDATED: BlazorSecurityConstants.cs
- Added new constant strings for account controller URIs.

UPDATED: AccountController.cs
- Added HTTP endpoints for login and logout AccountControllerUPDATED: ApplicationAuthenticationState.cs
- Updated the ApplicationAuthenticationState class to be a readonly record struct.

UPDATED: ApplicationUser.cs
- Removed unused using statements in the ApplicationUser class.

UPDATED: SecurityServiceCollectionExtensions.cs
 the Add toazorService.

UPDATED: ApplicationAuthenticationStateProvider.cs
- Added a using statement for the Dgmjr.Blazor.Services namespace.

UPDATED: SecurityService- Added new using statements for the Radzen namespace.
- Replaced hardcoded URIs with the Uris class.
- Added XML documentation to the Security.
  • Loading branch information
dgmjr committed Mar 2, 2024
1 parent cf94bfe commit f8f9739
Show file tree
Hide file tree
Showing 18 changed files with 206 additions and 53 deletions.
3 changes: 3 additions & 0 deletions src/Blazor.Components/Component1.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<div class="my-component">
This component is defined in the <strong>Dgmjr.Blazor.Components</strong> library.
</div>
6 changes: 6 additions & 0 deletions src/Blazor.Components/Component1.razor.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
.my-component {
border: 2px dashed red;
padding: 1em;
margin: 1em 0;
background-image: url('background.png');
}
26 changes: 26 additions & 0 deletions src/Blazor.Components/Dgmjr.Blazor.Components.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<Project Sdk="Microsoft.NET.Sdk.Razor">

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<TargetFrameworks>net8.0</TargetFrameworks>
<Nullable>enable</Nullable>
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>


<ItemGroup>
<SupportedPlatform Include="browser" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="Microsoft.AspNetCore.Components.Web" />
<PackageReference Include="Blazorise.Components" />
<PackageReference Include="Dgmjr.Abstractions" />
</ItemGroup>

<ItemGroup>
<PackageFile Include="./icon.png" />
<PackageFile Include="./bin/$(Configuration)/$(TargetFramework)/*.*" PackagePath="lib/$(TargetFramework)/%(Filename)%(Extension)" />
</ItemGroup>

</Project>
14 changes: 14 additions & 0 deletions src/Blazor.Components/EnumerationSelectList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Dgmjr.Blazor.Components;
using System.Linq;
using System.Collections;

public class EnumerationSelectList<TEnumeration, TIEnumeration> : Blazorise.Components.DropdownList<TIEnumeration, string>
where TIEnumeration : IHaveAShortName, IHaveADisplayName
{
public EnumerationSelectList()
{
this.Data = (typeof(TEnumeration).GetMethod("GetAll").Invoke(null, null) as IEnumerable).OfType<TIEnumeration>().ToList();
this.TextField = item => item.DisplayName;
this.ValueField = item => item.ShortName;
}
}
36 changes: 36 additions & 0 deletions src/Blazor.Components/ExampleJsInterop.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// using Microsoft.JSInterop;

// namespace Dgmjr.Blazor.Components;

// // This class provides an example of how JavaScript functionality can be wrapped
// // in a .NET class for easy consumption. The associated JavaScript module is
// // loaded on demand when first needed.
// //
// // This class can be registered as scoped DI service and then injected into Blazor
// // components for use.

// public class ExampleJsInterop : IAsyncDisposable
// {
// private readonly Lazy<Task<IJSObjectReference>> moduleTask;

// public ExampleJsInterop(IJSRuntime jsRuntime)
// {
// moduleTask = new (() => jsRuntime.InvokeAsync<IJSObjectReference>(
// "import", "./_content/Dgmjr.Blazor.Components/exampleJsInterop.js").AsTask());
// }

// public async ValueTask<string> Prompt(string message)
// {
// var module = await moduleTask.Value;
// return await module.InvokeAsync<string>("showPrompt", message);
// }

// public async ValueTask DisposeAsync()
// {
// if (moduleTask.IsValueCreated)
// {
// var module = await moduleTask.Value;
// await module.DisposeAsync();
// }
// }
// }
35 changes: 35 additions & 0 deletions src/Blazor.Components/LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
---
date: 2023-07-13T05:44:46:00-05:00Z
description: Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files, yadda, yadda, yadda...
keywords:
- IP
- copyright
- license
- mit
permissions:
- commercial-use
- modifications
- distribution
- private-use
conditions:
- include-copyright
limitations:
- liability
- warranty
lastmod: 2024-01-0T00:39:00.0000+05:00Z
license: MIT
slug: mit-license
title: MIT License
type: license
---

# MIT License

## Copyright © 2022-2024 [David G. Moore, Jr.](mailto:[email protected] "Send Dr. Moore") ([@dgmjr](https://github.com/dgmjr "Contact Dr. Moore on GitHub")), All Rights Reserved

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

45 changes: 45 additions & 0 deletions src/Blazor.Components/UnknownYesNoSelectList.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
namespace Dgmjr.Blazor.Components;
using Microsoft.AspNetCore.Components;

public class UnknownYesNoSelectList : Blazorise.Components.DropdownList<bool?, bool?>
{
[Parameter]
public string UnknownText { get; set; } = "Unknown";
[Parameter]
public string YesText { get; set; } = "Yes";
[Parameter]
public string NoText { get; set; } = "No";

public UnknownYesNoSelectList()
{
Data = new[] { (bool?)null, true, false };
TextField = item =>
item switch
{
null => UnknownText,
true => YesText,
false => NoText
};
ValueField = item => item;
}

public virtual string SelectedValueString
{
get => SelectedValue switch
{
null => UnknownText,
true => YesText,
false => NoText
};
set
{
SelectedValue = value switch
{
string s when s == UnknownText => null,
string s when s == YesText => true,
string s when s == NoText => false,
_ => SelectedValue
};
}
}
}
1 change: 1 addition & 0 deletions src/Blazor.Components/_Imports.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@using Microsoft.AspNetCore.Components.Web
Binary file added src/Blazor.Components/icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/Blazor.Components/wwwroot/background.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 6 additions & 0 deletions src/Blazor.Components/wwwroot/exampleJsInterop.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
// This is a JavaScript module that is loaded on demand. It can export any number of
// functions, and may import other JavaScript modules if required.

export function showPrompt(message) {
return prompt(message, 'Type anything here');
}
8 changes: 8 additions & 0 deletions src/Blazor.Security/Constants/BlazorSecurityConstants.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,11 @@ public static class BlazorSecurityConstants
{
public const string BlazorSecurity = nameof(BlazorSecurity);
}

public static class Uris
{
public const string AccountController = "/account";
public const string Login = $"{AccountController}/login";
public const string Logout = $"{AccountController}/logout";
public const string CurrentUser = $"{AccountController}/current-user";
}
4 changes: 3 additions & 1 deletion src/Blazor.Security/Controllers/AccountController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace Dgmjr.Blazor.Security.Controllers;
[ApiController]
public partial class AccountController(ILogger<AccountController> logger) : ApiControllerBase(logger)
{
[HttpGet]
public IActionResult Login(string redirectUri)
{
var redirectUrl = redirectUri ?? Url.Content("~/");
Expand All @@ -25,6 +26,7 @@ public IActionResult Login(string redirectUri)
);
}

[HttpGet]
public IActionResult Logout()
{
var redirectUrl = Url.Content("~/");
Expand All @@ -36,7 +38,7 @@ public IActionResult Logout()
);
}

[HttpPost]
[HttpPost("current-user")]
public ApplicationAuthenticationState CurrentUser()
{
return new ApplicationAuthenticationState
Expand Down
11 changes: 5 additions & 6 deletions src/Blazor.Security/Models/ApplicationAuthenticationState.cs
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
namespace Dgmjr.Blazor.Security.Models;
using System;
using System.Collections.Generic;

namespace Dgmjr.Blazor.Security.Models;

public partial class ApplicationAuthenticationState
public readonly record struct ApplicationAuthenticationState
{
public bool IsAuthenticated { get; set; }
public string Name { get; set; }
public IEnumerable<ApplicationClaim> Claims { get; set; }
public bool IsAuthenticated { get; init; }
public string Name { get; init; }
public IEnumerable<ApplicationClaim> Claims { get; init; }
}
3 changes: 0 additions & 3 deletions src/Blazor.Security/Models/ApplicationUser.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
using System;
using System.Collections.Generic;

namespace Dgmjr.Blazor.Security.Models;

public partial class ApplicationUser
Expand Down
5 changes: 2 additions & 3 deletions src/Blazor.Security/SecurityServiceCollectionExtensions.cs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
using Dgmjr.Blazor.Security.Services;

namespace Microsoft.Extensions.DependencyInjection;
using Dgmjr.Blazor.Security.Services;

public static class SecurityServiceCollectionExtensions
{
public static IServiceCollection AddSecurityService(this IServiceCollection services)
public static IServiceCollection AddBlazorSecurityService(this IServiceCollection services)
{
services.AddScoped<ISecurityService, SecurityService>();
return services;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
namespace Dgmjr.Blazor.Security.Services;
using System;
using System.Diagnostics;
using System.Linq;
Expand All @@ -7,12 +8,9 @@
using Microsoft.AspNetCore.Components.Authorization;

using Dgmjr.Blazor.Security.Models;

namespace Dgmjr.Blazor.Security.Services;

public class ApplicationAuthenticationStateProvider(ISecurityService securityService) : AuthenticationStateProvider
{
private ApplicationAuthenticationState _authenticationState;
private ApplicationAuthenticationState? _authenticationState;

public override async Task<AuthenticationState> GetAuthenticationStateAsync()
{
Expand Down
50 changes: 14 additions & 36 deletions src/Blazor.Security/Services/SecurityService.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
namespace Dgmjr.Blazor.Security.Services;
using System;
using System.Web;
using System.Linq;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Threading.Tasks;
using System.Security.Claims;
using System.Text;
using System.Text.Json;
using System.Security.Claims;
using System.Threading.Tasks;
using System.Web;

using Microsoft.AspNetCore.Components;
using Microsoft.AspNetCore.Components.Authorization;

using Radzen;

using Dgmjr.Blazor.Security.Models;

namespace Dgmjr.Blazor.Security.Services;
using Radzen;

public partial class SecurityService(NavigationManager navigationManager, IHttpClientFactory factory) : ISecurityService
public class SecurityService(NavigationManager navigationManager, IHttpClientFactory factory) : ISecurityService
{
private readonly HttpClient _httpClient = factory.CreateClient(BlazorSecurityConstants.BlazorSecurity);

Expand All @@ -26,37 +26,19 @@ public partial class SecurityService(NavigationManager navigationManager, IHttpC

public async Task<ApplicationAuthenticationState> GetAuthenticationStateAsync()
{
var uri = new Uri($"{navigationManager.BaseUri}Account/CurrentUser");
var uri = new Uri($"{navigationManager.BaseUri}{Uris.CurrentUser}");

var response = await _httpClient.SendAsync(new HttpRequestMessage(HttpMethod.Post, uri));

return await response.ReadAsync<ApplicationAuthenticationState>();
}

public bool IsInRole(params string[] roles)
{
if (roles.Contains("Everybody"))
{
return true;
}

if (!IsAuthenticated())
{
return false;
}

if (roles.Contains("Authenticated"))
{
return true;
}

return roles.Any(role => Principal.IsInRole(role));
}
=> roles.Contains("Everybody")
|| (IsAuthenticated() && (roles.Contains("Authenticated") || Exists(roles, role => Principal.IsInRole(role))));

public bool IsAuthenticated()
{
return Principal?.Identity.IsAuthenticated == true;
}
=> Principal?.Identity.IsAuthenticated == true;

public bool Initialize(AuthenticationState result)
{
Expand All @@ -73,12 +55,8 @@ public bool Initialize(AuthenticationState result)
}

public void Logout()
{
navigationManager.NavigateTo("Account/Logout", true);
}
=> navigationManager.NavigateTo(Uris.Logout, true);

public void Login()
{
navigationManager.NavigateTo("Login", true);
}
=> navigationManager.NavigateTo(Uris.Login, true);
}

0 comments on commit f8f9739

Please sign in to comment.