Skip to content

Commit b8c66af

Browse files
committed
Add Authorization post
1 parent 94d118e commit b8c66af

1 file changed

Lines changed: 81 additions & 0 deletions

File tree

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
# Layout
3+
layout: post
4+
title: "Introducing ASP.NET Core Authorization support"
5+
date: 2022-10-28 13:00:00 -0800
6+
categories: release
7+
# Author
8+
author: Guillaume Delahaye (https://github.com/g7ed6e)
9+
---
10+
#### Introduction
11+
Next release of CoreWCF will bring support of ASP.NET Core Authorization to allow developers to use ASP.NET Core builtin authentication middleware such as the `Microsoft.AspNetCore.Authentication.JwtBearer` and apply appropriate authorization policies.
12+
13+
#### Builtin attributes support
14+
When working with ASP.NET Core MVC usually developers use `[Authorize]` and `[AllowAnonymous]` to decorate actions that require specific authorizations.
15+
#### Authorize support
16+
To enable a seamless developer experience we brought the ability to decorate `OperationContract` implementation with the ASP.NET Core Authorize attribute. However we introduced the below limitations to suggest developers to embrace the flexible [Policy-based](https://learn.microsoft.com/en-us/aspnet/core/security/authorization/policies?view=aspnetcore-6.0) model based on `IAuthorizationRequirement`.
17+
- `AuthenticationSchemes` property is not supported and will trigger a build warning `COREWCF_02XX`.
18+
- `Roles` property is not supported and will trigger a build warning `COREWCF_02XX`.
19+
20+
We did not bring support of `[Authorize]` attribute at `ServiceContract` implementation level.
21+
#### AllowAnonymous support
22+
We did not bring support of the `[AllowAnonymous]` attribute as we believe that a strong interface segregation between anonymous and secured operations should be set. Moreover supporting this attribute would imply delaying the authentication step in the pipeline leading to potential DDoS vulnerabilities. Decorating an `OperationContract` implementation with `[AllowAnonymous]` will have no effect and will trigger a build warning `COREWCF_02XX`.
23+
#### Configuration
24+
To setup this feature in your CoreWCF application you should follow the below steps. I will assume there that we want to enforce clients are authenticating using a JWT Bearer token issued by an authorization server `https://authorization-server-uri`, the service should be protected by the audience `my-audience` and two policies should be defined, one requiring a scope `read` and another one requiring a scope `write`.
25+
1. Register and configure JWT Bearer authentication middleware as default `AuthenticationScheme`.
26+
```csharp
27+
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
28+
.AddJwtBearer(options =>
29+
{
30+
options.Authority = "https://authorization-server-uri";
31+
options.Audience = "my-audience";
32+
});
33+
```
34+
2. Register authorization policies
35+
```csharp
36+
services.AddAuthorization(options =>
37+
{
38+
options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireClaim("scope", "read").Build();
39+
options.AddPolicy("WritePolicy", new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireClaim("scope", "write").Build());
40+
})
41+
```
42+
3. Configure your service to use ASP.NET Core Authentication and Authorization middlewares.
43+
```csharp
44+
app.UseServiceModel(builder =>
45+
{
46+
builder.AddService<SecuredService>();
47+
builder.AddServiceEndpoint<SecuredService, ISecuredService>(new BasicHttpBinding
48+
{
49+
Security = new BasicHttpSecurity
50+
{
51+
Mode = BasicHttpSecurityMode.Transport,
52+
Transport = new HttpTransportSecurity
53+
{
54+
ClientCredentialType = HttpClientCredentialType.InheritedFromHost
55+
}
56+
}
57+
}, "/BasicWcfService/basichttp.svc");
58+
}
59+
```
60+
4. Decorate your service implementation
61+
```csharp
62+
[ServiceContract]
63+
public interface ISecuredService
64+
{
65+
[OperationContract]
66+
string ReadOperation();
67+
[OperationContract]
68+
void WriteOperation(string value);
69+
}
70+
71+
public class SecuredService : ISecuredService
72+
{
73+
[Authorize]
74+
public string ReadOperation() => "Hello world";
75+
76+
[Authorize(Policy = "WritePolicy")]
77+
public void WriteOperation(string value) { }
78+
}
79+
```
80+
#### Conclusion
81+
CoreWCF brings companies a path to extend the life expectancy of legacy WCF services supporting more up to data security standards and programming patterns well known from developers.

0 commit comments

Comments
 (0)