Skip to content

Commit dcb9b87

Browse files
committed
1 parent 47a33dd commit dcb9b87

8 files changed

Lines changed: 112 additions & 6 deletions

File tree

docker-compose.override.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,7 @@ services:
225225
apigw:
226226
environment:
227227
- ASPNETCORE_ENVIRONMENT=Development
228+
- identityUrl=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5110.
228229
ports:
229230
- "5200:80"
230231

@@ -233,6 +234,7 @@ services:
233234
- ASPNETCORE_ENVIRONMENT=Development
234235
- urls__basket=http://basket.api
235236
- urls__catalog=http://catalog.api
237+
- urls__identity=http://identity.api #Local: You need to open your local dev-machine firewall at range 5100-5110.
236238
ports:
237239
- "5120:80"
238240

src/Apigw/OcelotApiGw/Startup.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,17 @@ public Startup(IConfiguration configuration)
2626

2727
public void ConfigureServices(IServiceCollection services)
2828
{
29+
var identityUrl = _cfg.GetValue<string>("IdentityUrl");
30+
var authenticationProviderKey = "IdentityApiKey";
31+
32+
services.AddAuthentication()
33+
.AddJwtBearer(authenticationProviderKey, x =>
34+
{
35+
x.Authority = identityUrl;
36+
x.RequireHttpsMetadata = false;
37+
x.Audience = "ocelot";
38+
});
39+
2940
services.AddOcelot(_cfg);
3041
}
3142

src/Apigw/OcelotApiGw/configuration/configuration.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@
88
"UpstreamPathTemplate": "/purchase-bff/catalog/{everything}",
99
"UpstreamHttpMethod": [ "GET" ]
1010
},
11+
{
12+
"DownstreamPathTemplate": "/{everything}",
13+
"DownstreamScheme": "http",
14+
"DownstreamHost": "purchase-bff",
15+
"DownstreamPort": 80,
16+
"UpstreamPathTemplate": "/purchase-bff/{everything}",
17+
"UpstreamHttpMethod": [],
18+
"AuthenticationOptions": {
19+
"AuthenticationProviderKey": "IdentityApiKey",
20+
"AllowedScopes": []
21+
}
22+
},
1123
{
1224
"DownstreamPathTemplate": "/{everything}",
1325
"DownstreamScheme": "http",

src/BFFs/PurchaseBff/Startup.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
using System;
22
using System.Collections.Generic;
3+
using System.IdentityModel.Tokens.Jwt;
34
using System.Linq;
45
using System.Threading.Tasks;
6+
using Microsoft.AspNetCore.Authentication.JwtBearer;
57
using Microsoft.AspNetCore.Builder;
68
using Microsoft.AspNetCore.Hosting;
79
using Microsoft.AspNetCore.Http;
@@ -73,6 +75,28 @@ public void ConfigureServices(IServiceCollection services)
7375
.AllowAnyHeader()
7476
.AllowCredentials());
7577
});
78+
79+
80+
JwtSecurityTokenHandler.DefaultInboundClaimTypeMap.Clear();
81+
var identityUrl = Configuration.GetValue<string>("urls:identity");
82+
services.AddAuthentication(options =>
83+
{
84+
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
85+
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
86+
87+
}).AddJwtBearer(options =>
88+
{
89+
options.Authority = identityUrl;
90+
options.RequireHttpsMetadata = false;
91+
options.Audience = "ocelot";
92+
options.Events = new JwtBearerEvents()
93+
{
94+
OnAuthenticationFailed = async ctx =>
95+
{
96+
int i = 0;
97+
}
98+
};
99+
});
76100
}
77101

78102
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
{
22
"urls": {
33
"basket": "http://localhost:55105",
4-
"catalog": "http://localhost:55101"
4+
"catalog": "http://localhost:55101",
5+
"identity": "http://localhost:55105"
56
}
67
}

src/Services/Basket/Basket.API/Properties/launchSettings.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:55105/",
6+
"applicationUrl": "http://localhost:55103/",
77
"sslPort": 0
88
}
99
},
@@ -19,7 +19,7 @@
1919
"Microsoft.eShopOnContainers.Services.Basket.API": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"launchUrl": "http://localhost:55105/",
22+
"launchUrl": "http://localhost:55103/",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
}

src/Services/Identity/Identity.API/Properties/launchSettings.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,23 @@
33
"windowsAuthentication": false,
44
"anonymousAuthentication": true,
55
"iisExpress": {
6-
"applicationUrl": "http://localhost:5105",
6+
"applicationUrl": "http://localhost:55105",
77
"sslPort": 0
88
}
99
},
1010
"profiles": {
1111
"IIS Express": {
1212
"commandName": "IISExpress",
1313
"launchBrowser": true,
14-
"launchUrl": "http://localhost:5105",
14+
"launchUrl": "http://localhost:55105",
1515
"environmentVariables": {
1616
"ASPNETCORE_ENVIRONMENT": "Development"
1717
}
1818
},
1919
"eShopOnContainers.Identity": {
2020
"commandName": "Project",
2121
"launchBrowser": true,
22-
"launchUrl": "http://localhost:5000",
22+
"launchUrl": "http://localhost:55105",
2323
"environmentVariables": {
2424
"ASPNETCORE_ENVIRONMENT": "Development"
2525
}
Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
using Microsoft.AspNetCore.Authentication;
2+
using Microsoft.AspNetCore.Authorization;
3+
using Microsoft.AspNetCore.Mvc;
4+
using Microsoft.eShopOnContainers.BuildingBlocks.Resilience.Http;
5+
using Microsoft.eShopOnContainers.WebMVC.Services;
6+
using Microsoft.eShopOnContainers.WebMVC.ViewModels;
7+
using System;
8+
using System.Collections.Generic;
9+
using System.Linq;
10+
using System.Threading.Tasks;
11+
12+
namespace WebMVC.Controllers
13+
{
14+
class TestPayload
15+
{
16+
public int CatalogItemId { get; set; }
17+
public string BasketId { get; set; }
18+
19+
public int Quantity { get; set; }
20+
}
21+
22+
[Authorize]
23+
public class TestController : Controller
24+
{
25+
private readonly IHttpClient _client;
26+
private readonly IIdentityParser<ApplicationUser> _appUserParser;
27+
public TestController(IHttpClient client, IIdentityParser<ApplicationUser> identityParser)
28+
{
29+
_client = client;
30+
_appUserParser = identityParser;
31+
}
32+
33+
public async Task<IActionResult> Ocelot()
34+
{
35+
var url = "http://apigw/purchase-bff/api/v1/basket/items";
36+
var payload = new TestPayload()
37+
{
38+
CatalogItemId = 1,
39+
Quantity = 1,
40+
BasketId = _appUserParser.Parse(User).Id
41+
};
42+
var token = await HttpContext.GetTokenAsync("access_token");
43+
var response = await _client.PostAsync<TestPayload>(url, payload, token);
44+
45+
if (response.IsSuccessStatusCode)
46+
{
47+
var str = await response.Content.ReadAsStringAsync();
48+
return Ok(str);
49+
}
50+
else
51+
{
52+
return Ok(new { response.StatusCode, response.ReasonPhrase });
53+
}
54+
}
55+
}
56+
}

0 commit comments

Comments
 (0)