Esempio n. 1
0
        private IAsyncSession <DuplexMessage> GetRegisteredAndAuthedSession(ServerBootstrap bootstrap, bool clearSessionCache = false)
        {
            using (var scope = ObjectHost.Host.BeginLifetimeScope())
            {
                var factory = scope.Resolve <IAsyncSessionFactory <DuplexMessage> >();
                var session = factory.OpenSession(string.Format("{0};{1};",
                                                                bootstrap.EndPoint.ToString(), "keepalive=false"));
                var command = session.CreateCommand(CommandCode.Register);
                command.SecurityEnabled = true;
                command.Parameter       = new RegisterInfo {
                    ClientMacAddr = NetworkInfoHelper.GetMacAddr(), ClientPubKey = new TestRsaKeyProvider().GetPublicKey(null)
                };
                var result = command.Run(5000);
                command           = session.CreateCommand(CommandCode.Authentication);
                command.Parameter = new AuthInfo
                {
                    Identifier = scope.Resolve <IIdentifierProvider>().GetIdentifier(),
                    Mac        = NetworkInfoHelper.GetMacAddr()
                };
                command.SecurityEnabled = true;
                result = command.Run(5000);

                if (clearSessionCache)
                {
                    var map = (factory as AsyncSessionFactoryImpl).AsTransparentObject().sessionMap as ConcurrentDictionary <string, IAsyncSession <DuplexMessage> >;
                    map.Clear();
                }

                return(session);
            }
        }
Esempio n. 2
0
        public void RegisterAndAuthTest()
        {
            var bootstrap = new ServerBootstrap(EndPointDispatcher.GetRandomPort());

            bootstrap.AddFilterTypes(typeof(AuthenticationValidationFilter));
            bootstrap.StartUp(null,
                              new CommandServerModule(),
                              new RepositoryModule(),
                              new ClientModule(),
                              new TestModule());

            //dont register client module for they are in the same context.
            //new ClientInitializer().Init(new TestModule());
            using (var scope = ObjectHost.Host.BeginLifetimeScope())
            {
                var factory = scope.Resolve <IAsyncSessionFactory <DuplexMessage> >();
                var session = factory.OpenSession(string.Format("{0};{1};",
                                                                bootstrap.EndPoint.ToString(), "keepalive=false"));
                var command = session.CreateCommand(CommandCode.Register);
                command.SecurityEnabled = true;
                command.Parameter       = new RegisterInfo {
                    ClientMacAddr = NetworkInfoHelper.GetMacAddr(), ClientPubKey = new TestRsaKeyProvider().GetPublicKey(null)
                };
                var result = command.Run(5000);
                Assert.AreEqual(ErrorCode.NoError, result.Header.ErrorCode);
                Assert.AreEqual(command.ID, result.Header.MessageID);
                Assert.AreEqual(MessageType.Callback, result.Header.MessageType);

                command           = session.CreateCommand(CommandCode.Authentication);
                command.Parameter = new AuthInfo
                {
                    Identifier = scope.Resolve <IIdentifierProvider>().GetIdentifier(),
                    Mac        = NetworkInfoHelper.GetMacAddr()
                };
                command.SecurityEnabled = true;
                result = command.Run(5000);
                Assert.AreEqual(ErrorCode.NoError, result.Header.ErrorCode);
                Assert.AreEqual(command.ID, result.Header.MessageID);
                Assert.AreEqual(MessageType.Callback, result.Header.MessageType);

                command = session.CreateCommand(CommandCode.Test);
                result  = command.Run(5000);
                Assert.AreEqual(CommandCode.Test, result.Header.CommandCode);
                Assert.AreEqual(ErrorCode.BadRequest, result.Header.ErrorCode); // test command not registered, it's ture that it returns bad request
            }
        }