Sample code for 30+ languages & platforms
C# Requires Chilkat v11.6.0+

Azure Blob: Test an Unsigned CORS Preflight

See more Azure Cloud Storage Examples

Send an unauthenticated OPTIONS request with a complete origin (scheme and host), intended method, and intended headers. A matching configured CORS rule produces HTTP 200; 403 commonly indicates no matching rule. No account key or SAS is required for the preflight, and success does not grant access to the real operation.

CORS controls browser cross-origin behavior. Native desktop and server applications do not need a preflight before their own REST requests. This example is a diagnostic for an application that will make browser requests; it does not require a webhook or a web server to run.

Uses direct Chilkat REST calls with HTTPS certificate verification. Shared Key examples load a private qa_data/azure/storage.json file containing string fields account and access_key (the base64 account key). SAS consumers need only account there. Create local input/output directories before running. Examples target Azure public-cloud endpoints; adapt hostnames for other clouds or custom endpoints.

REST examples pin x-ms-version: 2026-04-06, and SAS generation uses the same service version. Complete-URL downloads use the supplied SAS version. This is a fully deployed version documented by Microsoft at review time. Older supported versions are not automatically obsolete. Transport completion and HTTP success are checked separately; writes are not automatically replayed after a lost response. Keep diagnostic dumps, keys and SAS URLs private.

Shared Key remains supported where allowed by account policy. Microsoft recommends Microsoft Entra ID, preferably managed identities where available, and user delegation SAS for delegated Blob access. Account-key SAS and Shared Key requests can be rejected when Shared Key access is disabled; these samples do not bypass that policy. See Azure data authorization.

Microsoft preflight blob request reference.

Chilkat C# Downloads

C#
bool success = false;

//  StringBuilder compares text by value in every generated language.
Chilkat.StringBuilder textCompare = new Chilkat.StringBuilder();
//  Assumes Chilkat is unlocked. See Global Unlock Sample.
success = false;
Chilkat.JsonObject config = new Chilkat.JsonObject();
config.Strict = true;
success = config.LoadFile("qa_data/azure/storage.json");
if (success != true) {
    Debug.WriteLine("Could not load private Azure configuration.");
    return;
}

textCompare.Clear();
textCompare.SetString(config.StringOf("account"));
bool sameText1 = textCompare.ContentsEqual("",true);
if ((config.JsonTypeOf("account") != 1) || (sameText1 == true)) {
    Debug.WriteLine("Supply the storage account name.");
    return;
}

Chilkat.StringBuilder endpoint = new Chilkat.StringBuilder();
success = endpoint.Append(config.StringOf("account"));
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = endpoint.Append(".blob.core.windows.net");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

//  resourcePath is already URL-encoded. Encode each path segment once when adapting it.
//  Keep slash separators; names and paths are case-sensitive.
string resourcePath = "/examplecontainer/example.bin";
Chilkat.Rest rest = new Chilkat.Rest();
rest.ConnectTimeoutMs = 20000;
rest.IdleTimeoutMs = 60000;
Chilkat.Socket connection = new Chilkat.Socket();
connection.RequireSslCertVerify = true;
connection.MaxReadIdleMs = 60000;
connection.MaxSendIdleMs = 60000;
success = connection.Connect(endpoint.GetAsString(),443,true,20000);
if (success != true) {
    Debug.WriteLine("TLS connection failed; inspect diagnostics privately.");
    return;
}

//  Do not replay a write automatically after losing its response.
success = rest.UseConnection(connection,false);
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

string responseText;
Chilkat.Xml result = new Chilkat.Xml();
result.Strict = true;
success = rest.AddHeader("x-ms-version","2026-04-06");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Origin","https://app.example.com");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Access-Control-Request-Method","PUT");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

success = rest.AddHeader("Access-Control-Request-Headers","content-type,x-ms-blob-type");
if (success != true) {
    Debug.WriteLine("Could not prepare the request.");
    return;
}

responseText = rest.FullRequestNoBody("OPTIONS",resourcePath);
if (rest.LastMethodSuccess != true) {
    Debug.WriteLine("Request did not complete; outcome may be uncertain.");
    return;
}

if (rest.ResponseStatusCode != 200) {
    Debug.WriteLine("HTTP status: " + Convert.ToString(rest.ResponseStatusCode));
    Debug.WriteLine("Azure error code: " + rest.ResponseHdrByName("x-ms-error-code"));
    Debug.WriteLine("Request ID: " + rest.ResponseHdrByName("x-ms-request-id"));
    //  Inspect any error body privately; do not log credentials, SAS URLs or payloads.
    return;
}

Debug.WriteLine("Allowed origin: " + rest.ResponseHdrByName("Access-Control-Allow-Origin"));
Debug.WriteLine("Allowed methods: " + rest.ResponseHdrByName("Access-Control-Allow-Methods"));
Debug.WriteLine("Allowed headers: " + rest.ResponseHdrByName("Access-Control-Allow-Headers"));
Debug.WriteLine("CORS preflight accepted; this does not authorize the actual upload.");