1
- using Microsoft . Identity . Client ;
1
+ /************************************************************************************************
2
+ The MIT License (MIT)
3
+
4
+ Copyright (c) 2015 Microsoft Corporation
5
+
6
+ Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ of this software and associated documentation files (the "Software"), to deal
8
+ in the Software without restriction, including without limitation the rights
9
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ copies of the Software, and to permit persons to whom the Software is
11
+ furnished to do so, subject to the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be included in all
14
+ copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ SOFTWARE.
23
+ ***********************************************************************************************/
24
+
25
+ using Microsoft . Identity . Client ;
2
26
using System . Security . Claims ;
3
27
4
28
namespace Microsoft . Identity . Web
5
29
{
6
30
public static class ClaimsPrincipalExtension
7
31
{
8
32
/// <summary>
9
- /// Get the Account identifier for an MSAL.NET account from a ClaimsPrincipal
33
+ /// Gets the Account identifier for an MSAL.NET account from a <see cref=" ClaimsPrincipal"/>
10
34
/// </summary>
11
35
/// <param name="claimsPrincipal">Claims principal</param>
12
36
/// <returns>A string corresponding to an account identifier as defined in <see cref="Microsoft.Identity.Client.AccountId.Identifier"/></returns>
13
37
public static string GetMsalAccountId ( this ClaimsPrincipal claimsPrincipal )
14
38
{
15
39
string userObjectId = GetObjectId ( claimsPrincipal ) ;
16
40
string tenantId = GetTenantId ( claimsPrincipal ) ;
17
-
18
41
if ( ! string . IsNullOrWhiteSpace ( userObjectId ) && ! string . IsNullOrWhiteSpace ( tenantId ) )
19
42
{
20
43
return $ "{ userObjectId } .{ tenantId } ";
@@ -24,41 +47,37 @@ public static string GetMsalAccountId(this ClaimsPrincipal claimsPrincipal)
24
47
}
25
48
26
49
/// <summary>
27
- /// Get the unique object ID associated with the claimsPrincipal
50
+ /// Gets the unique object ID associated with the <see cref="ClaimsPrincipal"/>
28
51
/// </summary>
29
- /// <param name="claimsPrincipal">Claims principal from which to retrieve the unique object id</param>
52
+ /// <param name="claimsPrincipal">the <see cref="ClaimsPrincipal"/> from which to retrieve the unique object id</param>
30
53
/// <returns>Unique object ID of the identity, or <c>null</c> if it cannot be found</returns>
31
54
public static string GetObjectId ( this ClaimsPrincipal claimsPrincipal )
32
- {
33
- string userObjectId = claimsPrincipal . FindFirstValue ( ClaimConstants . ObjectId ) ;
55
+ {
56
+ string userObjectId = claimsPrincipal . FindFirstValue ( ClaimConstants . Oid ) ;
34
57
if ( string . IsNullOrEmpty ( userObjectId ) )
35
- {
36
- userObjectId = claimsPrincipal . FindFirstValue ( "oid" ) ;
37
- }
58
+ userObjectId = claimsPrincipal . FindFirstValue ( ClaimConstants . ObjectId ) ;
38
59
39
60
return userObjectId ;
40
61
}
41
62
42
63
/// <summary>
43
- /// Tenant ID of the identity
64
+ /// Gets the Tenant ID associated with the <see cref="ClaimsPrincipal"/>
44
65
/// </summary>
45
- /// <param name="claimsPrincipal">Claims principal from which to retrieve the tenant id</param>
66
+ /// <param name="claimsPrincipal">the <see cref="ClaimsPrincipal"/> from which to retrieve the tenant id</param>
46
67
/// <returns>Tenant ID of the identity, or <c>null</c> if it cannot be found</returns>
47
68
public static string GetTenantId ( this ClaimsPrincipal claimsPrincipal )
48
69
{
49
- string tenantId = claimsPrincipal . FindFirstValue ( ClaimConstants . TenantId ) ;
70
+ string tenantId = claimsPrincipal . FindFirstValue ( ClaimConstants . Tid ) ;
50
71
if ( string . IsNullOrEmpty ( tenantId ) )
51
- {
52
- tenantId = claimsPrincipal . FindFirstValue ( "tid" ) ;
53
- }
72
+ tenantId = claimsPrincipal . FindFirstValue ( ClaimConstants . TenantId ) ;
54
73
55
74
return tenantId ;
56
75
}
57
76
58
77
/// <summary>
59
- /// Gets the login-hint associated with an identity
78
+ /// Gets the login-hint associated with a <see cref="ClaimsPrincipal"/>
60
79
/// </summary>
61
- /// <param name="claimsPrincipal">Identity for which to compte the login-hint</param>
80
+ /// <param name="claimsPrincipal">Identity for which to complete the login-hint</param>
62
81
/// <returns>login-hint for the identity, or <c>null</c> if it cannot be found</returns>
63
82
public static string GetLoginHint ( this ClaimsPrincipal claimsPrincipal )
64
83
{
@@ -76,13 +95,11 @@ public static string GetDomainHint(this ClaimsPrincipal claimsPrincipal)
76
95
const string msaTenantId = "9188040d-6c67-4c5b-b112-36a304b66dad" ;
77
96
78
97
var tenantId = GetTenantId ( claimsPrincipal ) ;
79
- string domainHint = string . IsNullOrWhiteSpace ( tenantId ) ? null :
80
- tenantId == msaTenantId ? "consumers" : "organizations" ;
81
- return domainHint ;
98
+ return string . IsNullOrWhiteSpace ( tenantId ) ? null : tenantId == msaTenantId ? "consumers" : "organizations" ;
82
99
}
83
100
84
101
/// <summary>
85
- /// Get the display name for the signed-in user, based on their claims principal
102
+ /// Get the display name for the signed-in user, from the <see cref="ClaimsPrincipal"/>
86
103
/// </summary>
87
104
/// <param name="claimsPrincipal">Claims about the user/account</param>
88
105
/// <returns>A string containing the display name for the user, as brought by Azure AD v1.0 and v2.0 tokens,
@@ -91,7 +108,7 @@ public static string GetDomainHint(this ClaimsPrincipal claimsPrincipal)
91
108
public static string GetDisplayName ( this ClaimsPrincipal claimsPrincipal )
92
109
{
93
110
// Attempting the claims brought by an Azure AD v2.0 token first
94
- string displayName = claimsPrincipal . FindFirstValue ( "preferred_username" ) ;
111
+ string displayName = claimsPrincipal . FindFirstValue ( ClaimConstants . PreferredUserName ) ;
95
112
96
113
// Otherwise falling back to the claims brought by an Azure AD v1.0 token
97
114
if ( string . IsNullOrWhiteSpace ( displayName ) )
@@ -102,8 +119,9 @@ public static string GetDisplayName(this ClaimsPrincipal claimsPrincipal)
102
119
// Finally falling back to name
103
120
if ( string . IsNullOrWhiteSpace ( displayName ) )
104
121
{
105
- displayName = claimsPrincipal . FindFirstValue ( "name" ) ;
122
+ displayName = claimsPrincipal . FindFirstValue ( ClaimConstants . Name ) ;
106
123
}
124
+
107
125
return displayName ;
108
126
}
109
127
@@ -131,32 +149,35 @@ public static string GetDisplayName(this ClaimsPrincipal claimsPrincipal)
131
149
/// </example>
132
150
public static ClaimsPrincipal FromTenantIdAndObjectId ( string tenantId , string objectId )
133
151
{
134
- var tidClaim = new Claim ( "tid" , tenantId ) ;
135
- var oidClaim = new Claim ( "oid" , objectId ) ;
136
- var claimsIdentity = new ClaimsIdentity ( ) ;
137
- claimsIdentity . AddClaims ( new Claim [ ] { oidClaim , tidClaim } ) ;
138
- var principal = new ClaimsPrincipal ( ) ;
139
- principal . AddIdentity ( claimsIdentity ) ;
140
- return principal ;
152
+ return new ClaimsPrincipal (
153
+ new ClaimsIdentity ( new Claim [ ]
154
+ {
155
+ new Claim ( ClaimConstants . Tid , tenantId ) ,
156
+ new Claim ( ClaimConstants . Oid , objectId )
157
+ } )
158
+ ) ;
141
159
}
142
160
143
161
/// <summary>
144
- /// Builds a ClaimsPrincipal from an IAccount
162
+ /// Creates the <see cref=" ClaimsPrincipal"/> from the values found in an <see cref=" IAccount"/>
145
163
/// </summary>
146
- /// <param name="account">The IAccount instance. </param>
147
- /// <returns>A ClaimsPrincipal built from IAccount</returns>
164
+ /// <param name="account">The IAccount instance</param>
165
+ /// <returns>A <see cref=" ClaimsPrincipal"/> built from IAccount</returns>
148
166
public static ClaimsPrincipal ToClaimsPrincipal ( this IAccount account )
149
167
{
150
168
if ( account != null )
151
169
{
152
- var identity = new ClaimsIdentity ( ) ;
153
- identity . AddClaim ( new Claim ( ClaimConstants . ObjectId , account . HomeAccountId . ObjectId ) ) ;
154
- identity . AddClaim ( new Claim ( ClaimConstants . TenantId , account . HomeAccountId . TenantId ) ) ;
155
- identity . AddClaim ( new Claim ( ClaimTypes . Upn , account . Username ) ) ;
156
- return new ClaimsPrincipal ( identity ) ;
170
+ return new ClaimsPrincipal (
171
+ new ClaimsIdentity ( new Claim [ ]
172
+ {
173
+ new Claim ( ClaimConstants . Oid , account . HomeAccountId . ObjectId ) ,
174
+ new Claim ( ClaimConstants . Tid , account . HomeAccountId . TenantId ) ,
175
+ new Claim ( ClaimTypes . Upn , account . Username )
176
+ } )
177
+ ) ;
157
178
}
158
179
159
180
return null ;
160
181
}
161
182
}
162
- }
183
+ }
0 commit comments