Skip to content

Commit 90118dc

Browse files
JamesNKscottaddie
andauthored
Use CORS with gRPC-Web (dotnet#17030)
* Use CORS with gRPC-Web * Apply suggestions from code review Co-Authored-By: Scott Addie <10702007+scottaddie@users.noreply.github.com> Co-authored-by: Scott Addie <10702007+scottaddie@users.noreply.github.com>
1 parent f2b86e3 commit 90118dc

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

aspnetcore/grpc/browser.md

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ author: jamesnk
44
description: Learn how to configure gRPC services on ASP.NET Core to be callable from browser apps using gRPC-Web.
55
monikerRange: '>= aspnetcore-3.0'
66
ms.author: jamesnk
7-
ms.date: 02/10/2020
7+
ms.date: 02/16/2020
88
uid: grpc/browser
99
---
1010
# Use gRPC in browser apps
@@ -43,7 +43,19 @@ Alternatively, configure all services to support gRPC-Web by adding `services.Ad
4343

4444
[!code-csharp[](~/grpc/browser/sample/AllServicesSupportExample_Startup.cs?name=snippet_1&highlight=6,13)]
4545

46-
Some additional configuration may be required to call gRPC-Web from the browser, such as configuring ASP.NET Core to support CORS. For more information, see [support CORS](xref:security/cors).
46+
### gRPC-Web and CORS
47+
48+
Browser security prevents a web page from making requests to a different domain than the one that served the web page. This restriction applies to making gRPC-Web calls with browser apps. For example, a browser app served by `https://www.contoso.com` is blocked from calling gRPC-Web services hosted on `https://services.contoso.com`. Cross Origin Resource Sharing (CORS) can be used to relax this restriction.
49+
50+
To allow your browser app to make cross-origin gRPC-Web calls, set up [CORS in ASP.NET Core](xref:security/cors). Use the built-in CORS support, and expose gRPC-specific headers with <xref:Microsoft.AspNetCore.Cors.Infrastructure.CorsPolicyBuilder.WithExposedHeaders*>.
51+
52+
[!code-csharp[](~/grpc/browser/sample/CORS_Startup.cs?name=snippet_1&highlight=5-11,19,24)]
53+
54+
The preceding code:
55+
56+
* Calls `AddCors` to add CORS services and configures a CORS policy that exposes gRPC-specific headers.
57+
* Calls `UseCors` to add the CORS middleware after routing and before endpoints.
58+
* Specifies the `endpoints.MapGrpcService<GreeterService>()` method supports CORS with `RequiresCors`.
4759

4860
## Call gRPC-Web from the browser
4961

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#region snippet_1
2+
public void ConfigureServices(IServiceCollection services)
3+
{
4+
services.AddGrpc();
5+
6+
services.AddCors(o => o.AddPolicy("AllowAll", builder =>
7+
{
8+
builder.AllowAnyOrigin()
9+
.AllowAnyMethod()
10+
.AllowAnyHeader()
11+
.WithExposedHeaders("Grpc-Status", "Grpc-Message");
12+
}));
13+
}
14+
15+
public void Configure(IApplicationBuilder app)
16+
{
17+
app.UseRouting();
18+
19+
app.UseGrpcWeb();
20+
app.UseCors();
21+
22+
app.UseEndpoints(endpoints =>
23+
{
24+
endpoints.MapGrpcService<GreeterService>().EnableGrpcWeb()
25+
.RequireCors("AllowAll");
26+
});
27+
}
28+
#endregion

0 commit comments

Comments
 (0)