예제 #1
0
        private void CreateEncryptedRoute(HttpConfiguration configuration)
        {
            var section = (NameValueCollection)ConfigurationManager.GetSection("Api2CryptoGraphySettings");
            if (section == null)
            {
                throw new ApplicationException("Config section 'RestfulWebService' has not been set.");
            }

            // arrange.
            var settings = new CryptoGraphySettings(section);
            var cryptoHandler = new ServerCryptoHandler(settings.SecretKey, settings.InitialVector, settings.HashKey);
            var injection = new DelegatingHandler[] { new ServerMessageDumper(), cryptoHandler }; // dump decrypted request & plain response.
            var handler = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(configuration), injection);

            // register encrypted HTTP route.
            configuration.Routes.MapHttpRoute("Encrypted Route", "api2/{controller}/{action}", null, null, handler);

            // register timestamp as a route.
            var timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15)) as ITimestampProvider<string>;
            var timestampHandler = new HttpTimestampHandler<string>(timestampProvider);
            configuration.Routes.MapHttpRoute("Timestamp Route", "api3/!timestamp!/get", null, null, timestampHandler);

            // register global timestamp service, it should align with encrypted HTTP route or will not work.
            configuration.MessageHandlers.Add(new HttpTimestampHandler<string>(configuration, "api2/!timestamp!/get", timestampProvider));
        }
        private void RegisterEncryptedRoute(HttpConfiguration configuration, ITimestampProvider<string> timestampProvider)
        {
            // arrange.
            var cryptoHandler = new ServerCryptoHandler("secretKeyPassword", "initialVectorPassword", "hashKeyString", timestampProvider);
            var injection = new DelegatingHandler[] { new ServerMessageDumper(), cryptoHandler, new ServerMessageDumper() };
            var handler = HttpClientFactory.CreatePipeline(new HttpControllerDispatcher(configuration), injection);

            // register encrypted HTTP route.
            configuration.Routes.MapHttpRoute("Encrypted API", "api2/{controller}/{action}", null, null, handler);
        }
        internal EncryptedHttpServerWrapper(string baseAddress)
        {
            var configuration = new HttpSelfHostConfiguration(baseAddress);

            // arrange.
            var timestampProvider = new DefaultTimestampProvider(TimeSpan.FromMinutes(15));
            var cryptoHandler = new ServerCryptoHandler("secretKeyPassword", "initialVectorPassword", "hashKeyString", timestampProvider);

            // register handlers.
            configuration.MessageHandlers.Add(new ServerMessageDumper());
            configuration.MessageHandlers.Add(new HttpTimestampHandler<long>(configuration, "api2/!timestamp!/get", timestampProvider));
            configuration.Routes.MapHttpRoute("Fake Timestamp Route", "api2/!timestamp!/{action}");

            // startup local HTTP server.
            this.inner = new HttpSelfHostServer(configuration);
            this.inner.OpenAsync().Wait();

            this.configuration = configuration;
        }