diff --git a/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs b/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs index 2491651..0b497b7 100644 --- a/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs +++ b/DotBased.ASP.Auth/BasedAuthenticationStateProvider.cs @@ -1,20 +1,24 @@ +using System.Security.Claims; using DotBased.Logging; using Microsoft.AspNetCore.Components.Authorization; namespace DotBased.ASP.Auth; +// RevalidatingServerAuthenticationStateProvider public class BasedAuthenticationStateProvider : AuthenticationStateProvider { - public BasedAuthenticationStateProvider() + public BasedAuthenticationStateProvider(BasedAuthConfiguration configuration) { + _config = configuration; _logger = LogService.RegisterLogger(typeof(BasedAuthenticationStateProvider)); } + private BasedAuthConfiguration _config; private ILogger _logger; + private AuthenticationState _anonState = new AuthenticationState(new ClaimsPrincipal(new ClaimsIdentity(new List() {new Claim(ClaimTypes.Role, "test")}))); public override Task GetAuthenticationStateAsync() { - - throw new NotImplementedException(); + return Task.FromResult(_anonState); } } \ No newline at end of file diff --git a/DotBased.ASP.Auth/DotBasedASPAuth.cs b/DotBased.ASP.Auth/DotBasedASPAuth.cs deleted file mode 100644 index 49fd0e2..0000000 --- a/DotBased.ASP.Auth/DotBasedASPAuth.cs +++ /dev/null @@ -1,13 +0,0 @@ -using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Components.Authorization; -using Microsoft.Extensions.DependencyInjection; - -namespace DotBased.ASP.Auth; - -public static class DotBasedASPAuth -{ - public static void UseBasedAuth(this WebApplicationBuilder builder, BasedAuthConfiguration configuration) - { - builder.Services.AddScoped(); - } -} \ No newline at end of file diff --git a/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs b/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs new file mode 100644 index 0000000..1fceae2 --- /dev/null +++ b/DotBased.ASP.Auth/DotBasedAuthDependencyInjection.cs @@ -0,0 +1,28 @@ +using DotBased.ASP.Auth.Scheme; +using Microsoft.AspNetCore.Components.Authorization; +using Microsoft.Extensions.DependencyInjection; + +namespace DotBased.ASP.Auth; + +public static class DotBasedAuthDependencyInjection +{ + /// + /// Use the DotBased authentication implementation + /// + /// Use the app.UseAuthentication() and app.UseAuthorization()! + /// Service colllection + /// DotBased auth configuration + public static void UseBasedAuth(this IServiceCollection services, Action? configurationAction = null) + { + var config = new BasedAuthConfiguration(); + configurationAction?.Invoke(config); + services.AddSingleton(config); + + services.AddScoped(); + services.AddAuthentication(options => + { + options.DefaultScheme = BasedAuthenticationHandler.AuthenticationScheme; + }).AddScheme(BasedAuthenticationHandler.AuthenticationScheme, null); + services.AddAuthorization(); + } +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs new file mode 100644 index 0000000..722644e --- /dev/null +++ b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandler.cs @@ -0,0 +1,31 @@ +using System.Security.Claims; +using System.Text.Encodings.Web; +using Microsoft.AspNetCore.Authentication; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Options; + +namespace DotBased.ASP.Auth.Scheme; + +public class BasedAuthenticationHandler : AuthenticationHandler +{ + public const string AuthenticationScheme = "DotBasedAuthentication"; + +#pragma warning disable CS0618 // Type or member is obsolete + public BasedAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder, ISystemClock clock) : base(options, logger, encoder, clock) +#pragma warning restore CS0618 // Type or member is obsolete + { + + } + + public BasedAuthenticationHandler(IOptionsMonitor options, ILoggerFactory logger, UrlEncoder encoder) : base(options, logger, encoder) + { + + } + + protected override Task HandleAuthenticateAsync() + { + var principal = new ClaimsPrincipal(new ClaimsIdentity(new List() { new Claim(ClaimTypes.Role, "Admin"), new Claim(ClaimTypes.Name, "Anon") }, AuthenticationScheme)); + var ticket = new AuthenticationTicket(principal, AuthenticationScheme); + return Task.FromResult(AuthenticateResult.Success(ticket)); + } +} \ No newline at end of file diff --git a/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandlerOptions.cs b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandlerOptions.cs new file mode 100644 index 0000000..3f8228c --- /dev/null +++ b/DotBased.ASP.Auth/Scheme/BasedAuthenticationHandlerOptions.cs @@ -0,0 +1,8 @@ +using Microsoft.AspNetCore.Authentication; + +namespace DotBased.ASP.Auth.Scheme; + +public class BasedAuthenticationHandlerOptions : AuthenticationSchemeOptions +{ + +} \ No newline at end of file