diff --git a/CoreRemoting.Tests/RpcTests.cs b/CoreRemoting.Tests/RpcTests.cs index d20c412..cbe9c42 100644 --- a/CoreRemoting.Tests/RpcTests.cs +++ b/CoreRemoting.Tests/RpcTests.cs @@ -679,5 +679,35 @@ void Roundtrip(T payload, int maxSize) where T : class } } } + + [Fact] + public void Authentication_is_taken_into_account() + { + _serverFixture.Server.Config.AuthenticationRequired = true; + try + { + using var client = new RemotingClient(new ClientConfig() + { + ConnectionTimeout = 0, + InvocationTimeout = 0, + SendTimeout = 0, + Channel = ClientChannel, + MessageEncryption = false, + ServerPort = _serverFixture.Server.Config.NetworkPort, + }); + + client.Connect(); + + var proxy = client.CreateProxy(); + var ex = Assert.Throws(() => proxy.Hello()); + + // Session is not authenticated + Assert.Contains("authenticated", ex.Message); + } + finally + { + _serverFixture.Server.Config.AuthenticationRequired = false; + } + } } } \ No newline at end of file diff --git a/CoreRemoting.Tests/Tools/FailingService.cs b/CoreRemoting.Tests/Tools/FailingService.cs index 08e68a2..82aee79 100644 --- a/CoreRemoting.Tests/Tools/FailingService.cs +++ b/CoreRemoting.Tests/Tools/FailingService.cs @@ -4,12 +4,13 @@ namespace CoreRemoting.Tests.Tools; public class FailingService : IFailingService { - public FailingService() - { - throw new NotImplementedException(); - } + public FailingService() + { + Console.WriteLine("FailingService constructor was called!"); + throw new NotImplementedException(); + } - public void Hello() - { - } + public void Hello() + { + } } diff --git a/CoreRemoting/RemotingSession.cs b/CoreRemoting/RemotingSession.cs index a31600e..ab5ae79 100644 --- a/CoreRemoting/RemotingSession.cs +++ b/CoreRemoting/RemotingSession.cs @@ -427,6 +427,9 @@ private void ProcessRpcMessage(WireMessage request) { CurrentSession.Value = this; + if (_server.Config.AuthenticationRequired && !_isAuthenticated) + throw new NetworkException("Session is not authenticated."); + var service = _server.ServiceRegistry.GetService(callMessage.ServiceName); var serviceInterfaceType = _server.ServiceRegistry.GetServiceInterfaceType(callMessage.ServiceName); @@ -448,9 +451,6 @@ private void ProcessRpcMessage(WireMessage request) methodName: callMessage.MethodName); oneWay = method.GetCustomAttribute() != null; - - if (_server.Config.AuthenticationRequired && !_isAuthenticated) - throw new NetworkException("Session is not authenticated."); } catch (Exception ex) {