|
| 1 | +--- |
| 2 | +# Layout |
| 3 | +layout: post |
| 4 | +title: "Introducing ASP.NET Core Authorization support and modernization of legacy WCF Authentication and Authorization APis" |
| 5 | +date: 2022-12-07 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_0201`. |
| 18 | +- `Roles` property is not supported and will trigger a build warning `COREWCF_0202`. |
| 19 | + |
| 20 | +#### AllowAnonymous support |
| 21 | +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_0200`. |
| 22 | +### Configuration |
| 23 | +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`. |
| 24 | +1. Register authentication infrastructure services and configure JWT Bearer authentication middleware as default `AuthenticationScheme`. (Internally CoreWCF is calling `HttpContext.AuthenticateAsync()` with the default registered authentication scheme). |
| 25 | +```csharp |
| 26 | +services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme) |
| 27 | +.AddJwtBearer(options => |
| 28 | +{ |
| 29 | + options.Authority = "https://authorization-server-uri"; |
| 30 | + options.Audience = "my-audience"; |
| 31 | +}); |
| 32 | +``` |
| 33 | +2. Register authorization infrastructure services and policies. |
| 34 | +```csharp |
| 35 | +services.AddAuthorization(options => |
| 36 | +{ |
| 37 | + options.DefaultPolicy = new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireClaim("scope", "read").Build(); |
| 38 | + options.AddPolicy("WritePolicy", new AuthorizationPolicyBuilder(JwtBearerDefaults.AuthenticationScheme).RequireClaim("scope", "write").Build()); |
| 39 | +}) |
| 40 | +``` |
| 41 | +3. Configure your service to use ASP.NET Core Authentication and Authorization middlewares setting the `ClientCredentialType` to `HttpClientCredentialType.InheritedFromHost`. |
| 42 | +```csharp |
| 43 | +app.UseServiceModel(builder => |
| 44 | +{ |
| 45 | + builder.AddService<SecuredService>(); |
| 46 | + builder.AddServiceEndpoint<SecuredService, ISecuredService>(new BasicHttpBinding |
| 47 | + { |
| 48 | + Security = new BasicHttpSecurity |
| 49 | + { |
| 50 | + Mode = BasicHttpSecurityMode.Transport, |
| 51 | + Transport = new HttpTransportSecurity |
| 52 | + { |
| 53 | + ClientCredentialType = HttpClientCredentialType.InheritedFromHost |
| 54 | + } |
| 55 | + } |
| 56 | + }, "/BasicWcfService/basichttp.svc"); |
| 57 | +} |
| 58 | +``` |
| 59 | +4. Decorate your service implementation |
| 60 | +```csharp |
| 61 | +[ServiceContract] |
| 62 | +public interface ISecuredService |
| 63 | +{ |
| 64 | + [OperationContract] |
| 65 | + string ReadOperation(); |
| 66 | + [OperationContract] |
| 67 | + void WriteOperation(string value); |
| 68 | +} |
| 69 | + |
| 70 | +public class SecuredService : ISecuredService |
| 71 | +{ |
| 72 | + [Authorize] |
| 73 | + public string ReadOperation() => "Hello world"; |
| 74 | + |
| 75 | + [Authorize(Policy = "WritePolicy")] |
| 76 | + public void WriteOperation(string value) { } |
| 77 | +} |
| 78 | +``` |
| 79 | +### Supported bindings |
| 80 | + |
| 81 | +ASP.NET Core Authorization policies support is implemented in http based bindings: |
| 82 | +- `BasicHttpBinding` |
| 83 | +- `WSHttpBinding` |
| 84 | +- `WebHttpBinding` |
| 85 | + |
| 86 | +### Authorization evaluation position in CoreWCF request pipeline |
| 87 | + |
| 88 | +There's an important difference regarding the "when" authorization evaluation occurs between `ServiceAuthorizationManager` usage and the ASP.NET Core Authorization usage. |
| 89 | + |
| 90 | +When using ASP.NET Core Authorization, ths below steps will be executed **before** authorization which didn't when using `ServiceAuthorizationManager`. |
| 91 | + |
| 92 | +- When setup, dynamic quota throttle acquisition. |
| 93 | +- Calls to registered `IDispatchMessageInspector.AfterReceiveRequest` |
| 94 | +- Concurrency lock acquisition |
| 95 | + |
| 96 | +Another impact is that authorization will now run on a captured `SynchronizationContext`. This point can impact CoreWCF services hosted in a UI thread (WPF or WinForms app). |
| 97 | + |
| 98 | +### Exclusiveness of ASP.NET Core Authorization policies and `ServiceAuthorizationManager` |
| 99 | + |
| 100 | +Having `ClientCredentialType` set to `InheritedFromHost` disable the execution of an authorization logic implemented in `ServiceAuthorizationManager`. |
| 101 | + |
| 102 | +### `ServiceAuthenticationManager` and `ServiceAuthorizationManager` API modernization |
| 103 | + |
| 104 | +Both implementations now support asynchronous implementations. Existing synchronous implementations will still be compatible but have been deprecated and will trigger a build warning. |
| 105 | + |
| 106 | +### Conclusion |
| 107 | +CoreWCF provides flexibility around authentication and authorization allowing implementation of more up to date security standards and programming patterns well known from developers. |
0 commit comments