diff --git a/ManagedCode.Orleans.Identity.Client/Extensions/AuthenticationHandlerExtensions.cs b/ManagedCode.Orleans.Identity.Client/Extensions/AuthenticationHandlerExtensions.cs
deleted file mode 100644
index 5ac5933..0000000
--- a/ManagedCode.Orleans.Identity.Client/Extensions/AuthenticationHandlerExtensions.cs
+++ /dev/null
@@ -1,56 +0,0 @@
-using System;
-using ManagedCode.Orleans.Identity.Client.Middlewares;
-using ManagedCode.Orleans.Identity.Core.Constants;
-using ManagedCode.Orleans.Identity.Core.Options;
-using Microsoft.AspNetCore.Authentication;
-using Microsoft.AspNetCore.Authorization;
-using Microsoft.Extensions.DependencyInjection;
-
-namespace ManagedCode.Orleans.Identity.Client.Extensions;
-
-public static class AuthenticationHandlerExtensions
-{
- ///
- /// Use Orleans.Identity authentication as default authentication scheme
- ///
- ///
- /// Options for working with session
- public static void AddOrleansIdentity(this IServiceCollection services, Action sessionOption)
- {
- var option = new SessionOption();
- sessionOption?.Invoke(option);
- AddOrleansIdentity(services, option);
- }
-
- ///
- /// Use Orleans.Identity authentication as default authentication scheme
- ///
- ///
- /// Options for working with session
- ///
- public static void AddOrleansIdentity(this IServiceCollection services, SessionOption? sessionOption = null, Action? authenticationBuilder = null)
-
- {
- sessionOption ??= new SessionOption();
-
- // Add custom authentication and authorization
- services.AddScoped();
- services.AddScoped();
-
- services.AddAuthentication(options =>
- {
- options.DefaultScheme = OrleansIdentityConstants.AUTHENTICATION_TYPE;
- })
- .AddScheme(OrleansIdentityConstants.AUTHENTICATION_TYPE, op =>
- {
-
- });
- services.AddAuthorization(options =>
- {
- var defaultAuthorizationPolicyBuilder = new AuthorizationPolicyBuilder(OrleansIdentityConstants.AUTHENTICATION_TYPE);
- defaultAuthorizationPolicyBuilder = defaultAuthorizationPolicyBuilder.RequireAuthenticatedUser();
- options.DefaultPolicy = defaultAuthorizationPolicyBuilder.Build();
- });
-
- }
-}
\ No newline at end of file
diff --git a/ManagedCode.Orleans.Identity.Client/Extensions/OrleansContextMiddlewareExtensions.cs b/ManagedCode.Orleans.Identity.Client/Extensions/OrleansContextMiddlewareExtensions.cs
deleted file mode 100644
index e3fa98c..0000000
--- a/ManagedCode.Orleans.Identity.Client/Extensions/OrleansContextMiddlewareExtensions.cs
+++ /dev/null
@@ -1,30 +0,0 @@
-using ManagedCode.Orleans.Identity.Client.Middlewares;
-using Microsoft.AspNetCore.Builder;
-
-namespace ManagedCode.Orleans.Identity.Client.Extensions;
-
-public static class OrleansContextMiddlewareExtensions
-{
- ///
- /// Use middleware to set claims and session id for request to cluster
- ///
- ///
- ///
- public static IApplicationBuilder UseOrleansIdentity(this IApplicationBuilder builder)
- {
- return builder.UseMiddleware()
- .UseMiddleware();
- }
-
- ///
- /// Use middleware to set claims and session id for request to cluster, this method includes UseAuthentication and UseAuthorization middlewares
- ///
- ///
- ///
- public static IApplicationBuilder UseAuthenticationAndOrleansIdentity(this IApplicationBuilder builder)
- {
- builder.UseAuthentication();
- builder.UseAuthorization();
- return builder.UseOrleansIdentity();
- }
-}
\ No newline at end of file
diff --git a/ManagedCode.Orleans.Identity.Client/Extensions/OrleansIdentityExtensions.cs b/ManagedCode.Orleans.Identity.Client/Extensions/OrleansIdentityExtensions.cs
new file mode 100644
index 0000000..7268b96
--- /dev/null
+++ b/ManagedCode.Orleans.Identity.Client/Extensions/OrleansIdentityExtensions.cs
@@ -0,0 +1,28 @@
+using ManagedCode.Orleans.Identity.Client.Middlewares;
+using Microsoft.AspNetCore.Builder;
+using Microsoft.AspNetCore.SignalR;
+using Microsoft.Extensions.DependencyInjection;
+
+namespace ManagedCode.Orleans.Identity.Client.Extensions;
+
+public static class OrleansIdentityExtensions
+{
+ public static IServiceCollection AddOrleansIdentity(this IServiceCollection services)
+ {
+ services.AddScoped();
+
+ services.AddSignalR(options =>
+ {
+ options.AddFilter();
+ });
+
+ return services;
+ }
+
+ public static IApplicationBuilder UseOrleansIdentity(this IApplicationBuilder app)
+ {
+ app.UseMiddleware();
+
+ return app;
+ }
+}
\ No newline at end of file
diff --git a/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansContextMiddleware.cs b/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansContextMiddleware.cs
index 4c37d70..efa4539 100644
--- a/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansContextMiddleware.cs
+++ b/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansContextMiddleware.cs
@@ -1,14 +1,20 @@
+using System.Security.Claims;
using System.Threading.Tasks;
-using ManagedCode.Orleans.Identity.Core.Extensions;
using Microsoft.AspNetCore.Http;
+using Orleans.Runtime;
+using ManagedCode.Orleans.Identity.Core.Constants;
namespace ManagedCode.Orleans.Identity.Client.Middlewares;
-public class OrleansContextMiddleware(RequestDelegate next)
+public class OrleansContextMiddleware : IMiddleware
{
- public async Task InvokeAsync(HttpContext context)
+ public async Task InvokeAsync(HttpContext context, RequestDelegate next)
{
- context.User.SetOrleansContext();
+ if (context.User.Identity?.IsAuthenticated == true)
+ {
+ RequestContext.Set(OrleansIdentityConstants.USER_CLAIMS, context.User);
+ }
+
await next(context);
}
}
\ No newline at end of file
diff --git a/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansIdentityAuthenticationHandler.cs b/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansIdentityAuthenticationHandler.cs
deleted file mode 100644
index d28656c..0000000
--- a/ManagedCode.Orleans.Identity.Client/Middlewares/OrleansIdentityAuthenticationHandler.cs
+++ /dev/null
@@ -1,73 +0,0 @@
-using System;
-using System.Security.Claims;
-using System.Text.Encodings.Web;
-using System.Threading.Tasks;
-using ManagedCode.Orleans.Identity.Core.Constants;
-using ManagedCode.Orleans.Identity.Core.Extensions;
-using ManagedCode.Orleans.Identity.Core.Interfaces;
-using Microsoft.AspNetCore.Authentication;
-using Microsoft.Extensions.Logging;
-using Microsoft.Extensions.Options;
-using Orleans;
-
-namespace ManagedCode.Orleans.Identity.Client.Middlewares;
-
-public class OrleansIdentityAuthenticationHandler(
- IOptionsMonitor options,
- ILoggerFactory logger,
- UrlEncoder encoder,
- ISystemClock clock,
- IClusterClient client) : AuthenticationHandler(options, logger, encoder, clock)
-{
- protected override async Task HandleAuthenticateAsync()
- {
- string sessionId;
- if (!Request.Headers.TryGetValue(OrleansIdentityConstants.AUTH_TOKEN, out var values))
- {
- if (Request.Headers.TryGetValue("Authorization", out var jwt))
- {
- sessionId = jwt.ToString().Replace("Bearer", "").Trim();
- }
- else if (Request.Query.TryGetValue(OrleansIdentityConstants.AUTH_TOKEN, out var queryValues))
- {
- sessionId = queryValues.ToString().Trim();
- }
- else
- {
- return AuthenticateResult.NoResult();
- }
- }
- else
- {
- sessionId = values.ToString().Trim();
- }
-
- if (string.IsNullOrEmpty(sessionId))
- {
- return AuthenticateResult.NoResult();
- }
-
- try
- {
- var sessionGrain = client.GetGrain(sessionId);
- var result = await sessionGrain.ValidateAndGetClaimsAsync();
-
- if (result.IsSuccess)
- {
- ClaimsIdentity claimsIdentity = new(OrleansIdentityConstants.AUTHENTICATION_TYPE);
-
- foreach (var claim in result.Value!)
- claimsIdentity.ParseClaims(claim.Key, claim.Value);
-
- var ticket = new AuthenticationTicket(new ClaimsPrincipal(claimsIdentity), Scheme.Name);
- return AuthenticateResult.Success(ticket);
- }
- }
- catch (Exception e)
- {
- Logger.LogError(e, "HandleAuthenticateAsync Validation");
- }
-
- return AuthenticateResult.Fail($"Unauthorized request. SessionId: {sessionId};");
- }
-}
\ No newline at end of file
diff --git a/ManagedCode.Orleans.Identity.Client/Middlewares/SignalRAuthorizationFilter.cs b/ManagedCode.Orleans.Identity.Client/Middlewares/SignalRAuthorizationFilter.cs
new file mode 100644
index 0000000..f826c67
--- /dev/null
+++ b/ManagedCode.Orleans.Identity.Client/Middlewares/SignalRAuthorizationFilter.cs
@@ -0,0 +1,37 @@
+using System;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.SignalR;
+using Orleans.Runtime;
+using ManagedCode.Orleans.Identity.Core.Constants;
+
+namespace ManagedCode.Orleans.Identity.Client.Middlewares;
+
+public class SignalRAuthorizationFilter : IHubFilter
+{
+ public async ValueTask