コード例 #1
0
        public void ShouldConfigureFromFile()
        {
            // Arrange
            HmacConfigurationManager configurationManager  = new HmacConfigurationManager();
            HmacConfiguration        expectedConfiguration = new HmacConfiguration
            {
                Name                   = "TestConfiguration",
                UserHeaderName         = "X-Test-User",
                AuthorizationScheme    = "TEST",
                SignatureDataSeparator = "_",
                SignatureEncoding      = "UTF-32",
                HmacAlgorithm          = "HMACSHA256",
                MaxRequestAge          = TimeSpan.FromMinutes(2),
                SignRequestUri         = false,
                ValidateContentMd5     = true,
                Headers                = new List <string> {
                    "X-Test-Header-1", "X-Test-Header-2"
                }
            };

            // Act
            configurationManager.ConfigureFromFile("Hmac.config");
            IHmacConfiguration configuration = configurationManager.Get(expectedConfiguration.Name);

            // Assert
            AssertConfiguration(expectedConfiguration, configuration);
        }
コード例 #2
0
ファイル: Program.cs プロジェクト: denedios/hmac
        static void Main()
        {
            // Retrieve the server URL
            string serverBaseUrl = ConfigurationManager.AppSettings["ServerBaseUrl"];

            // Setup the signer
            IHmacConfigurationManager configurationManager = new HmacConfigurationManager();

            configurationManager.ConfigureFromFile("Hmac.config");
            IHmacConfiguration   configuration = configurationManager.Get("Example");
            IHmacKeyRepository   keyRepository = new SingleHmacKeyRepository("FollowTheWhiteRabbit");
            IRestSharpHmacSigner signer        = new RestSharpHmacSigner(configuration, keyRepository);

            // Setup the RestSharp client
            IRestClient client = new RestClient(serverBaseUrl);

            client.AddHandler("application/json", new JsonDeserializer());
            client.Authenticator = new HmacAuthenticator(configuration, signer);
            client.AddDefaultHeader("X-Custom-Header", "Knock knock...");

            // Execute the GET request
            Console.WriteLine("Neo searches for a spoon.");
            IRestRequest getRequest = new RestRequest("spoon", Method.GET);

            getRequest.AddHeader(configuration.UserHeaderName, "Neo");
            IRestResponse <ExampleModel> getResponse = client.Execute <ExampleModel>(getRequest);

            Console.WriteLine("  There is a '{0}'!", getResponse.Data.Value);

            // Execute the POST request
            Console.WriteLine("Neo posts his sunglasses.");
            IRestRequest postRequest = new RestRequest(Method.POST);

            postRequest.RequestFormat = DataFormat.Json;
            postRequest.AddHeader(configuration.UserHeaderName, "Neo");
            postRequest.AddBody(new ExampleModel {
                Value = "sunglasses"
            });
            IRestResponse postResponse = client.Execute(postRequest);

            Console.WriteLine("  {0}", postResponse.Content);

            // Execute GET request with an incorrect username
            Console.WriteLine("Morpheus searches for The One.");
            IRestRequest incorrectGetRequest = new RestRequest("The One", Method.GET);

            incorrectGetRequest.AddHeader(configuration.UserHeaderName, "Morpheus");
            IRestResponse incorrectGetResponse = client.Execute(incorrectGetRequest);

            Console.WriteLine("  {0}", incorrectGetResponse.Content);

#if DEBUG
            if (Debugger.IsAttached)
            {
                Console.WriteLine("Press any key to continue . . .");
                Console.ReadKey(true);
            }
#endif
        }