Skip to content

Commit 46a189d

Browse files
add clarity on how to access role
1 parent d7088dd commit 46a189d

File tree

2 files changed

+37
-38
lines changed

2 files changed

+37
-38
lines changed

articles/active-directory/external-identities/customers/tutorial-desktop-maui-role-based-access-control.md

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -37,24 +37,24 @@ In this tutorial series, you created a .NET MAUI app where you developed the [_C
3737
To access the role claim, you can modify the code snippet as follows:
3838

3939
```csharp
40-
var idToken = PublicClientSingleton.Instance.MSALClientHelper.AuthResult.IdToken;
41-
var handler = new JwtSecurityTokenHandler();
42-
var token = handler.ReadJwtToken(idToken);
43-
// Get the role claim value
44-
var roleClaim = token.Claims.FirstOrDefault(c => c.Type == "roles")?.Value;
45-
46-
if (!string.IsNullOrEmpty(roleClaim))
47-
{
48-
// If the role claim exists, add it to the IdTokenClaims
49-
IdTokenClaims = new List<string> { roleClaim };
50-
}
51-
else
52-
{
53-
// If the role claim doesn't exist, add a message indicating that no role claim was found
54-
IdTokenClaims = new List<string> { "No role claim found in ID token" };
55-
}
56-
57-
Claims.ItemsSource = IdTokenClaims;
40+
var idToken = PublicClientSingleton.Instance.MSALClientHelper.AuthResult.IdToken;
41+
var handler = new JwtSecurityTokenHandler();
42+
var token = handler.ReadJwtToken(idToken);
43+
// Get the role claim value
44+
var roleClaim = token.Claims.FirstOrDefault(c => c.Type == "roles")?.Value;
45+
46+
if (!string.IsNullOrEmpty(roleClaim))
47+
{
48+
// If the role claim exists, add it to the IdTokenClaims
49+
IdTokenClaims = new List<string> { roleClaim };
50+
}
51+
else
52+
{
53+
// If the role claim doesn't exist, add a message indicating that no role claim was found
54+
IdTokenClaims = new List<string> { "No role claim found in ID token" };
55+
}
56+
57+
Claims.ItemsSource = IdTokenClaims;
5858
```
5959

6060
> [!NOTE]

articles/active-directory/external-identities/customers/tutorial-mobile-maui-role-based-access-control.md

Lines changed: 19 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,30 +37,29 @@ In this tutorial series, you created a .NET MAUI app where you developed the [_C
3737
To access the role claim, you can modify the code snippet as follows:
3838

3939
```csharp
40-
var idToken = PublicClientSingleton.Instance.MSALClientHelper.AuthResult.IdToken;
41-
var handler = new JwtSecurityTokenHandler();
42-
var token = handler.ReadJwtToken(idToken);
43-
// Get the role claim value
44-
var roleClaim = token.Claims.FirstOrDefault(c => c.Type == "roles")?.Value;
45-
46-
if (!string.IsNullOrEmpty(roleClaim))
47-
{
48-
// If the role claim exists, add it to the IdTokenClaims
49-
IdTokenClaims = new List<string> { roleClaim };
50-
}
51-
else
52-
{
53-
// If the role claim doesn't exist, add a message indicating that no role claim was found
54-
IdTokenClaims = new List<string> { "No role claim found in ID token" };
55-
}
56-
57-
Claims.ItemsSource = IdTokenClaims;
40+
var idToken = PublicClientSingleton.Instance.MSALClientHelper.AuthResult.IdToken;
41+
var handler = new JwtSecurityTokenHandler();
42+
var token = handler.ReadJwtToken(idToken);
43+
// Get the role claim value
44+
var roleClaim = token.Claims.FirstOrDefault(c => c.Type == "roles")?.Value;
45+
46+
if (!string.IsNullOrEmpty(roleClaim))
47+
{
48+
// If the role claim exists, add it to the IdTokenClaims
49+
IdTokenClaims = new List<string> { roleClaim };
50+
}
51+
else
52+
{
53+
// If the role claim doesn't exist, add a message indicating that no role claim was found
54+
IdTokenClaims = new List<string> { "No role claim found in ID token" };
55+
}
56+
57+
Claims.ItemsSource = IdTokenClaims;
5858
```
5959

60-
> [!NOTE]
60+
> [!NOTE]
6161
> To read the Id token, you must install the `System.IdentityModel.Tokens.Jwt` package.
6262
63-
6463
If you assign a user to multiple roles, the roles string contains all roles separated by a comma, such as `Orders.Manager, Store.Manager,...`. Make sure you build your application to handle the following conditions:
6564

6665
- Absence of roles claims in the token

0 commit comments

Comments
 (0)