├── .gitattributes ├── .gitignore ├── 01 Lightweight API (no MVC) └── LightweightApi │ ├── LightweightApi.csproj │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── appsettings.json │ └── web.config ├── 02 Lightweight API with authentication └── LightweightApiWithAuth │ ├── HttpExtensions.cs │ ├── IdentityServerBuilderTestExtensions.cs │ ├── LightweightApiWithAuth.csproj │ ├── Models │ ├── Contact.cs │ └── InMemoryContactRepository.cs │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── appsettings.json │ └── web.config ├── 03 MVC Core API └── MvcCoreApi │ ├── Controllers │ └── ContactsController.cs │ ├── Models │ ├── Contact.cs │ ├── IContactRepository.cs │ └── InMemoryContactRepository.cs │ ├── MvcCoreApi.csproj │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── appsettings.json │ └── web.config ├── 04 MVC Core API with versioning └── MvcCoreApiWithVersioning │ ├── Controllers │ ├── ContactsController.cs │ └── ContactsV2Controller.cs │ ├── MediaTypeApiVersionReader.cs │ ├── Models │ ├── Contact.cs │ ├── IContactRepository.cs │ └── InMemoryContactRepository.cs │ ├── MvcCoreApiWithVersioning.csproj │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── Startup.cs │ ├── StringExtensions.cs │ ├── appsettings.json │ └── web.config ├── 05 MVC Core API with authentication └── MvcCoreApiWithAuthentication │ ├── Controllers │ └── ContactsController.cs │ ├── IdentityServerBuilderTestExtensions.cs │ ├── Models │ ├── Contact.cs │ ├── IContactRepository.cs │ └── InMemoryContactRepository.cs │ ├── MvcCoreApiWithAuthentication.csproj │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── Startup.cs │ ├── appsettings.json │ └── web.config ├── 06 MVC Core API with documentation └── MvcCoreApiWithDocs │ ├── Controllers │ └── ContactsController.cs │ ├── IdentityServerBuilderTestExtensions.cs │ ├── Models │ ├── Contact.cs │ ├── IContactRepository.cs │ └── InMemoryContactRepository.cs │ ├── MvcCoreApiWithDocs.csproj │ ├── Program.cs │ ├── Properties │ └── launchSettings.json │ ├── ScopesDefinitionOperationFilter.cs │ ├── Startup.cs │ ├── appsettings.json │ └── web.config ├── LICENSE ├── LightweightApi.sln └── README.md /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | # http://davidlaing.com/2012/09/19/customise-your-gitattributes-to-become-a-git-ninja/ 3 | * text=auto 4 | 5 | # Custom for Visual Studio 6 | *.cs diff=csharp 7 | *.sln merge=union 8 | *.csproj merge=union 9 | *.vbproj merge=union 10 | *.fsproj merge=union 11 | *.dbproj merge=union 12 | 13 | # Standard to msysgit 14 | *.doc diff=astextplain 15 | *.DOC diff=astextplain 16 | *.docx diff=astextplain 17 | *.DOCX diff=astextplain 18 | *.dot diff=astextplain 19 | *.DOT diff=astextplain 20 | *.pdf diff=astextplain 21 | *.PDF diff=astextplain 22 | *.rtf diff=astextplain 23 | *.RTF diff=astextplain -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ## Ignore Visual Studio temporary files, build results, and 2 | ## files generated by popular Visual Studio add-ons. 3 | 4 | # User-specific files 5 | *.suo 6 | *.user 7 | *.userosscache 8 | *.sln.docstates 9 | 10 | # User-specific files (MonoDevelop/Xamarin Studio) 11 | *.userprefs 12 | 13 | # Build results 14 | [Dd]ebug/ 15 | [Dd]ebugPublic/ 16 | [Rr]elease/ 17 | [Rr]eleases/ 18 | x64/ 19 | x86/ 20 | bld/ 21 | [Bb]in/ 22 | [Oo]bj/ 23 | [Ll]og/ 24 | 25 | # Visual Studio 2015 cache/options directory 26 | .vs/ 27 | # Uncomment if you have tasks that create the project's static files in wwwroot 28 | #wwwroot/ 29 | 30 | # MSTest test Results 31 | [Tt]est[Rr]esult*/ 32 | [Bb]uild[Ll]og.* 33 | 34 | # NUNIT 35 | *.VisualState.xml 36 | TestResult.xml 37 | 38 | # Build Results of an ATL Project 39 | [Dd]ebugPS/ 40 | [Rr]eleasePS/ 41 | dlldata.c 42 | 43 | # DNX 44 | project.lock.json 45 | artifacts/ 46 | 47 | *_i.c 48 | *_p.c 49 | *_i.h 50 | *.ilk 51 | *.meta 52 | *.obj 53 | *.pch 54 | *.pdb 55 | *.pgc 56 | *.pgd 57 | *.rsp 58 | *.sbr 59 | *.tlb 60 | *.tli 61 | *.tlh 62 | *.tmp 63 | *.tmp_proj 64 | *.log 65 | *.vspscc 66 | *.vssscc 67 | .builds 68 | *.pidb 69 | *.svclog 70 | *.scc 71 | 72 | # Chutzpah Test files 73 | _Chutzpah* 74 | 75 | # Visual C++ cache files 76 | ipch/ 77 | *.aps 78 | *.ncb 79 | *.opendb 80 | *.opensdf 81 | *.sdf 82 | *.cachefile 83 | *.VC.db 84 | *.VC.VC.opendb 85 | 86 | # Visual Studio profiler 87 | *.psess 88 | *.vsp 89 | *.vspx 90 | *.sap 91 | 92 | # TFS 2012 Local Workspace 93 | $tf/ 94 | 95 | # Guidance Automation Toolkit 96 | *.gpState 97 | 98 | # ReSharper is a .NET coding add-in 99 | _ReSharper*/ 100 | *.[Rr]e[Ss]harper 101 | *.DotSettings.user 102 | 103 | # JustCode is a .NET coding add-in 104 | .JustCode 105 | 106 | # TeamCity is a build add-in 107 | _TeamCity* 108 | 109 | # DotCover is a Code Coverage Tool 110 | *.dotCover 111 | 112 | # NCrunch 113 | _NCrunch_* 114 | .*crunch*.local.xml 115 | nCrunchTemp_* 116 | 117 | # MightyMoose 118 | *.mm.* 119 | AutoTest.Net/ 120 | 121 | # Web workbench (sass) 122 | .sass-cache/ 123 | 124 | # Installshield output folder 125 | [Ee]xpress/ 126 | 127 | # DocProject is a documentation generator add-in 128 | DocProject/buildhelp/ 129 | DocProject/Help/*.HxT 130 | DocProject/Help/*.HxC 131 | DocProject/Help/*.hhc 132 | DocProject/Help/*.hhk 133 | DocProject/Help/*.hhp 134 | DocProject/Help/Html2 135 | DocProject/Help/html 136 | 137 | # Click-Once directory 138 | publish/ 139 | 140 | # Publish Web Output 141 | *.[Pp]ublish.xml 142 | *.azurePubxml 143 | # TODO: Comment the next line if you want to checkin your web deploy settings 144 | # but database connection strings (with potential passwords) will be unencrypted 145 | *.pubxml 146 | *.publishproj 147 | 148 | # Microsoft Azure Web App publish settings. Comment the next line if you want to 149 | # checkin your Azure Web App publish settings, but sensitive information contained 150 | # in these scripts will be unencrypted 151 | PublishScripts/ 152 | 153 | # NuGet Packages 154 | *.nupkg 155 | # The packages folder can be ignored because of Package Restore 156 | **/packages/* 157 | # except build/, which is used as an MSBuild target. 158 | !**/packages/build/ 159 | # Uncomment if necessary however generally it will be regenerated when needed 160 | #!**/packages/repositories.config 161 | # NuGet v3's project.json files produces more ignoreable files 162 | *.nuget.props 163 | *.nuget.targets 164 | 165 | # Microsoft Azure Build Output 166 | csx/ 167 | *.build.csdef 168 | 169 | # Microsoft Azure Emulator 170 | ecf/ 171 | rcf/ 172 | 173 | # Windows Store app package directories and files 174 | AppPackages/ 175 | BundleArtifacts/ 176 | Package.StoreAssociation.xml 177 | _pkginfo.txt 178 | 179 | # Visual Studio cache files 180 | # files ending in .cache can be ignored 181 | *.[Cc]ache 182 | # but keep track of directories ending in .cache 183 | !*.[Cc]ache/ 184 | 185 | # Others 186 | ClientBin/ 187 | ~$* 188 | *~ 189 | *.dbmdl 190 | *.dbproj.schemaview 191 | *.pfx 192 | *.publishsettings 193 | node_modules/ 194 | orleans.codegen.cs 195 | 196 | # Since there are multiple workflows, uncomment next line to ignore bower_components 197 | # (https://github.com/github/gitignore/pull/1529#issuecomment-104372622) 198 | #bower_components/ 199 | 200 | # RIA/Silverlight projects 201 | Generated_Code/ 202 | 203 | # Backup & report files from converting an old project file 204 | # to a newer Visual Studio version. Backup files are not needed, 205 | # because we have git ;-) 206 | _UpgradeReport_Files/ 207 | Backup*/ 208 | UpgradeLog*.XML 209 | UpgradeLog*.htm 210 | 211 | # SQL Server files 212 | *.mdf 213 | *.ldf 214 | 215 | # Business Intelligence projects 216 | *.rdl.data 217 | *.bim.layout 218 | *.bim_*.settings 219 | 220 | # Microsoft Fakes 221 | FakesAssemblies/ 222 | 223 | # GhostDoc plugin setting file 224 | *.GhostDoc.xml 225 | 226 | # Node.js Tools for Visual Studio 227 | .ntvs_analysis.dat 228 | 229 | # Visual Studio 6 build log 230 | *.plg 231 | 232 | # Visual Studio 6 workspace options file 233 | *.opt 234 | 235 | # Visual Studio LightSwitch build output 236 | **/*.HTMLClient/GeneratedArtifacts 237 | **/*.DesktopClient/GeneratedArtifacts 238 | **/*.DesktopClient/ModelManifest.xml 239 | **/*.Server/GeneratedArtifacts 240 | **/*.Server/ModelManifest.xml 241 | _Pvt_Extensions 242 | 243 | # Paket dependency manager 244 | .paket/paket.exe 245 | paket-files/ 246 | 247 | # FAKE - F# Make 248 | .fake/ 249 | 250 | # JetBrains Rider 251 | .idea/ 252 | *.sln.iml 253 | /.vscode/launch.json 254 | /.vscode/tasks.json 255 | -------------------------------------------------------------------------------- /01 Lightweight API (no MVC)/LightweightApi/LightweightApi.csproj: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | netcoreapp2.0 5 | true 6 | LightweightApi 7 | Exe 8 | LightweightApi 9 | 10 | 11 | 12 | 13 | PreserveNewest 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | -------------------------------------------------------------------------------- /01 Lightweight API (no MVC)/LightweightApi/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.ComponentModel.DataAnnotations; 4 | using System.IO; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | using Microsoft.AspNetCore.Hosting; 8 | using Microsoft.AspNetCore.Builder; 9 | using Microsoft.AspNetCore.Http; 10 | using Newtonsoft.Json; 11 | using Microsoft.AspNetCore.Routing; 12 | using Microsoft.Extensions.Configuration; 13 | using Microsoft.Extensions.DependencyInjection; 14 | using Microsoft.Extensions.Logging; 15 | using System.Text; 16 | using Microsoft.AspNetCore.WebUtilities; 17 | using System.Buffers; 18 | 19 | namespace LightweightApi 20 | { 21 | public class Program 22 | { 23 | public static void Main(string[] args) 24 | { 25 | var host = new WebHostBuilder() 26 | .UseKestrel() 27 | .UseContentRoot(Directory.GetCurrentDirectory()) 28 | .ConfigureAppConfiguration((hostingContext, config) => 29 | { 30 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); 31 | }) 32 | .ConfigureLogging((hostingContext, l) => 33 | { 34 | l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 35 | l.AddConsole(); 36 | }) 37 | .UseIISIntegration() 38 | .ConfigureServices(s => s.AddRouting()) 39 | .Configure(app => 40 | { 41 | // define all API endpoints 42 | app.UseRouter(r => 43 | { 44 | var contactRepo = new InMemoryContactRepository(); 45 | 46 | r.MapGet("contacts", async (request, response, routeData) => 47 | { 48 | var contacts = await contactRepo.GetAll(); 49 | response.WriteJson(contacts); 50 | }); 51 | }); 52 | }) 53 | .Build(); 54 | 55 | host.Run(); 56 | } 57 | } 58 | 59 | public static class HttpExtensions 60 | { 61 | private static readonly JsonArrayPool JsonArrayPool = new JsonArrayPool(ArrayPool.Shared); 62 | 63 | public static void WriteJson(this HttpResponse response, T obj) 64 | { 65 | response.ContentType = "application/json"; 66 | 67 | var serializer = JsonSerializer.CreateDefault(); 68 | using (var writer = new HttpResponseStreamWriter(response.Body, Encoding.UTF8)) 69 | { 70 | using (var jsonWriter = new JsonTextWriter(writer)) 71 | { 72 | jsonWriter.ArrayPool = JsonArrayPool; 73 | jsonWriter.CloseOutput = false; 74 | jsonWriter.AutoCompleteOnClose = false; 75 | 76 | serializer.Serialize(jsonWriter, obj); 77 | } 78 | } 79 | } 80 | 81 | public static T ReadFromJson(this HttpContext httpContext) 82 | { 83 | var serializer = JsonSerializer.CreateDefault(); 84 | using (var streamReader = new HttpRequestStreamReader(httpContext.Request.Body, Encoding.UTF8)) 85 | using (var jsonTextReader = new JsonTextReader(streamReader)) 86 | { 87 | jsonTextReader.ArrayPool = JsonArrayPool; 88 | jsonTextReader.CloseInput = false; 89 | 90 | var obj = serializer.Deserialize(jsonTextReader); 91 | 92 | var results = new List(); 93 | if (Validator.TryValidateObject(obj, new ValidationContext(obj), results)) 94 | { 95 | return obj; 96 | } 97 | 98 | httpContext.Response.StatusCode = 400; 99 | httpContext.Response.WriteJson(results); 100 | 101 | return default(T); 102 | } 103 | } 104 | } 105 | 106 | public class Contact 107 | { 108 | public int ContactId { get; set; } 109 | 110 | [Required] 111 | public string Name { get; set; } 112 | 113 | public string Address { get; set; } 114 | 115 | public string City { get; set; } 116 | } 117 | 118 | public class InMemoryContactRepository 119 | { 120 | private readonly List _contacts = new List 121 | { 122 | new Contact { ContactId = 1, Name = "Filip W", Address = "Bahnhofstrasse 1", City = "Zurich" }, 123 | new Contact { ContactId = 2, Name = "Josh Donaldson", Address = "1 Blue Jays Way", City = "Toronto" }, 124 | new Contact { ContactId = 3, Name = "Aaron Sanchez", Address = "1 Blue Jays Way", City = "Toronto" }, 125 | new Contact { ContactId = 4, Name = "Jose Bautista", Address = "1 Blue Jays Way", City = "Toronto" }, 126 | new Contact { ContactId = 5, Name = "Edwin Encarnacion", Address = "1 Blue Jays Way", City = "Toronto" } 127 | }; 128 | 129 | public Task> GetAll() 130 | { 131 | return Task.FromResult(_contacts.AsEnumerable()); 132 | } 133 | 134 | public Task Get(int id) 135 | { 136 | return Task.FromResult(_contacts.FirstOrDefault(x => x.ContactId == id)); 137 | } 138 | 139 | public Task Add(Contact contact) 140 | { 141 | var newId = (_contacts.LastOrDefault()?.ContactId ?? 0) + 1; 142 | contact.ContactId = newId; 143 | _contacts.Add(contact); 144 | return Task.FromResult(newId); 145 | } 146 | 147 | public async Task Update(Contact updatedContact) 148 | { 149 | var contact = await Get(updatedContact.ContactId).ConfigureAwait(false); 150 | if (contact == null) 151 | { 152 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", updatedContact.ContactId)); 153 | } 154 | 155 | contact.Address = updatedContact.Address; 156 | contact.City = updatedContact.City; 157 | contact.Name = updatedContact.Name; 158 | } 159 | 160 | public async Task Delete(int id) 161 | { 162 | var contact = await Get(id).ConfigureAwait(false); 163 | if (contact == null) 164 | { 165 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", id)); 166 | } 167 | 168 | _contacts.Remove(contact); 169 | } 170 | } 171 | 172 | class JsonArrayPool : IArrayPool 173 | { 174 | private readonly ArrayPool _inner; 175 | 176 | public JsonArrayPool(ArrayPool inner) 177 | { 178 | if (inner == null) 179 | { 180 | throw new ArgumentNullException(nameof(inner)); 181 | } 182 | 183 | _inner = inner; 184 | } 185 | 186 | public T[] Rent(int minimumLength) 187 | { 188 | return _inner.Rent(minimumLength); 189 | } 190 | 191 | public void Return(T[] array) 192 | { 193 | if (array == null) 194 | { 195 | throw new ArgumentNullException(nameof(array)); 196 | } 197 | 198 | _inner.Return(array); 199 | } 200 | } 201 | } 202 | -------------------------------------------------------------------------------- /01 Lightweight API (no MVC)/LightweightApi/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:34916/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "contacts", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "LightweightApi": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "http://localhost:5000/api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /01 Lightweight API (no MVC)/LightweightApi/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /01 Lightweight API (no MVC)/LightweightApi/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/HttpExtensions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.ComponentModel.DataAnnotations; 3 | using System.IO; 4 | using System.Threading.Tasks; 5 | using Microsoft.AspNetCore.Http; 6 | using Newtonsoft.Json; 7 | using Microsoft.AspNetCore.WebUtilities; 8 | using System.Text; 9 | 10 | namespace LightweightApiWithAuth 11 | { 12 | public static class HttpExtensions 13 | { 14 | private static readonly JsonSerializer Serializer = new JsonSerializer(); 15 | 16 | public static void WriteJson(this HttpResponse response, T obj) 17 | { 18 | response.ContentType = "application/json"; 19 | using (var writer = new HttpResponseStreamWriter(response.Body, Encoding.UTF8)) 20 | { 21 | using (var jsonWriter = new JsonTextWriter(writer)) 22 | { 23 | jsonWriter.CloseOutput = false; 24 | jsonWriter.AutoCompleteOnClose = false; 25 | 26 | Serializer.Serialize(jsonWriter, obj); 27 | } 28 | } 29 | } 30 | 31 | public static T ReadFromJson(this HttpContext httpContext) 32 | { 33 | using (var streamReader = new StreamReader(httpContext.Request.Body)) 34 | using (var jsonTextReader = new JsonTextReader(streamReader)) 35 | { 36 | var obj = Serializer.Deserialize(jsonTextReader); 37 | 38 | var results = new List(); 39 | if (Validator.TryValidateObject(obj, new ValidationContext(obj), results)) 40 | { 41 | return obj; 42 | } 43 | 44 | httpContext.Response.StatusCode = 400; 45 | httpContext.Response.WriteJson(results); 46 | 47 | return default(T); 48 | } 49 | } 50 | } 51 | } -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/IdentityServerBuilderTestExtensions.cs: -------------------------------------------------------------------------------- 1 | using IdentityServer4.Models; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace LightweightApiWithAuth 5 | { 6 | public static class IdentityServerBuilderTestExtensions 7 | { 8 | public static IIdentityServerBuilder AddTestClients(this IIdentityServerBuilder builder) 9 | { 10 | return builder.AddInMemoryClients(new[] { new Client 11 | { 12 | ClientId = "client1", 13 | ClientSecrets = 14 | { 15 | new Secret("secret1".Sha256()) 16 | }, 17 | AllowedGrantTypes = new[] 18 | { 19 | GrantType.ClientCredentials 20 | }, 21 | AllowedScopes = new[] 22 | { 23 | "read" 24 | } 25 | }}); 26 | } 27 | 28 | public static IIdentityServerBuilder AddTestResources(this IIdentityServerBuilder builder) 29 | { 30 | return builder.AddInMemoryApiResources(new[] 31 | { 32 | new ApiResource("embedded") 33 | { 34 | Scopes = 35 | { 36 | new Scope("read") 37 | }, 38 | Enabled = true 39 | }, 40 | }); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/LightweightApiWithAuth.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | true 6 | LightweightApiWithAuth 7 | Exe 8 | LightweightApiWithAuth 9 | 10 | 11 | 12 | 13 | PreserveNewest 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/Models/Contact.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace LightweightApiWithAuth.Models 4 | { 5 | public class Contact 6 | { 7 | public int ContactId { get; set; } 8 | 9 | [Required] 10 | public string Name { get; set; } 11 | 12 | public string Address { get; set; } 13 | 14 | public string City { get; set; } 15 | } 16 | } -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/Models/InMemoryContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace LightweightApiWithAuth.Models 7 | { 8 | public class InMemoryContactRepository 9 | { 10 | private readonly List _contacts = new List 11 | { 12 | new Contact { ContactId = 1, Name = "Filip W", Address = "Bahnhofstrasse 1", City = "Zurich" }, 13 | new Contact { ContactId = 2, Name = "Josh Donaldson", Address = "1 Blue Jays Way", City = "Toronto" }, 14 | new Contact { ContactId = 3, Name = "Aaron Sanchez", Address = "1 Blue Jays Way", City = "Toronto" }, 15 | new Contact { ContactId = 4, Name = "Jose Bautista", Address = "1 Blue Jays Way", City = "Toronto" }, 16 | new Contact { ContactId = 5, Name = "Edwin Encarnacion", Address = "1 Blue Jays Way", City = "Toronto" } 17 | }; 18 | 19 | public Task> GetAll() 20 | { 21 | return Task.FromResult(_contacts.AsEnumerable()); 22 | } 23 | 24 | public Task Get(int id) 25 | { 26 | return Task.FromResult(_contacts.FirstOrDefault(x => x.ContactId == id)); 27 | } 28 | 29 | public Task Add(Contact contact) 30 | { 31 | var newId = (_contacts.LastOrDefault()?.ContactId ?? 0) + 1; 32 | contact.ContactId = newId; 33 | _contacts.Add(contact); 34 | return Task.FromResult(newId); 35 | } 36 | 37 | public async Task Update(Contact updatedContact) 38 | { 39 | var contact = await Get(updatedContact.ContactId).ConfigureAwait(false); 40 | if (contact == null) 41 | { 42 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", updatedContact.ContactId)); 43 | } 44 | 45 | contact.Address = updatedContact.Address; 46 | contact.City = updatedContact.City; 47 | contact.Name = updatedContact.Name; 48 | } 49 | 50 | public async Task Delete(int id) 51 | { 52 | var contact = await Get(id).ConfigureAwait(false); 53 | if (contact == null) 54 | { 55 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", id)); 56 | } 57 | 58 | _contacts.Remove(contact); 59 | } 60 | } 61 | } -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/Program.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.IO; 3 | using LightweightApiWithAuth.Models; 4 | using Microsoft.AspNetCore.Authentication.JwtBearer; 5 | using Microsoft.AspNetCore.Authorization; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.AspNetCore.Builder; 8 | using Microsoft.AspNetCore.Routing; 9 | using Microsoft.Extensions.Configuration; 10 | using Microsoft.Extensions.DependencyInjection; 11 | using Microsoft.Extensions.Logging; 12 | using IdentityServer4.AccessTokenValidation; 13 | 14 | namespace LightweightApiWithAuth 15 | { 16 | public class Program 17 | { 18 | public static void Main(string[] args) => 19 | new WebHostBuilder() 20 | .UseKestrel() 21 | .UseContentRoot(Directory.GetCurrentDirectory()) 22 | .ConfigureAppConfiguration((hostingContext, config) => 23 | { 24 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); 25 | }) 26 | .ConfigureLogging((hostingContext, l) => 27 | { 28 | l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 29 | l.AddConsole(); 30 | }) 31 | .UseIISIntegration() 32 | .ConfigureServices(s => 33 | { 34 | // set up embedded identity server 35 | s.AddIdentityServer(). 36 | AddTestClients(). 37 | AddTestResources(). 38 | AddDeveloperSigningCredential(); 39 | 40 | s.AddRouting() 41 | .AddAuthorization(options => 42 | { 43 | // set up authorization policy for the API 44 | options.AddPolicy("API", policy => 45 | { 46 | policy.AddAuthenticationSchemes(JwtBearerDefaults.AuthenticationScheme); 47 | policy.RequireAuthenticatedUser().RequireClaim("scope", "read"); 48 | }); 49 | }) 50 | .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) 51 | .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, o => 52 | { 53 | o.Authority = "http://localhost:5000/openid"; 54 | o.RequireHttpsMetadata = false; 55 | }); 56 | }) 57 | .Configure(app => 58 | { 59 | app.Map("/openid", id => 60 | { 61 | // use embedded identity server to issue tokens 62 | id.UseIdentityServer(); 63 | }) 64 | .UseAuthentication() // consume the JWT tokens in the API 65 | .Use(async (c, next) => // authorize the whole API against the API policy 66 | { 67 | var allowed = await c.RequestServices.GetRequiredService().AuthorizeAsync(c.User, null, "API"); 68 | if (allowed.Succeeded) await next(); 69 | else 70 | c.Response.StatusCode = 401; 71 | }) 72 | .UseRouter(r => // define all API endpoints 73 | { 74 | var contactRepo = new InMemoryContactRepository(); 75 | 76 | r.MapGet("contacts", async (request, response, routeData) => 77 | { 78 | var contacts = await contactRepo.GetAll(); 79 | response.WriteJson(contacts); 80 | }); 81 | 82 | r.MapGet("contacts/{id:int}", async (request, response, routeData) => 83 | { 84 | var contact = await contactRepo.Get(Convert.ToInt32(routeData.Values["id"])); 85 | if (contact == null) 86 | { 87 | response.StatusCode = 404; 88 | return; 89 | } 90 | 91 | response.WriteJson(contact); 92 | }); 93 | }); 94 | }) 95 | .Build().Run(); 96 | } 97 | } -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:34917/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "contacts", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "LightweightApi": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "http://localhost:5000/api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /02 Lightweight API with authentication/LightweightApiWithAuth/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/Controllers/ContactsController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.AspNetCore.Mvc; 3 | using MvcCoreApi.Models; 4 | 5 | namespace MvcCoreApi.Controllers 6 | { 7 | [Route("[controller]")] 8 | public class ContactsController : ControllerBase 9 | { 10 | private readonly IContactRepository _repository; 11 | 12 | public ContactsController(IContactRepository repository) 13 | { 14 | if (repository == null) 15 | throw new System.ArgumentNullException(nameof(repository)); 16 | 17 | _repository = repository; 18 | } 19 | 20 | [HttpGet("")] 21 | public async Task Get() 22 | { 23 | return Ok(await _repository.GetAll()); 24 | } 25 | 26 | [HttpGet("{id}", Name = "GetContactById")] 27 | public async Task Get(int id) 28 | { 29 | var contact = await _repository.Get(id); 30 | if (contact == null) 31 | { 32 | return NotFound(); 33 | } 34 | 35 | return Ok(contact); 36 | } 37 | 38 | [HttpPost("")] 39 | public async Task Post([FromBody]Contact contact) 40 | { 41 | if (ModelState.IsValid) 42 | { 43 | var newId = await _repository.Add(contact); 44 | return CreatedAtRoute("GetContactById", new { id = newId }, contact); 45 | } 46 | 47 | return BadRequest(ModelState); 48 | } 49 | 50 | [HttpPut("{id}")] 51 | public async Task Put(int id, [FromBody]Contact contact) 52 | { 53 | contact.ContactId = id; 54 | await _repository.Update(contact); 55 | return NoContent(); 56 | } 57 | 58 | [HttpDelete("{id}")] 59 | public async Task Delete(int id) 60 | { 61 | var deleted = await _repository.Get(id); 62 | if (deleted == null) 63 | { 64 | return NotFound(); 65 | } 66 | 67 | await _repository.Delete(id); 68 | return NoContent(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/Models/Contact.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace MvcCoreApi.Models 4 | { 5 | public class Contact 6 | { 7 | public int ContactId { get; set; } 8 | 9 | [Required] 10 | public string Name { get; set; } 11 | 12 | public string Address { get; set; } 13 | 14 | public string City { get; set; } 15 | 16 | public string State { get; set; } 17 | 18 | public string Zip { get; set; } 19 | 20 | public string Email { get; set; } 21 | 22 | public string Twitter { get; set; } 23 | } 24 | } -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/Models/IContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace MvcCoreApi.Models 5 | { 6 | public interface IContactRepository 7 | { 8 | Task> GetAll(); 9 | 10 | Task Get(int id); 11 | 12 | Task Add(Contact contact); 13 | 14 | Task Update(Contact updatedContact); 15 | 16 | Task Delete(int id); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/Models/InMemoryContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace MvcCoreApi.Models 7 | { 8 | public class InMemoryContactRepository : IContactRepository 9 | { 10 | private readonly List _contacts = new List 11 | { 12 | new Contact { ContactId = 1, Name = "Filip W", Address = "107 Atlantic Avenue", City = "Toronto", State = "ON", Zip = "M6K 1Y2", Email = "filip.wojcieszyn@climaxmedia.com", Twitter = "filip_woj" }, 13 | new Contact { ContactId = 2, Name = "Josh Donaldson", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joshd@bluejays.com", Twitter = "BringerOfRain20" }, 14 | new Contact { ContactId = 3, Name = "Aaron Sanchez", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "aarons@bluejays.com", Twitter = "A_Sanch41" }, 15 | new Contact { ContactId = 4, Name = "Jose Bautista", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joseb@bluejays.com", Twitter = "JoeyBats19" }, 16 | new Contact { ContactId = 5, Name = "Edwin Encarnacion", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "edwine@bluejays.com", Twitter = "Encadwin" }, 17 | }; 18 | 19 | public Task> GetAll() 20 | { 21 | return Task.FromResult(_contacts.AsEnumerable()); 22 | } 23 | 24 | public Task Get(int id) 25 | { 26 | return Task.FromResult(_contacts.FirstOrDefault(x => x.ContactId == id)); 27 | } 28 | 29 | public Task Add(Contact contact) 30 | { 31 | var newId = (_contacts.LastOrDefault()?.ContactId ?? 0) + 1; 32 | contact.ContactId = newId; 33 | _contacts.Add(contact); 34 | return Task.FromResult(newId); 35 | } 36 | 37 | public async Task Update(Contact updatedContact) 38 | { 39 | var contact = await Get(updatedContact.ContactId).ConfigureAwait(false); 40 | if (contact == null) 41 | { 42 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", updatedContact.ContactId)); 43 | } 44 | 45 | contact.Address = updatedContact.Address; 46 | contact.City = updatedContact.City; 47 | contact.Email = updatedContact.Email; 48 | contact.Name = updatedContact.Name; 49 | contact.State = updatedContact.State; 50 | contact.Twitter = updatedContact.Twitter; 51 | contact.Zip = updatedContact.Zip; 52 | } 53 | 54 | public async Task Delete(int id) 55 | { 56 | var contact = await Get(id).ConfigureAwait(false); 57 | if (contact == null) 58 | { 59 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", id)); 60 | } 61 | 62 | _contacts.Remove(contact); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/MvcCoreApi.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | true 6 | MvcCoreApi 7 | Exe 8 | MvcCoreApi 9 | 10 | 11 | 12 | 13 | PreserveNewest 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Configuration; 4 | using MvcCoreApi.Models; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.Extensions.DependencyInjection; 7 | using Microsoft.Extensions.Logging; 8 | 9 | namespace MvcCoreApi 10 | { 11 | public class Program 12 | { 13 | public static void Main(string[] args) 14 | { 15 | var host = new WebHostBuilder() 16 | .UseKestrel() 17 | .UseContentRoot(Directory.GetCurrentDirectory()) 18 | .ConfigureAppConfiguration((hostingContext, config) => 19 | { 20 | config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); 21 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); 22 | }) 23 | .ConfigureLogging((hostingContext, l) => 24 | { 25 | l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 26 | l.AddConsole(); 27 | }) 28 | .UseIISIntegration() 29 | .ConfigureServices(services => 30 | { 31 | services.AddSingleton(); 32 | services. 33 | AddMvcCore(). 34 | AddDataAnnotations(). 35 | AddJsonFormatters(); 36 | }) 37 | .Configure(app => 38 | { 39 | app.UseMvc(); 40 | }) 41 | .Build(); 42 | 43 | host.Run(); 44 | } 45 | } 46 | } 47 | -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:28136/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "contacts", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AspNetCore.Sample.Api": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "http://localhost:5000/api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /03 MVC Core API/MvcCoreApi/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Controllers/ContactsController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.AspNetCore.Mvc; 3 | using MvcCoreApi.Models; 4 | 5 | namespace MvcCoreApi.Controllers 6 | { 7 | [Route("[controller]")] 8 | [ApiVersion("1")] 9 | public class ContactsController : ControllerBase 10 | { 11 | private readonly IContactRepository _repository; 12 | 13 | public ContactsController(IContactRepository repository) 14 | { 15 | _repository = repository; 16 | } 17 | 18 | [HttpGet("")] 19 | public async Task Get() 20 | { 21 | return Ok(await _repository.GetAll()); 22 | } 23 | 24 | [HttpGet("{id}", Name = "GetContactById")] 25 | public async Task Get(int id) 26 | { 27 | var contact = await _repository.Get(id); 28 | if (contact == null) 29 | { 30 | return NotFound(); 31 | } 32 | 33 | return Ok(contact); 34 | } 35 | 36 | [HttpPost("")] 37 | public async Task Post([FromBody]Contact contact) 38 | { 39 | if (ModelState.IsValid) 40 | { 41 | var newId = await _repository.Add(contact); 42 | return CreatedAtRoute("GetContactById", new { id = newId }, contact); 43 | } 44 | 45 | return BadRequest(ModelState); 46 | } 47 | 48 | [HttpPut("{id}")] 49 | public async Task Put(int id, [FromBody]Contact contact) 50 | { 51 | contact.ContactId = id; 52 | await _repository.Update(contact); 53 | return NoContent(); 54 | } 55 | 56 | [HttpDelete("{id}")] 57 | public async Task Delete(int id) 58 | { 59 | var deleted = await _repository.Get(id); 60 | if (deleted == null) 61 | { 62 | return NotFound(); 63 | } 64 | 65 | await _repository.Delete(id); 66 | return NoContent(); 67 | } 68 | } 69 | } 70 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Controllers/ContactsV2Controller.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.AspNetCore.Mvc; 3 | using MvcCoreApi.Models; 4 | 5 | namespace MvcCoreApi.Controllers 6 | { 7 | [Route("contacts")] 8 | [ApiVersion("2")] 9 | public class ContactsV2Controller : ControllerBase 10 | { 11 | private readonly IContactRepository _repository; 12 | 13 | public ContactsV2Controller(IContactRepository repository) 14 | { 15 | _repository = repository; 16 | } 17 | 18 | [HttpGet("{id}", Name = "GetContactById")] 19 | public async Task Get(int id) 20 | { 21 | var contact = await _repository.Get(id); 22 | if (contact == null) 23 | { 24 | return NotFound(); 25 | } 26 | 27 | return Ok(new ContactV2(contact) 28 | { 29 | CumulusNumber = 12345678 30 | }); 31 | } 32 | } 33 | } 34 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/MediaTypeApiVersionReader.cs: -------------------------------------------------------------------------------- 1 | using System.Linq; 2 | using Microsoft.AspNetCore.Http; 3 | using Microsoft.AspNetCore.Http.Headers; 4 | using Microsoft.Net.Http.Headers; 5 | using Microsoft.AspNetCore.Mvc.Versioning; 6 | 7 | namespace MvcCoreApi 8 | { 9 | public class MediaTypeApiVersionReader : IApiVersionReader 10 | { 11 | private readonly string[] _mediaTypes = { "application/vnd.demo" }; 12 | 13 | public void AddParameters(IApiVersionParameterDescriptionContext context) 14 | { 15 | } 16 | 17 | public string Read(HttpRequest request) 18 | { 19 | var headers = request.GetTypedHeaders(); 20 | 21 | var acceptHeaderVersion = headers.Accept.FirstOrDefault(x => _mediaTypes.Any(a => x.MediaType.ToString().ToLowerInvariant().Contains(a))); 22 | 23 | if (acceptHeaderVersion != null && acceptHeaderVersion.MediaType.ToString().Contains("-v") && 24 | acceptHeaderVersion.MediaType.ToString().Contains("+")) 25 | { 26 | return acceptHeaderVersion.MediaType.ToString().Between("-v", "+"); 27 | } 28 | 29 | return null; 30 | } 31 | } 32 | } -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Models/Contact.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace MvcCoreApi.Models 4 | { 5 | public abstract class CommonContact 6 | { 7 | public int ContactId { get; set; } 8 | 9 | [Required] 10 | public string Name { get; set; } 11 | 12 | public string Address { get; set; } 13 | 14 | public string City { get; set; } 15 | 16 | public string Email { get; set; } 17 | 18 | public string Twitter { get; set; } 19 | } 20 | 21 | public class Contact : CommonContact 22 | { 23 | public string State { get; set; } 24 | 25 | public string Zip { get; set; } 26 | } 27 | 28 | public class ContactV2 : CommonContact 29 | { 30 | public ContactV2(Contact c) 31 | { 32 | ContactId = c.ContactId; 33 | Name = c.Name; 34 | Address = c.Address; 35 | City = c.City; 36 | Email = c.Email; 37 | Twitter = c.Twitter; 38 | } 39 | 40 | public int CumulusNumber { get; set; } 41 | } 42 | } -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Models/IContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace MvcCoreApi.Models 5 | { 6 | public interface IContactRepository 7 | { 8 | Task> GetAll(); 9 | 10 | Task Get(int id); 11 | 12 | Task Add(Contact contact); 13 | 14 | Task Update(Contact updatedContact); 15 | 16 | Task Delete(int id); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Models/InMemoryContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace MvcCoreApi.Models 7 | { 8 | public class InMemoryContactRepository : IContactRepository 9 | { 10 | private readonly List _contacts = new List 11 | { 12 | new Contact { ContactId = 1, Name = "Filip W", Address = "107 Atlantic Avenue", City = "Toronto", State = "ON", Zip = "M6K 1Y2", Email = "filip.wojcieszyn@climaxmedia.com", Twitter = "filip_woj" }, 13 | new Contact { ContactId = 2, Name = "Josh Donaldson", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joshd@bluejays.com", Twitter = "BringerOfRain20" }, 14 | new Contact { ContactId = 3, Name = "Aaron Sanchez", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "aarons@bluejays.com", Twitter = "A_Sanch41" }, 15 | new Contact { ContactId = 4, Name = "Jose Bautista", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joseb@bluejays.com", Twitter = "JoeyBats19" }, 16 | new Contact { ContactId = 5, Name = "Edwin Encarnacion", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "edwine@bluejays.com", Twitter = "Encadwin" }, 17 | }; 18 | 19 | public Task> GetAll() 20 | { 21 | return Task.FromResult(_contacts.AsEnumerable()); 22 | } 23 | 24 | public Task Get(int id) 25 | { 26 | return Task.FromResult(_contacts.FirstOrDefault(x => x.ContactId == id)); 27 | } 28 | 29 | public Task Add(Contact contact) 30 | { 31 | var newId = (_contacts.LastOrDefault()?.ContactId ?? 0) + 1; 32 | contact.ContactId = newId; 33 | _contacts.Add(contact); 34 | return Task.FromResult(newId); 35 | } 36 | 37 | public async Task Update(Contact updatedContact) 38 | { 39 | var contact = await Get(updatedContact.ContactId).ConfigureAwait(false); 40 | if (contact == null) 41 | { 42 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", updatedContact.ContactId)); 43 | } 44 | 45 | contact.Address = updatedContact.Address; 46 | contact.City = updatedContact.City; 47 | contact.Email = updatedContact.Email; 48 | contact.Name = updatedContact.Name; 49 | contact.State = updatedContact.State; 50 | contact.Twitter = updatedContact.Twitter; 51 | contact.Zip = updatedContact.Zip; 52 | } 53 | 54 | public async Task Delete(int id) 55 | { 56 | var contact = await Get(id).ConfigureAwait(false); 57 | if (contact == null) 58 | { 59 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", id)); 60 | } 61 | 62 | _contacts.Remove(contact); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/MvcCoreApiWithVersioning.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | true 6 | MvcCoreApiWithVersioning 7 | Exe 8 | MvcCoreApiWithVersioning 9 | 10 | 11 | 12 | 13 | PreserveNewest 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Configuration; 5 | 6 | namespace MvcCoreApi 7 | { 8 | public class Program 9 | { 10 | public static void Main(string[] args) 11 | { 12 | var host = new WebHostBuilder() 13 | .UseKestrel() 14 | .UseContentRoot(Directory.GetCurrentDirectory()) 15 | .ConfigureAppConfiguration((hostingContext, config) => 16 | { 17 | config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); 18 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); 19 | }) 20 | .ConfigureLogging((hostingContext, l) => 21 | { 22 | l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 23 | l.AddConsole(); 24 | }) 25 | .UseIISIntegration() 26 | .UseStartup() 27 | .Build(); 28 | 29 | host.Run(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:28136/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "contacts", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AspNetCore.Sample.Api": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "http://localhost:5000/api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/Startup.cs: -------------------------------------------------------------------------------- 1 | using Microsoft.AspNetCore.Builder; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.AspNetCore.Mvc; 4 | using Microsoft.Extensions.Configuration; 5 | using Microsoft.Extensions.DependencyInjection; 6 | using Microsoft.Extensions.Logging; 7 | using MvcCoreApi.Models; 8 | 9 | namespace MvcCoreApi 10 | { 11 | public class Startup 12 | { 13 | public void ConfigureServices(IServiceCollection services) 14 | { 15 | services.AddSingleton(); 16 | services.AddMvcCore(). 17 | AddDataAnnotations(). 18 | AddJsonFormatters(); 19 | 20 | services.AddApiVersioning(o => 21 | { 22 | o.DefaultApiVersion = ApiVersion.Parse("1"); 23 | o.AssumeDefaultVersionWhenUnspecified = true; 24 | o.ReportApiVersions = true; 25 | // o.ApiVersionReader = new HeaderApiVersionReader("version"); 26 | 27 | // request "application/vnd.demo-v2+json" 28 | o.ApiVersionReader = new MediaTypeApiVersionReader(); 29 | }); 30 | } 31 | 32 | public void Configure(IApplicationBuilder app, ILoggerFactory loggerFactory) 33 | { 34 | app.UseMvc(); 35 | } 36 | } 37 | } 38 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/StringExtensions.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | 3 | namespace MvcCoreApi 4 | { 5 | public static class StringExtensions 6 | { 7 | public static string Between(this string value, string start, string end) 8 | { 9 | var startPosition = value.IndexOf(start, StringComparison.Ordinal); 10 | var endPosition = value.LastIndexOf(end, StringComparison.Ordinal); 11 | if (startPosition == -1 || endPosition == -1) return string.Empty; 12 | 13 | var adjustedStart = startPosition + start.Length; 14 | return adjustedStart >= endPosition ? string.Empty : value.Substring(adjustedStart, endPosition - adjustedStart); 15 | } 16 | } 17 | } -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /04 MVC Core API with versioning/MvcCoreApiWithVersioning/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Controllers/ContactsController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.AspNetCore.Authorization; 3 | using Microsoft.AspNetCore.Mvc; 4 | using MvcCoreApiWithAuthentication.Models; 5 | 6 | namespace MvcCoreApiWithAuthentication.Controllers 7 | { 8 | [Route("[controller]")] 9 | public class ContactsController : ControllerBase 10 | { 11 | private readonly IContactRepository _repository; 12 | 13 | public ContactsController(IContactRepository repository) 14 | { 15 | _repository = repository; 16 | } 17 | 18 | [HttpGet("")] 19 | public async Task Get() 20 | { 21 | return Ok(await _repository.GetAll()); 22 | } 23 | 24 | [HttpGet("{id}", Name = "GetContactById")] 25 | public async Task Get(int id) 26 | { 27 | var contact = await _repository.Get(id); 28 | if (contact == null) 29 | { 30 | return NotFound(); 31 | } 32 | 33 | return Ok(contact); 34 | } 35 | 36 | [HttpPost("")] 37 | public async Task Post([FromBody]Contact contact) 38 | { 39 | if (ModelState.IsValid) 40 | { 41 | var newId = await _repository.Add(contact); 42 | return CreatedAtRoute("GetContactById", new { id = newId }, contact); 43 | } 44 | 45 | return BadRequest(ModelState); 46 | } 47 | 48 | [HttpPut("{id}")] 49 | [Authorize("WritePolicy")] 50 | public async Task Put(int id, [FromBody]Contact contact) 51 | { 52 | contact.ContactId = id; 53 | await _repository.Update(contact); 54 | return NoContent(); 55 | } 56 | 57 | [HttpDelete("{id}")] 58 | [Authorize("WritePolicy")] 59 | public async Task Delete(int id) 60 | { 61 | var deleted = await _repository.Get(id); 62 | if (deleted == null) 63 | { 64 | return NotFound(); 65 | } 66 | 67 | await _repository.Delete(id); 68 | return NoContent(); 69 | } 70 | } 71 | } 72 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/IdentityServerBuilderTestExtensions.cs: -------------------------------------------------------------------------------- 1 | using IdentityServer4.Models; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace MvcCoreApiWithAuthentication 5 | { 6 | public static class IdentityServerBuilderTestExtensions 7 | { 8 | public static IIdentityServerBuilder AddTestClients(this IIdentityServerBuilder builder) 9 | { 10 | return builder.AddInMemoryClients(new[] { new Client 11 | { 12 | ClientId = "client1", 13 | ClientSecrets = 14 | { 15 | new Secret("secret1".Sha256()) 16 | }, 17 | AllowedGrantTypes = new[] 18 | { 19 | GrantType.ClientCredentials 20 | }, 21 | AllowedScopes = new[] 22 | { 23 | "read", 24 | "write" 25 | } 26 | }}); 27 | } 28 | 29 | public static IIdentityServerBuilder AddTestResources(this IIdentityServerBuilder builder) 30 | { 31 | return builder.AddInMemoryApiResources(new[] 32 | { 33 | new ApiResource("embedded") 34 | { 35 | Scopes = 36 | { 37 | new Scope("read"), 38 | new Scope("write") 39 | }, 40 | Enabled = true 41 | }, 42 | }); 43 | } 44 | } 45 | } -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Models/Contact.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace MvcCoreApiWithAuthentication.Models 4 | { 5 | public class Contact 6 | { 7 | public int ContactId { get; set; } 8 | 9 | [Required] 10 | public string Name { get; set; } 11 | 12 | public string Address { get; set; } 13 | 14 | public string City { get; set; } 15 | 16 | public string State { get; set; } 17 | 18 | public string Zip { get; set; } 19 | 20 | public string Email { get; set; } 21 | 22 | public string Twitter { get; set; } 23 | } 24 | } -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Models/IContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace MvcCoreApiWithAuthentication.Models 5 | { 6 | public interface IContactRepository 7 | { 8 | Task> GetAll(); 9 | 10 | Task Get(int id); 11 | 12 | Task Add(Contact contact); 13 | 14 | Task Update(Contact updatedContact); 15 | 16 | Task Delete(int id); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Models/InMemoryContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace MvcCoreApiWithAuthentication.Models 7 | { 8 | public class InMemoryContactRepository : IContactRepository 9 | { 10 | private readonly List _contacts = new List 11 | { 12 | new Contact { ContactId = 1, Name = "Filip W", Address = "107 Atlantic Avenue", City = "Toronto", State = "ON", Zip = "M6K 1Y2", Email = "filip.wojcieszyn@climaxmedia.com", Twitter = "filip_woj" }, 13 | new Contact { ContactId = 2, Name = "Josh Donaldson", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joshd@bluejays.com", Twitter = "BringerOfRain20" }, 14 | new Contact { ContactId = 3, Name = "Aaron Sanchez", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "aarons@bluejays.com", Twitter = "A_Sanch41" }, 15 | new Contact { ContactId = 4, Name = "Jose Bautista", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joseb@bluejays.com", Twitter = "JoeyBats19" }, 16 | new Contact { ContactId = 5, Name = "Edwin Encarnacion", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "edwine@bluejays.com", Twitter = "Encadwin" }, 17 | }; 18 | 19 | public Task> GetAll() 20 | { 21 | return Task.FromResult(_contacts.AsEnumerable()); 22 | } 23 | 24 | public Task Get(int id) 25 | { 26 | return Task.FromResult(_contacts.FirstOrDefault(x => x.ContactId == id)); 27 | } 28 | 29 | public Task Add(Contact contact) 30 | { 31 | var newId = (_contacts.LastOrDefault()?.ContactId ?? 0) + 1; 32 | contact.ContactId = newId; 33 | _contacts.Add(contact); 34 | return Task.FromResult(newId); 35 | } 36 | 37 | public async Task Update(Contact updatedContact) 38 | { 39 | var contact = await Get(updatedContact.ContactId).ConfigureAwait(false); 40 | if (contact == null) 41 | { 42 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", updatedContact.ContactId)); 43 | } 44 | 45 | contact.Address = updatedContact.Address; 46 | contact.City = updatedContact.City; 47 | contact.Email = updatedContact.Email; 48 | contact.Name = updatedContact.Name; 49 | contact.State = updatedContact.State; 50 | contact.Twitter = updatedContact.Twitter; 51 | contact.Zip = updatedContact.Zip; 52 | } 53 | 54 | public async Task Delete(int id) 55 | { 56 | var contact = await Get(id).ConfigureAwait(false); 57 | if (contact == null) 58 | { 59 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", id)); 60 | } 61 | 62 | _contacts.Remove(contact); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/MvcCoreApiWithAuthentication.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | true 6 | MvcCoreApiWithAuthentication 7 | Exe 8 | MvcCoreApiWithAuthentication 9 | 10 | 11 | 12 | 13 | PreserveNewest 14 | 15 | 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Configuration; 5 | 6 | namespace MvcCoreApiWithAuthentication 7 | { 8 | public class Program 9 | { 10 | public static void Main(string[] args) 11 | { 12 | var host = new WebHostBuilder() 13 | .UseKestrel() 14 | .UseContentRoot(Directory.GetCurrentDirectory()) 15 | .ConfigureAppConfiguration((hostingContext, config) => 16 | { 17 | config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); 18 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); 19 | }) 20 | .ConfigureLogging((hostingContext, l) => 21 | { 22 | l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 23 | l.AddConsole(); 24 | }) 25 | .UseIISIntegration() 26 | .UseStartup() 27 | .Build(); 28 | 29 | host.Run(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:28135/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "contacts", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AspNetCore.Sample.Api": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "http://localhost:5000/api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/Startup.cs: -------------------------------------------------------------------------------- 1 | using IdentityServer4.AccessTokenValidation; 2 | using Microsoft.AspNetCore.Authentication.JwtBearer; 3 | using Microsoft.AspNetCore.Authorization; 4 | using Microsoft.AspNetCore.Builder; 5 | using Microsoft.AspNetCore.Hosting; 6 | using Microsoft.AspNetCore.Mvc.Authorization; 7 | using Microsoft.Extensions.Configuration; 8 | using Microsoft.Extensions.DependencyInjection; 9 | using Microsoft.Extensions.Logging; 10 | using MvcCoreApiWithAuthentication.Models; 11 | 12 | namespace MvcCoreApiWithAuthentication 13 | { 14 | public class Startup 15 | { 16 | public void ConfigureServices(IServiceCollection services) 17 | { 18 | var readPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme). 19 | RequireAuthenticatedUser(). 20 | RequireClaim("scope", "read").Build(); 21 | 22 | var writePolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme). 23 | RequireAuthenticatedUser(). 24 | RequireClaim("scope", "write").Build(); 25 | 26 | services.AddSingleton(); 27 | services.AddMvcCore().AddDataAnnotations(). 28 | AddJsonFormatters(); 29 | 30 | // set up embedded identity server 31 | services.AddIdentityServer(). 32 | AddTestClients(). 33 | AddTestResources(). 34 | AddDeveloperSigningCredential(); 35 | 36 | services.AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) 37 | .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, o => 38 | { 39 | o.Authority = "http://localhost:5000/openid"; 40 | o.RequireHttpsMetadata = false; 41 | }); 42 | } 43 | 44 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 45 | { 46 | app.Map("/openid", id => { 47 | // use embedded identity server to issue tokens 48 | id.UseIdentityServer(); 49 | }); 50 | 51 | app.Map("/api", api => { 52 | // consume the JWT tokens in the API 53 | api.UseAuthentication(); 54 | api.UseMvc(); 55 | }); 56 | } 57 | } 58 | } 59 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /05 MVC Core API with authentication/MvcCoreApiWithAuthentication/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Controllers/ContactsController.cs: -------------------------------------------------------------------------------- 1 | using System.Threading.Tasks; 2 | using Microsoft.AspNetCore.Authorization; 3 | using Microsoft.AspNetCore.Mvc; 4 | using MvcCoreApiWithDocs.Models; 5 | 6 | namespace MvcCoreApiWithDocs.Controllers 7 | { 8 | [Route("[controller]")] 9 | public class ContactsController : ControllerBase 10 | { 11 | private readonly IContactRepository _repository; 12 | 13 | public ContactsController(IContactRepository repository) 14 | { 15 | _repository = repository; 16 | } 17 | 18 | /// 19 | /// Fetch all available contacts 20 | /// 21 | /// Contacts returned (could be an empty array) 22 | [HttpGet("")] 23 | public async Task Get() 24 | { 25 | return Ok(await _repository.GetAll()); 26 | } 27 | 28 | /// 29 | /// Fetch a single contact by its ID 30 | /// 31 | /// ID of the contact (integer) 32 | /// A contact resource 33 | /// No contact with a given ID exists 34 | [HttpGet("{id}", Name = "GetContactById")] 35 | public async Task Get(int id) 36 | { 37 | var contact = await _repository.Get(id); 38 | if (contact == null) 39 | { 40 | return NotFound(); 41 | } 42 | 43 | return Ok(contact); 44 | } 45 | 46 | /// 47 | /// Create a new contact 48 | /// 49 | /// Contact to create. 50 | /// Contact created successfully 51 | /// The payload of the request was invalid. 52 | [HttpPost("")] 53 | [Authorize("WritePolicy")] 54 | public async Task Post([FromBody]Contact contact) 55 | { 56 | if (ModelState.IsValid) 57 | { 58 | var newId = await _repository.Add(contact); 59 | return CreatedAtRoute("GetContactById", new { id = newId }, contact); 60 | } 61 | 62 | return BadRequest(ModelState); 63 | } 64 | 65 | /// 66 | /// Update an existing contact. Note - all of the properties will be overwritten. 67 | /// 68 | /// ID of the contact (integer) 69 | /// Updated contact. 70 | /// Contact updated correctly 71 | /// The payload of the request was invalid. 72 | [HttpPut("{id}")] 73 | [Authorize("WritePolicy")] 74 | public async Task Put(int id, [FromBody]Contact contact) 75 | { 76 | if (ModelState.IsValid) 77 | { 78 | contact.ContactId = id; 79 | await _repository.Update(contact); 80 | return NoContent(); 81 | } 82 | 83 | return BadRequest(ModelState); 84 | } 85 | 86 | /// 87 | /// Remove an existing contact by its ID 88 | /// 89 | /// ID of the contact (integer) 90 | /// Contact successfully removed 91 | /// No contact with a given ID exists 92 | [HttpDelete("{id}")] 93 | [Authorize("WritePolicy")] 94 | public async Task Delete(int id) 95 | { 96 | var deleted = await _repository.Get(id); 97 | if (deleted == null) 98 | { 99 | return NotFound(); 100 | } 101 | 102 | await _repository.Delete(id); 103 | return NoContent(); 104 | } 105 | } 106 | } 107 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/IdentityServerBuilderTestExtensions.cs: -------------------------------------------------------------------------------- 1 | using IdentityServer4.Models; 2 | using Microsoft.Extensions.DependencyInjection; 3 | 4 | namespace MvcCoreApiWithDocs 5 | { 6 | public static class IdentityServerBuilderTestExtensions 7 | { 8 | public static IIdentityServerBuilder AddTestClients(this IIdentityServerBuilder builder) 9 | { 10 | return builder.AddInMemoryClients(new[] { new Client 11 | { 12 | ClientId = "client1", 13 | ClientSecrets = 14 | { 15 | new Secret("secret1".Sha256()) 16 | }, 17 | AllowedGrantTypes = new[] 18 | { 19 | GrantType.ClientCredentials 20 | }, 21 | AllowedScopes = new[] 22 | { 23 | "testscope" 24 | } 25 | }}); 26 | } 27 | 28 | public static IIdentityServerBuilder AddTestResources(this IIdentityServerBuilder builder) 29 | { 30 | return builder.AddInMemoryApiResources(new[] 31 | { 32 | new ApiResource("embedded") 33 | { 34 | Scopes = 35 | { 36 | new Scope("testscope") 37 | }, 38 | Enabled = true 39 | }, 40 | }); 41 | } 42 | } 43 | } -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Models/Contact.cs: -------------------------------------------------------------------------------- 1 | using System.ComponentModel.DataAnnotations; 2 | 3 | namespace MvcCoreApiWithDocs.Models 4 | { 5 | public class Contact 6 | { 7 | public int ContactId { get; set; } 8 | 9 | [Required] 10 | public string Name { get; set; } 11 | 12 | public string Address { get; set; } 13 | 14 | public string City { get; set; } 15 | 16 | public string State { get; set; } 17 | 18 | public string Zip { get; set; } 19 | 20 | public string Email { get; set; } 21 | 22 | public string Twitter { get; set; } 23 | } 24 | } -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Models/IContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.Threading.Tasks; 3 | 4 | namespace MvcCoreApiWithDocs.Models 5 | { 6 | public interface IContactRepository 7 | { 8 | Task> GetAll(); 9 | 10 | Task Get(int id); 11 | 12 | Task Add(Contact contact); 13 | 14 | Task Update(Contact updatedContact); 15 | 16 | Task Delete(int id); 17 | } 18 | } 19 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Models/InMemoryContactRepository.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Threading.Tasks; 5 | 6 | namespace MvcCoreApiWithDocs.Models 7 | { 8 | public class InMemoryContactRepository : IContactRepository 9 | { 10 | private readonly List _contacts = new List 11 | { 12 | new Contact { ContactId = 1, Name = "Filip W", Address = "107 Atlantic Avenue", City = "Toronto", State = "ON", Zip = "M6K 1Y2", Email = "filip.wojcieszyn@climaxmedia.com", Twitter = "filip_woj" }, 13 | new Contact { ContactId = 2, Name = "Josh Donaldson", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joshd@bluejays.com", Twitter = "BringerOfRain20" }, 14 | new Contact { ContactId = 3, Name = "Aaron Sanchez", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "aarons@bluejays.com", Twitter = "A_Sanch41" }, 15 | new Contact { ContactId = 4, Name = "Jose Bautista", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "joseb@bluejays.com", Twitter = "JoeyBats19" }, 16 | new Contact { ContactId = 5, Name = "Edwin Encarnacion", Address = "1 Blue Jays Way", City = "Toronto", State = "ON", Zip = "M5V 1J1", Email = "edwine@bluejays.com", Twitter = "Encadwin" }, 17 | }; 18 | 19 | public Task> GetAll() 20 | { 21 | return Task.FromResult(_contacts.AsEnumerable()); 22 | } 23 | 24 | public Task Get(int id) 25 | { 26 | return Task.FromResult(_contacts.FirstOrDefault(x => x.ContactId == id)); 27 | } 28 | 29 | public Task Add(Contact contact) 30 | { 31 | var newId = (_contacts.LastOrDefault()?.ContactId ?? 0) + 1; 32 | contact.ContactId = newId; 33 | _contacts.Add(contact); 34 | return Task.FromResult(newId); 35 | } 36 | 37 | public async Task Update(Contact updatedContact) 38 | { 39 | var contact = await Get(updatedContact.ContactId).ConfigureAwait(false); 40 | if (contact == null) 41 | { 42 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", updatedContact.ContactId)); 43 | } 44 | 45 | contact.Address = updatedContact.Address; 46 | contact.City = updatedContact.City; 47 | contact.Email = updatedContact.Email; 48 | contact.Name = updatedContact.Name; 49 | contact.State = updatedContact.State; 50 | contact.Twitter = updatedContact.Twitter; 51 | contact.Zip = updatedContact.Zip; 52 | } 53 | 54 | public async Task Delete(int id) 55 | { 56 | var contact = await Get(id).ConfigureAwait(false); 57 | if (contact == null) 58 | { 59 | throw new InvalidOperationException(string.Format("Contact with id '{0}' does not exists", id)); 60 | } 61 | 62 | _contacts.Remove(contact); 63 | } 64 | } 65 | } -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/MvcCoreApiWithDocs.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | netcoreapp2.0 5 | $(NoWarn);1591 6 | true 7 | true 8 | MvcCoreApiWithDocs 9 | Exe 10 | MvcCoreApiWithDocs 11 | 12 | 13 | 14 | 15 | PreserveNewest 16 | 17 | 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | 31 | 32 | 33 | 34 | 35 | 36 | 37 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Program.cs: -------------------------------------------------------------------------------- 1 | using System.IO; 2 | using Microsoft.AspNetCore.Hosting; 3 | using Microsoft.Extensions.Logging; 4 | using Microsoft.Extensions.Configuration; 5 | 6 | namespace MvcCoreApiWithDocs 7 | { 8 | public class Program 9 | { 10 | public static void Main(string[] args) 11 | { 12 | var host = new WebHostBuilder() 13 | .UseKestrel() 14 | .UseContentRoot(Directory.GetCurrentDirectory()) 15 | .ConfigureAppConfiguration((hostingContext, config) => 16 | { 17 | config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); 18 | config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true).AddEnvironmentVariables(); 19 | }) 20 | .ConfigureLogging((hostingContext, l) => 21 | { 22 | l.AddConfiguration(hostingContext.Configuration.GetSection("Logging")); 23 | l.AddConsole(); 24 | }) 25 | .UseIISIntegration() 26 | .UseStartup() 27 | .Build(); 28 | 29 | host.Run(); 30 | } 31 | } 32 | } 33 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Properties/launchSettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "iisSettings": { 3 | "windowsAuthentication": false, 4 | "anonymousAuthentication": true, 5 | "iisExpress": { 6 | "applicationUrl": "http://localhost:28238/", 7 | "sslPort": 0 8 | } 9 | }, 10 | "profiles": { 11 | "IIS Express": { 12 | "commandName": "IISExpress", 13 | "launchBrowser": true, 14 | "launchUrl": "contacts", 15 | "environmentVariables": { 16 | "ASPNETCORE_ENVIRONMENT": "Development" 17 | } 18 | }, 19 | "AspNetCore.Sample.Api": { 20 | "commandName": "Project", 21 | "launchBrowser": true, 22 | "launchUrl": "http://localhost:5000/api/values", 23 | "environmentVariables": { 24 | "ASPNETCORE_ENVIRONMENT": "Development" 25 | } 26 | } 27 | } 28 | } -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/ScopesDefinitionOperationFilter.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using Microsoft.AspNetCore.Authorization; 3 | using Swashbuckle.AspNetCore.Swagger; 4 | using Swashbuckle.AspNetCore.SwaggerGen; 5 | using System.Linq; 6 | using Microsoft.AspNetCore.Mvc.Controllers; 7 | using Microsoft.AspNetCore.Mvc.ApiExplorer; 8 | using Microsoft.AspNetCore.Mvc.Authorization; 9 | using Microsoft.AspNetCore.Mvc.Filters; 10 | 11 | namespace MvcCoreApiWithDocs 12 | { 13 | public class ScopesDefinitionOperationFilter : IOperationFilter 14 | { 15 | private readonly Dictionary _policyToScopesMappings; 16 | 17 | public ScopesDefinitionOperationFilter(Dictionary policyToScopesMappings) 18 | { 19 | _policyToScopesMappings = policyToScopesMappings; 20 | } 21 | 22 | public void Apply(Operation operation, OperationFilterContext context) 23 | { 24 | var requiredScopes = new HashSet(); 25 | 26 | var controllerActionDescriptor = context.ApiDescription.GetProperty(); 27 | var globalPolicies = controllerActionDescriptor.FilterDescriptors.Where(x => x.Scope == FilterScope.Global). 28 | Select(x => x.Filter). 29 | OfType(). 30 | Select(x => x.AuthorizeData.FirstOrDefault().Policy); 31 | 32 | var controllerPolicies = context.ApiDescription.ControllerAttributes() 33 | .OfType() 34 | .Select(attr => attr.Policy); 35 | 36 | var actionPolicies = context.ApiDescription.ActionAttributes() 37 | .OfType() 38 | .Select(attr => attr.Policy); 39 | 40 | var allPolicies = globalPolicies.Union(actionPolicies.Union(controllerPolicies)).Distinct(); 41 | 42 | foreach (var policy in allPolicies) 43 | { 44 | if (_policyToScopesMappings.ContainsKey(policy)) 45 | { 46 | var policyScope = _policyToScopesMappings[policy]; 47 | requiredScopes.Add(policyScope); 48 | } 49 | } 50 | 51 | if (requiredScopes.Any()) 52 | { 53 | operation.Responses.Add("401", new Response { Description = "Unauthorized" }); 54 | operation.Responses.Add("403", new Response { Description = "Forbidden" }); 55 | 56 | operation.Security = new List>> 57 | { 58 | new Dictionary> 59 | { 60 | {"openid", requiredScopes} 61 | } 62 | }; 63 | } 64 | } 65 | } 66 | } -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/Startup.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | using System.IO; 3 | using Microsoft.AspNetCore.Authentication.JwtBearer; 4 | using Microsoft.AspNetCore.Authorization; 5 | using Microsoft.AspNetCore.Builder; 6 | using Microsoft.AspNetCore.Hosting; 7 | using Microsoft.AspNetCore.Mvc.Authorization; 8 | using Microsoft.Extensions.Configuration; 9 | using Microsoft.Extensions.DependencyInjection; 10 | using Microsoft.Extensions.Logging; 11 | using MvcCoreApiWithDocs.Models; 12 | using Swashbuckle.AspNetCore.Swagger; 13 | using System; 14 | using IdentityServer4.AccessTokenValidation; 15 | 16 | namespace MvcCoreApiWithDocs 17 | { 18 | public class Startup 19 | { 20 | public void ConfigureServices(IServiceCollection services) 21 | { 22 | var readPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme). 23 | RequireAuthenticatedUser(). 24 | RequireClaim("scope", "read").Build(); 25 | 26 | var writePolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme). 27 | RequireAuthenticatedUser(). 28 | RequireClaim("scope", "write").Build(); 29 | 30 | services.AddSingleton(); 31 | services.AddMvcCore(opt => 32 | { 33 | opt.Filters.Add(new AuthorizeFilter("ReadPolicy")); 34 | }).AddAuthorization(o => 35 | { 36 | o.AddPolicy("ReadPolicy", readPolicy); 37 | o.AddPolicy("WritePolicy", writePolicy); 38 | }).AddDataAnnotations(). 39 | AddJsonFormatters(). 40 | AddApiExplorer(); 41 | 42 | // set up embedded identity server 43 | services.AddIdentityServer(). 44 | AddTestClients(). 45 | AddTestResources(). 46 | AddDeveloperSigningCredential(); 47 | 48 | services 49 | .AddAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme) 50 | .AddIdentityServerAuthentication(IdentityServerAuthenticationDefaults.AuthenticationScheme, o => 51 | { 52 | o.Authority = "http://localhost:5000/openid"; 53 | o.RequireHttpsMetadata = false; 54 | }); 55 | 56 | services.AddSwaggerGen(options => { 57 | options.SwaggerDoc("v1", new Info 58 | { 59 | Title = "Contacts API", 60 | Version = "v1", 61 | Description = "Used to exchange contact information" 62 | }); 63 | 64 | options.AddSecurityDefinition("openid", new OAuth2Scheme 65 | { 66 | Type = "openid", 67 | Flow = "Client Credentials", 68 | Scopes = new Dictionary { { "read", "Read access"}, {"write", "Write access"} }, 69 | TokenUrl = "http://localhost:5000/openid/token/connect" 70 | }); 71 | 72 | options.OperationFilter(new Dictionary { { "ReadPolicy", "read" }, { "WritePolicy", "write" } }); 73 | 74 | var xmlDocs = Path.Combine(AppContext.BaseDirectory, "MvcCoreApiWithDocs.xml"); 75 | options.IncludeXmlComments(xmlDocs); 76 | }); 77 | } 78 | 79 | public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) 80 | { 81 | app.Map("/openid", id => { 82 | // use embedded identity server to issue tokens 83 | id.UseIdentityServer(); 84 | }); 85 | 86 | app.Map("/api", api => { 87 | // consume the JWT tokens in the API 88 | api.UseAuthentication(); 89 | api.UseSwagger(); 90 | api.UseMvc(); 91 | }); 92 | 93 | app.UseSwaggerUI(c => 94 | { 95 | c.SwaggerEndpoint("/api/swagger/v1/swagger.json", "V1 Docs"); 96 | }); 97 | } 98 | } 99 | } 100 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/appsettings.json: -------------------------------------------------------------------------------- 1 | { 2 | "Logging": { 3 | "IncludeScopes": false, 4 | "LogLevel": { 5 | "Default": "Debug", 6 | "System": "Information", 7 | "Microsoft": "Information" 8 | } 9 | } 10 | } 11 | -------------------------------------------------------------------------------- /06 MVC Core API with documentation/MvcCoreApiWithDocs/web.config: -------------------------------------------------------------------------------- 1 |  2 | 3 | 4 | 7 | 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019 Filip W 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /LightweightApi.sln: -------------------------------------------------------------------------------- 1 |  2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.26228.4 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightweightApi", "01 Lightweight API (no MVC)\LightweightApi\LightweightApi.csproj", "{F0F43AD5-D9CB-48E4-9C0F-84142C7A57E6}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "LightweightApiWithAuth", "02 Lightweight API with authentication\LightweightApiWithAuth\LightweightApiWithAuth.csproj", "{1569B3B2-FA3E-4032-AB12-817DEC9852F0}" 9 | EndProject 10 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcCoreApi", "03 MVC Core API\MvcCoreApi\MvcCoreApi.csproj", "{061713B6-5D96-41B5-9C10-85DBB89A87F7}" 11 | EndProject 12 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcCoreApiWithVersioning", "04 MVC Core API with versioning\MvcCoreApiWithVersioning\MvcCoreApiWithVersioning.csproj", "{1E376FD4-4151-4578-AE3A-D699461DAF5F}" 13 | EndProject 14 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcCoreApiWithAuthentication", "05 MVC Core API with authentication\MvcCoreApiWithAuthentication\MvcCoreApiWithAuthentication.csproj", "{C7BB45A4-F1E8-44BC-8985-3BF0B4ACE8A1}" 15 | EndProject 16 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "MvcCoreApiWithDocs", "06 MVC Core API with documentation\MvcCoreApiWithDocs\MvcCoreApiWithDocs.csproj", "{EFB905E0-8122-41A9-8CB1-354E11447501}" 17 | EndProject 18 | Global 19 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 20 | Debug|Any CPU = Debug|Any CPU 21 | Release|Any CPU = Release|Any CPU 22 | EndGlobalSection 23 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 24 | {F0F43AD5-D9CB-48E4-9C0F-84142C7A57E6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 25 | {F0F43AD5-D9CB-48E4-9C0F-84142C7A57E6}.Debug|Any CPU.Build.0 = Debug|Any CPU 26 | {F0F43AD5-D9CB-48E4-9C0F-84142C7A57E6}.Release|Any CPU.ActiveCfg = Release|Any CPU 27 | {F0F43AD5-D9CB-48E4-9C0F-84142C7A57E6}.Release|Any CPU.Build.0 = Release|Any CPU 28 | {1569B3B2-FA3E-4032-AB12-817DEC9852F0}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 29 | {1569B3B2-FA3E-4032-AB12-817DEC9852F0}.Debug|Any CPU.Build.0 = Debug|Any CPU 30 | {1569B3B2-FA3E-4032-AB12-817DEC9852F0}.Release|Any CPU.ActiveCfg = Release|Any CPU 31 | {1569B3B2-FA3E-4032-AB12-817DEC9852F0}.Release|Any CPU.Build.0 = Release|Any CPU 32 | {061713B6-5D96-41B5-9C10-85DBB89A87F7}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 33 | {061713B6-5D96-41B5-9C10-85DBB89A87F7}.Debug|Any CPU.Build.0 = Debug|Any CPU 34 | {061713B6-5D96-41B5-9C10-85DBB89A87F7}.Release|Any CPU.ActiveCfg = Release|Any CPU 35 | {061713B6-5D96-41B5-9C10-85DBB89A87F7}.Release|Any CPU.Build.0 = Release|Any CPU 36 | {1E376FD4-4151-4578-AE3A-D699461DAF5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 37 | {1E376FD4-4151-4578-AE3A-D699461DAF5F}.Debug|Any CPU.Build.0 = Debug|Any CPU 38 | {1E376FD4-4151-4578-AE3A-D699461DAF5F}.Release|Any CPU.ActiveCfg = Release|Any CPU 39 | {1E376FD4-4151-4578-AE3A-D699461DAF5F}.Release|Any CPU.Build.0 = Release|Any CPU 40 | {C7BB45A4-F1E8-44BC-8985-3BF0B4ACE8A1}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 41 | {C7BB45A4-F1E8-44BC-8985-3BF0B4ACE8A1}.Debug|Any CPU.Build.0 = Debug|Any CPU 42 | {C7BB45A4-F1E8-44BC-8985-3BF0B4ACE8A1}.Release|Any CPU.ActiveCfg = Release|Any CPU 43 | {C7BB45A4-F1E8-44BC-8985-3BF0B4ACE8A1}.Release|Any CPU.Build.0 = Release|Any CPU 44 | {EFB905E0-8122-41A9-8CB1-354E11447501}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 45 | {EFB905E0-8122-41A9-8CB1-354E11447501}.Debug|Any CPU.Build.0 = Debug|Any CPU 46 | {EFB905E0-8122-41A9-8CB1-354E11447501}.Release|Any CPU.ActiveCfg = Release|Any CPU 47 | {EFB905E0-8122-41A9-8CB1-354E11447501}.Release|Any CPU.Build.0 = Release|Any CPU 48 | EndGlobalSection 49 | GlobalSection(SolutionProperties) = preSolution 50 | HideSolutionNode = FALSE 51 | EndGlobalSection 52 | EndGlobal 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ASP.NET Core API Demos 2 | 3 | Demos from my talk "Building HTTP APIs with ASP.NET Core" on 31.01.2017 at Microsoft TechDays, Baden, Switzerland. 4 | --------------------------------------------------------------------------------