Amazon Cloudfront signierte URL -AusgabeC#

Ein Treffpunkt für C#-Programmierer
Anonymous
 Amazon Cloudfront signierte URL -Ausgabe

Post by Anonymous »

Ich habe eine CloudFront -Verteilung für meinen S3 -Bucket erstellt, stellte sicher, dass die ordnungsgemäße Ursprungs -Zugriffskontrolle (OAC) vorhanden war und dass die korrekte Bucket -Richtlinie den OAC -Zugriff ergab, dass der Zugriff auf den Zuschauer auf den Zugriff auf den Zuschauer auf meine Schlüsselgruppe eingestellt wurde. Sorgte sicherzustellen, dass die zu korrekten Tastengruppe verwendet wurde. Signierte URL mit einem ungültigen/unbekannten Schlüssel und einem 403 -Fehler? Außerdem weiß ich nicht, ob dies hilft, aber ich habe eine CORS -Richtlinie hinzugefügt (vielleicht brauchte ich sie nicht?), Und dann habe ich dafür gesorgt, dass mein Verteilungsverhalten nicht nur CORS -Header, sondern auch Abfragebräge enthielt. Ich habe auch Cookies eingeschlossen (was wahrscheinlich unnötig ist). < /P>

Code: Select all

using Amazon.CloudFront;
using System;
using System.IO;
using System.Security.Cryptography;
using System.Text;
using Newtonsoft.Json;

public class CloudFrontSignedUrlGenerator
{
public static string GenerateSignedUrl(string distributionDomain, string filePath, string privateKeyFilePath, string keyPairId, DateTime expirationDate)
{
string resourceUrl = $"https://{distributionDomain}/{filePath}";

// Create the policy document
var policy = new
{
Statement = new[]
{
new
{
Resource = resourceUrl,
Condition = new
{
DateLessThan = new { EpochTime = ((DateTimeOffset)expirationDate).ToUnixTimeSeconds() }
}
}
}
};

string policyJson = JsonConvert.SerializeObject(policy);

// Load the private key
string privateKey = File.ReadAllText(privateKeyFilePath);

// Sign the policy
string signature = SignPolicyWithRSA(policyJson, privateKey);

// Construct signed URL
return $"{resourceUrl}?Policy={Uri.EscapeDataString(policyJson)}&Signature={signature}&Key-Pair-Id={keyPairId}";
}

private static string SignPolicyWithRSA(string policyJson, string privateKey)
{
using (RSA rsa = RSA.Create())
{
rsa.ImportFromPem(privateKey);
byte[] data = Encoding.UTF8.GetBytes(policyJson);
byte[] signature = rsa.SignData(data, HashAlgorithmName.SHA1, RSASignaturePadding.Pkcs1);

// Make Base64 signature URL-safe
return Convert.ToBase64String(signature)
.Replace('+', '-')
.Replace('/', '_')
.Replace("=", "");
}
}

public static void Main()
{
string distributionDomain = "XXXXXXXXXXXXXX.cloudfront.net";
string filePath = "WindowsForm_1/20241205_WFASample.mp4";
string privateKeyFilePath = "C:\\Users\\USERNAME\\OneDrive\\Coding\\AmazonWebServices\\KeyPair\\PrivateKey\\pk-XXXXXXXXXXXXXXXXXXXX.pem";
string keyPairId = "XXXXXXXXXXXXXXXXXXXX";
DateTime expirationDate = DateTime.UtcNow.AddHours(1);

string signedUrl = GenerateSignedUrl(distributionDomain, filePath, privateKeyFilePath, keyPairId, expirationDate);

Console.WriteLine($"Generated Signed URL: {signedUrl}");
}
}

Quick Reply

Change Text Case: 
   
  • Similar Topics
    Replies
    Views
    Last post