The Azure API Management is a scalable and secure API gateway/proxy/cache where you can expose your API’s externally and still have secure access.
In Azure API Management you create a “Product” which is a collection of API’s that is protected using the same product key.
The 2 products above contains a collection of API’s, and each product have it’s own key.
As a developer you can find the API Keys using the Azure API Management Service Developer Portal:
When clicking around you will end up finding the “Try it” button where you are allowed to test your API endpoints:
And here you can get the subscription key by clicking the icon shaped as an eye:
When calling any API, you simply need to add the subscription key to the request header in the field:
- Ocp-Apim-Subscription-Key
This is an example on how to GET or POST to an API that is secured by the Azure API Management. There is many ways to do it, and this is not the most elegant. But this code will work in production with most versions of .NET:
using System; using System.IO; using System.Net; using System.Text; namespace MyNamespace { public class AzureApimService { private readonly string _domain; private readonly string _ocp_Apim_Subscription_Key; public AzureApimService(string domain, string subscriptionKey) { _domain = domain; _ocp_Apim_Subscription_Key = subscriptionKey; } public byte[] Get(string relativePath, out string contentType) { Uri fullyQualifiedUrl = GetFullyQualifiedURL(_domain, relativePath); try { byte[] bytes; HttpWebRequest webRequest = (HttpWebRequest) WebRequest.Create(fullyQualifiedUrl); webRequest.Headers.Add("Ocp-Apim-Trace", "true"); webRequest.Headers.Add("Ocp-Apim-Subscription-Key", _ocp_Apim_Subscription_Key); webRequest.Headers.Add("UserAgent", "YourUserAgent"); webRequest.KeepAlive = false; webRequest.ProtocolVersion = HttpVersion.Version10; webRequest.ServicePoint.ConnectionLimit = 24; webRequest.Method = WebRequestMethods.Http.Get; using (WebResponse webResponse = webRequest.GetResponse()) { contentType = webResponse.ContentType; using (Stream stream = webResponse.GetResponseStream()) { using (MemoryStream memoryStream = new MemoryStream()) { byte[] buffer = new byte[0x1000]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { memoryStream.Write(buffer, 0, bytesRead); } bytes = memoryStream.ToArray(); } } } // For test/debug purposes (to see what is actually returned by the service) Console.WriteLine("Response data (relativePath: \"{0}\"):\n{1}\n\n", relativePath, Encoding.Default.GetString(bytes)); return bytes; } catch (Exception ex) { throw new Exception("Failed to retrieve data from '" + fullyQualifiedUrl + "': " + ex.Message, ex); } } public byte[] Post(string relativePath, byte[] postData, out string contentType) { Uri fullyQualifiedUrl = GetFullyQualifiedURL(_domain, relativePath); try { byte[] bytes; HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(fullyQualifiedUrl); webRequest.Headers.Add("Ocp-Apim-Trace", "true"); webRequest.Headers.Add("Ocp-Apim-Subscription-Key", _ocp_Apim_Subscription_Key); webRequest.KeepAlive = false; webRequest.ServicePoint.ConnectionLimit = 24; webRequest.Headers.Add("UserAgent", "YourUserAgent"); webRequest.ProtocolVersion = HttpVersion.Version10; webRequest.ContentType = "application/json"; webRequest.Method = WebRequestMethods.Http.Post; webRequest.ContentLength = postData.Length; Stream dataStream = webRequest.GetRequestStream(); dataStream.Write(postData, 0, postData.Length); dataStream.Close(); using (WebResponse webResponse = webRequest.GetResponse()) { contentType = webResponse.ContentType; using (Stream stream = webResponse.GetResponseStream()) { using (MemoryStream memoryStream = new MemoryStream()) { byte[] buffer = new byte[0x1000]; int bytesRead; while ((bytesRead = stream.Read(buffer, 0, buffer.Length)) > 0) { memoryStream.Write(buffer, 0, bytesRead); } bytes = memoryStream.ToArray(); } } } // For test/debug purposes (to see what is actually returned by the service) Console.WriteLine("Response data (relativePath: \"{0}\"):\n{1}\n\n", relativePath, Encoding.Default.GetString(bytes)); return bytes; } catch (Exception ex) { throw new Exception("Failed to retrieve data from '" + fullyQualifiedUrl + "': " + ex.Message, ex); } } private static Uri GetFullyQualifiedURL(string domain, string relativePath) { if (!domain.EndsWith("/")) domain = domain + "/"; if (relativePath.StartsWith("/")) relativePath = relativePath.Remove(0, 1); return new Uri(domain + relativePath); } } }
The service is simple to use:
AzureApimService service = new AzureApimService("https://yourapim.azure-api.net", "12a6aca3c5a242f181f3dec39b264ab5"); string contentType; byte[] response = service.Get("/api/endpoint", out contentType);
MORE TO READ:
- API Management Documentation by Microsoft
- Unable to read data from the transport connection: The connection was closed by briancaos
- An existing connection was forcibly closed by the remote host by briancaos
