Skip to content

Codepolice

  • ⤫

Signing a request to set up a custom Amazon CloudFront Distribution with C#

Posted by Judy Alvarez Posted on February 28, 2022March 1, 2022
0

I love most of Amazon’s Web Services (AWS). CloudFront is their CDN service and they have an awesome feature that lets you host your files on your server and then Amazon automatically grabs the files from your server and puts them on their CDN. This makes it possible to easily modify your files without having to download them from some S3 bucket or something to make changes.

Unfortunately, Amazon doesn’t provide a UI to set up a “Custom Distribution” so you have to do a post request to a URL and send them some XML. It’s maybe not that hard but I think they could provide us with some simple page where you just paste some XML and sent the request. But you have to do it yourself. The only thing I had issues with was signing the request and getting your “encrypted” secret.

I found some code on Stack Overflow (as usual) and just made some small modifications to it for doing a Cloud Front setup request.

public static void InvalidateContent() {
    string httpDate = Helper.GetHttpDate();

    string AWSSecret = "YOUR SECRET";
    string AWSAccessKeyId = "YOUR ACCESS KEY";

    ASCIIEncoding encoding = new ASCIIEncoding();
    string postData = @"<DistributionConfig xmlns='http://cloudfront.amazonaws.com/doc/2010-11-01/'>
        <CustomOrigin>
            <DNSName>dist.alternativeto.net</DNSName>
            <HTTPPort>80</HTTPPort>
            <HTTPSPort>443</HTTPSPort>
            <OriginProtocolPolicy>match-viewer</OriginProtocolPolicy>
        </CustomOrigin>
        <CallerReference>" + httpDate + @"</CallerReference>
        <CNAME>static.alternativeto.net</CNAME>
        <Comment>My comments</Comment>
        <Enabled>true</Enabled>
        <Logging>
            <Bucket>mylogs.s3.amazonaws.com</Bucket>
            <Prefix>myprefix/</Prefix>
        </Logging>
    </DistributionConfig>";
    byte[] data = encoding.GetBytes(postData);

    // Prepare web request...
    HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create("https://cloudfront.amazonaws.com/2010-11-01/distribution");
    webRequest.Method = "POST";
    webRequest.ContentType = "text/xml";
    webRequest.Headers.Add("x-amz-date", httpDate);

    Encoding ae = new UTF8Encoding();
    HMACSHA1 signature = new HMACSHA1(ae.GetBytes(AWSSecret.ToCharArray()));

    string b64 = Convert.ToBase64String(signature.ComputeHash(ae.GetBytes(webRequest.Headers["x-amz-date"].ToCharArray())));

    webRequest.Headers.Add(HttpRequestHeader.Authorization, "AWS" + " " + AWSAccessKeyId + ":" + b64);

    webRequest.ContentLength = data.Length;

    Stream newStream = webRequest.GetRequestStream();
    // Send the data.
    newStream.Write(data, 0, data.Length);
    newStream.Close();
}

/// <summary>
/// Gets a proper HTTP date
/// </summary>
public static string GetHttpDate() {
    // Setting the Culture will ensure we get a proper HTTP Date.
    string date = System.DateTime.UtcNow.ToString("ddd, dd MMM yyyy HH:mm:ss ", System.Globalization.CultureInfo.InvariantCulture) + "GMT";
    return date;
}
Categories: UncategorizedTagged: asp .net ajax, asp net blogs, asp net boiler plate, asp net core cms, asp net core interview questions and answers for experienced, asp net core pdf viewer, asp net core web development, asp net crud, asp net environment variables, asp net in hindi, asp net msdn, asp net mvc generate pdf from view, asp net mvc interview questions and answers pdf, asp net pdf component, asp net pdf editor, asp net read excel file, asp net server, asp net table, asp net web pages, building microservices using asp net core 5.0 and docker, cheap asp net hosting, control state in asp net, convert pdf to html asp net, interview questions on asp net for experienced, jquery asp net, kudvenkat asp net core, passport authentication in asp net, shopping cart in asp net c# source code, textarea in asp net, validation for email in asp net

Post navigation

Previous Previous post: Use your IIS logs with WCAT
Next Next post: Debug and fix the z-index bug in IE6 and IE7

Related Posts

  • Debug and fix the z-index bug in IE6 and IE7

    Internet Explorer 6 and 7 has a really really annoying bug that makes it really hard to use z-index. I won’t go into any specifics about exactly what is causing this and so on because others have already done that. I just want to tell you that I found this awesome jQuery script that injects z-index that

    Posted by Judy Alvarez Posted on February 28, 2022March 3, 2022
    0
  • Use your IIS logs with WCAT

    I just want to write this down so I do not forget it. I found this article that describes how to convert your IIS logs to a script that you can run with WCAT. http://theether.net/kb/100128 I had to change the HOST header to the script that was generated. And as you can see you can

    Posted by Judy Alvarez Posted on February 28, 2022March 1, 2022
    0
  • SEO: How the Panda update affected AlternativeTo

    This Monday we noticed a sudden drop in traffic from Google.com on AlternativeTo.net. I and my colleague have tried to figure out why all week and finally we found the answer. In the Google Webmaster Blog, there is a post about how Google is deploying its new spam filter (named “Panda”) to all English-speaking markets. And that

    Posted by Judy Alvarez Posted on February 28, 2022February 28, 2022
    0
  • Issues when you upgrade WordPress plugins and core on ISS 7.5

    I have had lots of issues with upgrading WordPress plugins and the core WordPress engine on my IIS 7.5 server. The problem has been that you try to upgrade the plugin, you get a fail notice AND the plugin is simply disabled and you lose permission to the folder where the plugin was located. Then

    Posted by Judy Alvarez Posted on February 28, 2022March 1, 2022
    0
  • Using an external SMTP server to send mail via different addresses in Gmail

    There is an awesome feature in Gmail that makes it stand out from many other webmails and it’s that you can specify an external SMTP server so that you can send mail from other e-mails. I understand this is an advanced feature that not so many people use but it has a major flaw. If

    Posted by Judy Alvarez Posted on February 28, 2022March 3, 2022
    0
  • MS SQL: Saving changes is not permitted if the table needs to be re-created

    This is a classic. Microsoft SQL Server 2008 introduced a feature that prevents you from changing a table in the designer if it needs to be re-created. And whenever I get this I always forget where to turn this feature off. This time I recorded a 14-sec video that shows where this option is. Enjoy 

    Posted by Judy Alvarez Posted on February 28, 2022March 1, 2022
    0
Judy Alvarez

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Codepolice

  • Github
  • Atlassian
  • Flatlogic
  • Xero
  • Jetbrains
  • Figma
  • Debug and fix the z-index bug in IE6 and IE7
  • Signing a request to set up a custom Amazon CloudFront Distribution with C#
  • Use your IIS logs with WCAT
  • SEO: How the Panda update affected AlternativeTo
  • Issues when you upgrade WordPress plugins and core on ISS 7.5

COPYRIGHT © 2022 - Codepolice