Skip to content

Commit 12508c0

Browse files
author
robertob
committed
fixing code that was causing IsInRole checks to not work
1 parent 644b73a commit 12508c0

1 file changed

Lines changed: 14 additions & 8 deletions

File tree

src/KK.AspNetCore.EasyAuthAuthentication/EasyAuthAuthenticationHandler.cs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ private AuthenticationTicket BuildIdentityFromEasyAuthRequestHeaders(Microsoft.A
9393
var name = requestHeaders["X-MS-CLIENT-PRINCIPAL-NAME"][0];
9494
this.Logger.LogDebug("payload was fetched from easyauth headers, name: {0}", name);
9595

96-
var identity = new GenericIdentity(name, "AuthenticationTypes.Federation"); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
96+
//var identity = new GenericIdentity(name, "AuthenticationTypes.Federation"); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
9797

9898
this.Logger.LogInformation("building claims from payload...");
9999

@@ -126,21 +126,24 @@ private AuthenticationTicket BuildIdentityFromEasyAuthRequestHeaders(Microsoft.A
126126

127127
this.Logger.LogInformation("Add claims to new identity");
128128

129-
identity.AddClaims(claims);
129+
//identity.AddClaims(claims);
130+
var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation"); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
131+
130132
//identity.AddClaim(new Claim("id_token", idToken)); // don't think we should be including this
131133
//identity.AddClaim(new Claim("http://schemas.microsoft.com/claims/authnclassreference", 1)); // don't think we need to add this
132134
if (!(identity.Claims as List<Claim>).Exists(claim => claim.Type == "scp")) identity.AddClaim(new Claim("scp", "user_impersonation")); // not sure why easyauth is dropping this
133135
identity.AddClaim(new Claim("provider_name", requestHeaders["X-MS-CLIENT-PRINCIPAL-IDP"][0]));
134-
var genericPrincipal = new GenericPrincipal(identity, null);
135-
return new AuthenticationTicket(genericPrincipal, EasyAuthAuthenticationDefaults.AuthenticationScheme);
136+
//var principal = new GenericPrincipal(identity, null); // maybe passing valid string[] roles for second parameter would make IsInRole work but no need just use ClaimsPrincipal
137+
var principal = new ClaimsPrincipal(identity);
138+
return new AuthenticationTicket(principal, EasyAuthAuthenticationDefaults.AuthenticationScheme);
136139
}
137140

138141
private AuthenticationTicket BuildIdentityFromEasyAuthMeJson(JObject payload)
139142
{
140143
var name = payload["user_id"].Value<string>(); // X-MS-CLIENT-PRINCIPAL-NAME
141144
this.Logger.LogDebug("payload was fetched from easyauth me json, name: {0}", name);
142145

143-
var identity = new GenericIdentity(name, "AuthenticationTypes.Federation"); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
146+
//var identity = new GenericIdentity(name, "AuthenticationTypes.Federation"); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
144147

145148
this.Logger.LogInformation("building claims from payload...");
146149

@@ -171,13 +174,16 @@ private AuthenticationTicket BuildIdentityFromEasyAuthMeJson(JObject payload)
171174

172175
this.Logger.LogInformation("Add claims to new identity");
173176

174-
identity.AddClaims(claims);
177+
//identity.AddClaims(claims);
178+
var identity = new ClaimsIdentity(claims, "AuthenticationTypes.Federation"); // setting ClaimsIdentity.AuthenticationType to value that azuread non-easyauth setups use
179+
175180
//identity.AddClaim(new Claim("id_token", idToken)); // don't think we should be including this
176181
//identity.AddClaim(new Claim("http://schemas.microsoft.com/claims/authnclassreference", 1)); // don't think we need to add this
177182
if (!(identity.Claims as List<Claim>).Exists(claim => claim.Type == "scp")) identity.AddClaim(new Claim("scp", "user_impersonation")); // not sure why easyauth is dropping this
178183
identity.AddClaim(new Claim("provider_name", payload["provider_name"].Value<string>())); // X-MS-CLIENT-PRINCIPAL-IDP
179-
var genericPrincipal = new GenericPrincipal(identity, null);
180-
return new AuthenticationTicket(genericPrincipal, EasyAuthAuthenticationDefaults.AuthenticationScheme);
184+
//var principal = new GenericPrincipal(identity, null); // maybe passing valid string[] roles for second parameter would make IsInRole work but no need just use ClaimsPrincipal
185+
var principal = new ClaimsPrincipal(identity);
186+
return new AuthenticationTicket(principal, EasyAuthAuthenticationDefaults.AuthenticationScheme);
181187
}
182188

183189
private HttpRequestMessage CreateAuthRequest(ref CookieContainer cookieContainer)

0 commit comments

Comments
 (0)