diff --git a/samples/README.md b/samples/README.md index a0cd02d8fa..033737f503 100644 --- a/samples/README.md +++ b/samples/README.md @@ -1,34 +1,35 @@ -CORS Sample -=== -This sample consists of a request origin (SampleOrigin) and a request destination (SampleDestination). -Both have different domain names, to simulate a CORS request. +# CORS Sample -Modify Hosts File -Windows: +This sample consists of a request origin (SampleOrigin) and a request destination (SampleDestination). Both have different domain names, to simulate a CORS request. + +## Modify Hosts File +To run this CORS sample, modify the hosts file to register the hostnames ```destination.example.com``` and ```origin.example.com.``` +### Windows: Run a text editor (e.g. Notepad) as an Administrator. Open the hosts file on the path: "C:\Windows\System32\drivers\etc\hosts". -Linux: +### Linux: On a Terminal window, type "sudo nano /etc/hosts" and enter your admin password when prompted. In the hosts file, add the following to the bottom of the file: -127.0.0.1 destination.example.com -127.0.0.1 origin.example.com +``` 127.0.0.1 destination.example.com``` +``` 127.0.0.1 origin.example.com ``` Save the file and close it. Then clear your browser history. -Run the sample -*In a command prompt window, open the directory where you cloned the repository, and open the SampleDestination directory. Run the command: dotnet run -*Repeat the above step in the SampleOrigin directory. -*Open a browser window and go to http://origin.example.com:5001 -*Click the button to see CORS in action. - +## Run the sample The SampleOrigin application will use port 5001, and SampleDestination will use 5000. Please ensure there are no other processes using those ports before running the CORS sample. -As an example, apart from GET, HEAD and POST requests, PUT requests are allowed in the CORS policy on SampleDestination. Any others, like DELETE, OPTIONS etc. are not allowed and throw an error. -Content-Length has been added as an allowed header to the sample. Any other headers are not allowed and throw an error. -To edit the policy, please see app.UseCors() method in the Startup.cs file of SampleDestination. +* In a command prompt window, open the directory where you cloned the repository, and open the SampleDestination directory. Run the command: dotnet run +* Repeat the above step in the SampleOrigin directory. +* Open a browser window and go to ```http://origin.example.com:5001``` +* Input a method and header to create a CORS request or use one of the example buttons to see CORS in action. + +As an example, apart from ```GET```, ```HEAD``` and ```POST``` requests, ```PUT``` requests are allowed in the CORS policy on SampleDestination. Any others, like ```DELETE```, ```OPTIONS``` etc. are not allowed and throw an error. +```Cache-Control``` has been added as an allowed header to the sample. Any other headers are not allowed and throw an error. You may leave the header name and value blank. + +To edit the policy, please see ```app.UseCors()``` method in the ```Startup.cs``` file of SampleDestination. + +**If using Visual Studio to launch the request origin:** +Open Visual Studio and in the ```launchSettings.json``` file for the SampleOrigin project, change the ```launchUrl``` under SampleOrigin to ```http://origin.example.com:5001```. +Using the dropdown near the Start button, choose SampleOrigin before pressing Start to ensure that it uses Kestrel and not IIS Express. -If using Visual Studio to launch the request origin: -Open Visual Studio and in the launchSettings.json file for the SampleOrigin project, change the launchUrl under SampleOrigin to -http://origin.example.com:5001. Using the dropdown near the Start button, choose SampleOrigin before pressing Start to ensure that it uses Kestrel -and not IIS Express. diff --git a/samples/SampleDestination/Startup.cs b/samples/SampleDestination/Startup.cs index 9859aa3abb..e874697e18 100644 --- a/samples/SampleDestination/Startup.cs +++ b/samples/SampleDestination/Startup.cs @@ -2,7 +2,6 @@ // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. using Microsoft.AspNetCore.Builder; -using Microsoft.AspNetCore.Cors.Infrastructure; using Microsoft.AspNetCore.Hosting; using Microsoft.AspNetCore.Http; using Microsoft.Extensions.DependencyInjection; @@ -21,17 +20,18 @@ namespace SampleDestination { loggerFactory.AddConsole(); - app.UseCors(policy => policy - .WithOrigins("http://origin.example.com:5001") + app.UseCors(policy => policy + .WithOrigins("http://origin.example.com:5001") .WithMethods("PUT") .WithHeaders("Cache-Control")); app.Run(async context => { var responseHeaders = context.Response.Headers; + context.Response.ContentType = "text/plain"; foreach (var responseHeader in responseHeaders) { - await context.Response.WriteAsync("\n"+responseHeader.Key+": "+responseHeader.Value); + await context.Response.WriteAsync("\n" + responseHeader.Key + ": " + responseHeader.Value); } await context.Response.WriteAsync("\nStatus code of your request: " + context.Response.StatusCode.ToString()); diff --git a/samples/SampleOrigin/Program.cs b/samples/SampleOrigin/Program.cs index 1a6960a91f..34a19eccb5 100644 --- a/samples/SampleOrigin/Program.cs +++ b/samples/SampleOrigin/Program.cs @@ -20,5 +20,4 @@ namespace SampleOrigin host.Run(); } } - } diff --git a/samples/SampleOrigin/Startup.cs b/samples/SampleOrigin/Startup.cs index 01e05c1b4d..5bc5c494c5 100644 --- a/samples/SampleOrigin/Startup.cs +++ b/samples/SampleOrigin/Startup.cs @@ -18,19 +18,13 @@ namespace SampleOrigin public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); - app.Run( context => - { - var fileInfoProvider = env.WebRootFileProvider; - var fileInfo = fileInfoProvider.GetFileInfo("/Index.html"); - context.Response.Headers.Add("Content-Type", "text/html; charset=utf-8"); - return context.Response.SendFileAsync(fileInfo); - }); - - app.Run(async context => - { - await context.Response.WriteAsync("Status code of your request: " + context.Response.StatusCode.ToString()); - }); - + app.Run(context => + { + var fileInfoProvider = env.WebRootFileProvider; + var fileInfo = fileInfoProvider.GetFileInfo("/Index.html"); + context.Response.ContentType = "text/html"; + return context.Response.SendFileAsync(fileInfo); + }); } } } diff --git a/samples/SampleOrigin/web.config b/samples/SampleOrigin/web.config deleted file mode 100644 index e04a0397bf..0000000000 --- a/samples/SampleOrigin/web.config +++ /dev/null @@ -1,9 +0,0 @@ - - - - - - - - - \ No newline at end of file diff --git a/samples/SampleOrigin/wwwroot/Index.html b/samples/SampleOrigin/wwwroot/Index.html index 6529ddb6ea..97d5cbd126 100644 --- a/samples/SampleOrigin/wwwroot/Index.html +++ b/samples/SampleOrigin/wwwroot/Index.html @@ -41,7 +41,9 @@ var url = 'http://destination.example.com:5000/'; var request = new XMLHttpRequest(); request.open(method, url, true); - request.setRequestHeader(headerName, headerValue); + if (headerName && headerValue) { + request.setRequestHeader(headerName, headerValue); + } if (!request) { alert('CORS not supported'); @@ -61,24 +63,11 @@ request.send(); } +

CORS Sample

Method:

- Header Name: Header Value:

+ Header Name: Header Value: