|
7 | 7 | using Microsoft.Identity.Web; |
8 | 8 | using Microsoft.AspNetCore.Authentication.JwtBearer; |
9 | 9 | using System.IdentityModel.Tokens.Jwt; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using System.Linq; |
10 | 12 | using TodoListAPI.Models; |
11 | 13 | using TodoListAPI.Infrastructure; |
12 | 14 |
|
@@ -47,18 +49,20 @@ public void ConfigureServices(IServiceCollection services) |
47 | 49 | /// For more information, visit: https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validate-the-user-has-permission-to-access-this-data |
48 | 50 | /// </summary> |
49 | 51 |
|
50 | | - //options.Events.OnTokenValidated = async context => |
51 | | - //{ |
52 | | - // string[] allowedClientApps = { /* list of client ids to allow */ }; |
53 | | - |
54 | | - // string clientappId = context?.Principal?.Claims |
55 | | - // .FirstOrDefault(x => x.Type == "azp" || x.Type == "appid")?.Value; |
56 | | - |
57 | | - // if (!allowedClientApps.Contains(clientappId)) |
58 | | - // { |
59 | | - // throw new System.Exception("This client is not authorized"); |
60 | | - // } |
61 | | - //}; |
| 52 | + options.Events.OnTokenValidated = async context => |
| 53 | + { |
| 54 | + string[] allowedClientApps = { Configuration["AzureAd:ClientId"] }; // In this scenario, client and service share the same clientId |
| 55 | + |
| 56 | + string clientappId = context?.Principal?.Claims |
| 57 | + .FirstOrDefault(x => x.Type == "azp" || x.Type == "appid")?.Value; |
| 58 | + |
| 59 | + if (!allowedClientApps.Contains(clientappId)) |
| 60 | + { |
| 61 | + throw new System.Exception("This client is not authorized"); |
| 62 | + } |
| 63 | + |
| 64 | + await Task.CompletedTask; |
| 65 | + }; |
62 | 66 | }, options => { Configuration.Bind("AzureAd", options); }); |
63 | 67 |
|
64 | 68 | // The following lines code instruct the asp.net core middleware to use the data in the "roles" claim in the Authorize attribute and User.IsInrole() |
|
0 commit comments