Skip to content

Commit 1acc4ec

Browse files
committed
Added CORS section
1 parent 98edf69 commit 1acc4ec

File tree

1 file changed

+44
-0
lines changed
  • sessions/Season-05/0505-ApiDesignAndDevelopment

1 file changed

+44
-0
lines changed

sessions/Season-05/0505-ApiDesignAndDevelopment/README.md

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,55 @@ You will need to [extend the configuration of Kestrel](https://docs.microsoft.co
8989

9090
Blog at: https://devblogs.microsoft.com/aspnet/jwt-validation-and-authorization-in-asp-net-core/
9191

92+
## Configuring CORS
93+
94+
Cross-Origin Resource Sharing (CORS) can be configured in your application with a policy using settings like the following:
95+
96+
```csharp
97+
98+
public void ConfigureServices(IServiceCollection services)
99+
{
100+
101+
...
102+
103+
services.AddCors(options =>
104+
{
105+
options.AddPolicy(name: "MyPolicy",
106+
builder =>
107+
{
108+
builder.AllowAnyOrigin();
109+
.AllowAnyHeader();
110+
});
111+
});
112+
113+
114+
}
115+
116+
```
117+
118+
and then add the CORS policy to the HTTP pipeline with:
119+
120+
```csharp
121+
app.UseRouting();
122+
123+
app.UseCors("MyPolicy");
124+
125+
app.UseAuthorization();
126+
```
127+
92128
## Connecting to a front-end framework
93129

94130
With Blazor, use `HttpClient` to request and interact with data from the server:
95131

132+
```csharp
133+
await Http.GetFromJsonAsync<WeatherForecast[]>("https://localhost:5011/WeatherForecast");
134+
```
135+
136+
We can add headers using standard `HttpClient.DefaultRequestHeaders` properties:
96137

138+
```csharp
139+
Http.DefaultRequestHeaders.Add("X-ApplicationId", "My Blazor App");
140+
```
97141

98142
## Deploying - Docker, Azure, etc
99143

0 commit comments

Comments
 (0)