Skip to content

Commit 3695ae9

Browse files
committed
check for client app id
1 parent f8db575 commit 3695ae9

File tree

1 file changed

+16
-12
lines changed
  • 5-AccessControl/1-call-api-roles/API/TodoListAPI

1 file changed

+16
-12
lines changed

5-AccessControl/1-call-api-roles/API/TodoListAPI/Startup.cs

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
using Microsoft.Identity.Web;
88
using Microsoft.AspNetCore.Authentication.JwtBearer;
99
using System.IdentityModel.Tokens.Jwt;
10+
using System.Threading.Tasks;
11+
using System.Linq;
1012
using TodoListAPI.Models;
1113
using TodoListAPI.Infrastructure;
1214

@@ -47,18 +49,20 @@ public void ConfigureServices(IServiceCollection services)
4749
/// For more information, visit: https://docs.microsoft.com/azure/active-directory/develop/access-tokens#validate-the-user-has-permission-to-access-this-data
4850
/// </summary>
4951

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+
};
6266
}, options => { Configuration.Bind("AzureAd", options); });
6367

6468
// 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

Comments
 (0)