Adding CORS to the sample
This commit is contained in:
parent
273ad22337
commit
1eb38e5708
|
|
@ -2,6 +2,7 @@
|
|||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.AspNet.Cors.Core;
|
||||
using Microsoft.AspNet.Mvc;
|
||||
using Microsoft.AspNet.Mvc.Rendering;
|
||||
using Microsoft.AspNet.SignalR;
|
||||
|
|
@ -196,6 +197,7 @@ namespace MusicStore.Areas.Admin.Controllers
|
|||
// Note: Added for automated testing purpose. Application does not use this.
|
||||
[HttpGet]
|
||||
[SkipStatusCodePages]
|
||||
[EnableCors("CorsPolicy")]
|
||||
public async Task<IActionResult> GetAlbumIdFromName(string albumName)
|
||||
{
|
||||
var album = await DbContext.Albums.Where(a => a.Title == albumName).FirstOrDefaultAsync();
|
||||
|
|
|
|||
|
|
@ -76,6 +76,14 @@ namespace MusicStore
|
|||
options.ClientSecret = "GaMQ2hCnqAC6EcDLnXsAeBVIJOLmeutL";
|
||||
});
|
||||
|
||||
services.ConfigureCors(options =>
|
||||
{
|
||||
options.AddPolicy("CorsPolicy", builder =>
|
||||
{
|
||||
builder.WithOrigins("http://example.com");
|
||||
});
|
||||
});
|
||||
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc();
|
||||
|
||||
|
|
|
|||
|
|
@ -50,6 +50,14 @@ namespace MusicStore
|
|||
.AddDbContext<MusicStoreContext>(options =>
|
||||
options.UseSqlServer(Configuration.Get("Data:DefaultConnection:ConnectionString")));
|
||||
|
||||
services.ConfigureCors(options =>
|
||||
{
|
||||
options.AddPolicy("CorsPolicy", builder =>
|
||||
{
|
||||
builder.WithOrigins("http://example.com");
|
||||
});
|
||||
});
|
||||
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc();
|
||||
|
||||
|
|
|
|||
|
|
@ -70,6 +70,14 @@ namespace MusicStore
|
|||
options.ClientId = "[ClientId]";
|
||||
});
|
||||
|
||||
services.ConfigureCors(options =>
|
||||
{
|
||||
options.AddPolicy("CorsPolicy", builder =>
|
||||
{
|
||||
builder.WithOrigins("http://example.com");
|
||||
});
|
||||
});
|
||||
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc();
|
||||
|
||||
|
|
|
|||
|
|
@ -13,6 +13,9 @@
|
|||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
<ProjectExtensions>
|
||||
<VisualStudio>
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Net;
|
||||
using System.Net.Http;
|
||||
using System.Threading;
|
||||
|
|
@ -346,9 +347,21 @@ namespace E2ETests
|
|||
|
||||
private string FetchAlbumIdFromName(string albumName)
|
||||
{
|
||||
// Run some CORS validation.
|
||||
_logger.LogInformation("Fetching the album id of '{album}'", albumName);
|
||||
_httpClient.DefaultRequestHeaders.Add("Origin", "http://notpermitteddomain.com");
|
||||
var response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result;
|
||||
ThrowIfResponseStatusNotOk(response);
|
||||
IEnumerable<string> values;
|
||||
Assert.False(response.Headers.TryGetValues("Access-Control-Allow-Origin", out values));
|
||||
|
||||
_httpClient.DefaultRequestHeaders.Remove("Origin");
|
||||
_httpClient.DefaultRequestHeaders.Add("Origin", "http://example.com");
|
||||
response = _httpClient.GetAsync(string.Format("Admin/StoreManager/GetAlbumIdFromName?albumName={0}", albumName)).Result;
|
||||
ThrowIfResponseStatusNotOk(response);
|
||||
Assert.Equal("http://example.com", response.Headers.GetValues("Access-Control-Allow-Origin").First());
|
||||
_httpClient.DefaultRequestHeaders.Remove("Origin");
|
||||
|
||||
var albumId = response.Content.ReadAsStringAsync().Result;
|
||||
_logger.LogInformation("Album id for album '{album}' is '{id}'", albumName, albumId);
|
||||
return albumId;
|
||||
|
|
@ -454,4 +467,4 @@ namespace E2ETests
|
|||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -76,6 +76,14 @@ namespace MusicStore
|
|||
};
|
||||
});
|
||||
|
||||
services.ConfigureCors(options =>
|
||||
{
|
||||
options.AddPolicy("CorsPolicy", builder =>
|
||||
{
|
||||
builder.WithOrigins("http://example.com");
|
||||
});
|
||||
});
|
||||
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc();
|
||||
|
||||
|
|
|
|||
|
|
@ -137,6 +137,14 @@ namespace MusicStore
|
|||
options.Scope.Add("wl.signin");
|
||||
});
|
||||
|
||||
services.ConfigureCors(options =>
|
||||
{
|
||||
options.AddPolicy("CorsPolicy", builder =>
|
||||
{
|
||||
builder.WithOrigins("http://example.com");
|
||||
});
|
||||
});
|
||||
|
||||
// Add MVC services to the services container
|
||||
services.AddMvc();
|
||||
|
||||
|
|
|
|||
|
|
@ -4,7 +4,6 @@
|
|||
<VisualStudioVersion Condition="'$(VisualStudioVersion)' == ''">14.0</VisualStudioVersion>
|
||||
<VSToolsPath Condition="'$(VSToolsPath)' == ''">$(MSBuildExtensionsPath32)\Microsoft\VisualStudio\v$(VisualStudioVersion)</VSToolsPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.Props" Condition="'$(VSToolsPath)' != ''" />
|
||||
<PropertyGroup Label="Globals">
|
||||
<ProjectGuid>9d3326c4-1f12-4526-9f25-712a1463b3fa</ProjectGuid>
|
||||
|
|
@ -12,9 +11,11 @@
|
|||
<BaseIntermediateOutputPath Condition="'$(BaseIntermediateOutputPath)'=='' ">..\..\artifacts\obj\$(MSBuildProjectName)</BaseIntermediateOutputPath>
|
||||
<OutputPath Condition="'$(OutputPath)'=='' ">..\..\artifacts\bin\$(MSBuildProjectName)\</OutputPath>
|
||||
</PropertyGroup>
|
||||
|
||||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
|
|
@ -14,5 +14,8 @@
|
|||
<PropertyGroup>
|
||||
<SchemaVersion>2.0</SchemaVersion>
|
||||
</PropertyGroup>
|
||||
<ItemGroup>
|
||||
<Service Include="{82a7f48d-3b50-4b1e-b82e-3ada8210c358}" />
|
||||
</ItemGroup>
|
||||
<Import Project="$(VSToolsPath)\AspNet\Microsoft.Web.AspNet.targets" Condition="'$(VSToolsPath)' != ''" />
|
||||
</Project>
|
||||
Loading…
Reference in New Issue