Understanding Startup.CS in .Net Core

.Net Core
One file that has always gotten to me in a .Net Core project is the Startup.CS file. It seems like a simple class but when it came to knowing which method to update I would get confused... Is it the ConigureServices method or Configure method. I have decided to help demystify the startup file and explain what it does, how the methods are used, and which method to update when. In The Beginning A long long time ago there was something called OWIN which introduced me to the Startup.CS file. It was simple, it had one method called Configuration and it took in an IAppBuilder that allowed you to configure your application by injecting middleware into the OWIN pipeline. With the introduction to .Net Core dependency injection configuration was brought…
Read More

Git Commands for Beginners

Git
Git is becoming a popular version control for all the things. I have used it primarily in my day to day activities as a software engineer and wanted to help those getting started. To do so, I plan to do a series of posts to help better understand git and how it works. The documentation for git - https://git-scm.com/ - is helpful when wanting to have a deeper understanding of how these commands work. Git Init If you are looking to get started with an empty repository:https://git-scm.com/docs/git-init git init This will turn whatever directory you are in when you run the command into a git repository. Allowing you to start versioning your files in that directory. Git Clone If there is a repository already created and you would like bring…
Read More

How to Integrate with Stripe ASP.NET Core 2.2

ASP.NET Core
The Stripe API is fabulous at documentation and giving examples of how to do all sorts of things to allow your application to take credit cards. This example will describe how to create a simple charge using ASP.NET Core 2.2 First we will need to install the Stripe.Net nuget package. Then we will need to create the StripeChargeCreateOptions model and fill in some of the properties. var chargeOptions = new StripeChargeCreateOptions() { Amount = 200, Currency = "usd", Description = $"Charge to {customer_email} for {product_name}", Capture = true, SourceTokenOrExistingSourceId = "tokenId_from_stripe_libraries" }; The "tokenId_stripe_libraries" is going to be the id returned from Stripe when using the Stripe libraries and validating your request with your public Stripe API key. We then just need to create the StripeChargeService and have it create…
Read More

How to Create Email Templates with ASP.NET Core 2.2

ASP.NET Core
Hello World Email Template Having your application send nice looking emails is becoming a must when notifying your customers of some action or item related to your business. This tutorial will walk you through how to add the ability for your ASP.NET Core application to send customizable email templates using Razor pages, HTML, and CSS. The code for this sample is available at - https://github.com/derekarends/dotnetcore-emailtemplateengine First we will want to create a couple projects, one will be the main project that sends emails called Api the other being a project for templates we will simple call Templates. Project Setup To start we will create an ASP.NET Core 2.2 Web Application with a Project Type of Web API and call it Api. Next we will want to create another project called…
Read More

How to Create an HTTP Request in ASP.NET Core 2.2

ASP.NET Core
Sometimes your application may need to talk to an external application. One way to accomplish this is through an HTTP request. I will be walking you through how to create an HTTP POST and GET request in ASP.NET Core 2.2. Sample code is available on github - https://github.com/derekarends/dotnetcore-httprequest HTTP GET Request The first thing we will want to do is create a single instance of the HttpClient. This is to prevent socket exhaustion. To learn more about it - https://docs.microsoft.com/en-us/dotnet/api/system.net.http.httpclient?view=netcore-2.2#remarks private static readonly HttpClient HttpClient = new HttpClient(); The first request we will create is probably one of the simpler ones and that is an HTTP GET request. For demo purposes I created another API controller called ExternalEndpointController that our ValuesController will make it's HTTP requests to but the sample…
Read More

How to Setup ASP.NET Core 2.2 with a Multiple Build Dockerfile

ASP.NET Core, Docker
Now that Docker is becoming a must have when creating small services and needing them to be resilient and easy to deploy, I wanted to create a simple how-to for using ASP.NET Core 2.2 and a Dockerfile with multiple build steps. This tutorial assumes you know how to create an ASP.NET Core 2.2 project but for context on how the Dockerfile is written, here is my project structure. Project - src -- Api --- Api.csproj -- Core --- Core.csproj - Dockerfile Dockerfile FROM microsoft/dotnet:2.2-sdk AS build WORKDIR /src COPY . . RUN dotnet restore RUN dotnet build -c Release --no-restore FROM build AS publish RUN dotnet publish /src/Api/Api.csproj -c Release -o /app --no-restore FROM microsoft/dotnet:2.2-aspnetcore-runtime WORKDIR /src COPY --from=publish /app . ENTRYPOINT [ "dotnet", "Api.dll"] Walkthrough The Dockerfile is short…
Read More

Simple Ways to Greatly Increase Website Security

Security
Adding just a few extra headers to your website's response can help improve the overall security of your website. By doing this it will help prevent clickjacking, MIME based, and cross-site scripting attacks. Clickjacking Clickjacking occurs when an attacker is able to load a transparent page over top your website. This allows an unsuspecting user to think they are clicking items and actions on your website but are really interacting with the invisible page. This would allow malicious actions to occur that the user never intended. To help prevent this we can add the following header. # if you need to allow [i]frames, you can use SAMEORIGIN # or even set a uri with ALLOW-FROM uri add_header X-Frame-Options SAMEORIGIN; For additional X-Frame-Options visit - https://developer.mozilla.org/en-US/docs/HTTP/X-Frame-Options MIME based attacks MIME based…
Read More

How to Create a Global Logger for ASP.NET Core 2.2

ASP.NET Core
Catching all errors is important, not just for helping developers troubleshoot their application but also to help provider a better user experience when handled correctly. I want to help make software a better experience for everyone so here is how I implemented a global logger for ASP.NET Core 2.2 Full Code Example available on Github - https://github.com/derekarends/dotnetcore-globallogger One of my favorite libraries for logging is called Serilog - https://serilog.net/ - It provides a TON of different types of what they call "sinks" or ways of logging. This will be the one used in this tutorial but the basic concepts are the same. First you will want to install a few Nuget Packages. Serilog.AspNetCore - Main package for Serilogging in ASP.NET CoreSerilog.Settings.Configuration - Allows us to configure Serilog via appsettings.json filesSerilog.Sinks.Async…
Read More

How to Add Localization to ASP.NET Core 2.2

ASP.NET Core
With the need for localization becoming more and more popular, I thought I would create a little how-to article for localizing ASP.NET Core 2.2 applications where the localized resx files are in a separate project. I am going to make the assumptions you already know how to create/build/run a .NET Core application and the full code sample is available on GitHub - https://github.com/derekarends/dotnetcore-localization For project setup we will want a new ASP.NET Core Web Application called DotNetLocalization for the solution name and API for the project name. Once the solution is created we will then add a new .NET Core Class Library targeting the .netstandard2.0 framework project called Core. Your project should look similar to: Project Structure Core Project Setup We will want to add a few files to the…
Read More

What Is .NET Core?

ASP.NET Core
.NET Core is the latest .NET implementation with a twist. Microsoft has decided to create it as a open source project that allows running .NET on multiple operating systems. With this new approach Microsoft has opened up a world for .NET developers that was once off limits. Using .NET Core allows you to build performant, cross platform console and ASP.NET web applications. This lets developers and businesses start to use other operating systems on their servers, which in todays world is usually hosted in "the cloud" but we all know Linux servers are cheaper than Windows servers so being able to build for Linux already has one advantage. .NET Core vs .NET Standard In this new era of open source .NET, Microsoft created .NET Core and .NET Standard. Why was…
Read More