AzureAD - Can't get userinfo into claim
- [x] I read and understood how to enable logging
Issue / Steps to reproduce the problem
I have the following config for AzureAD authentication, and the authentication part itself works perfectly:
app.UseOpenIdConnectAuthentication(new OpenIdConnectOptions
{
AuthenticationScheme = "aad",
SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme,
SignOutScheme = IdentityServerConstants.SignoutScheme,
DisplayName = "Azure Active Directory",
Authority = "https://login.microsoftonline.com/x",
ClientId = "x",
ClientSecret = "x",
ResponseType = OpenIdConnectResponseType.CodeIdToken,
Scope = { "openid email profile offline_access" },
CallbackPath = new PathString("/signin-aad"),
SignedOutCallbackPath = new PathString("/signout-callback-aad"),
RemoteSignOutPath = new PathString("/signout-aad"),
TokenValidationParameters = new TokenValidationParameters
{
NameClaimType = "name",
RoleClaimType = "role"
}
});
I have the following config:
using IdentityServer4;
using IdentityServer4.Models;
using System.Collections.Generic;
namespace IdentityServer
{
public class Config
{
// scopes define the resources in your system
public static IEnumerable<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResources.Profile(),
new IdentityResources.Email()
};
}
public static IEnumerable<ApiResource> GetApiResources()
{
return new List<ApiResource>
{
new ApiResource("api")
{
ApiSecrets =
{
new Secret("secret".Sha256())
}
}
};
}
// clients want to access resources (aka scopes)
public static IEnumerable<Client> GetClients()
{
// client credentials client
return new List<Client>
{
// OpenID Connect implicit flow client (MVC)
new Client
{
ClientId = "dashboard",
ClientName = "dashboard",
AllowedGrantTypes = GrantTypes.Implicit,
AllowAccessTokensViaBrowser = true,
RedirectUris = { "http://localhost:3000" },
PostLogoutRedirectUris = { "http://localhost:3000" },
AllowedCorsOrigins = { "http://localhost:3000" },
RequireConsent = false,
EnableLocalLogin = false,
AllowedScopes =
{
IdentityServerConstants.StandardScopes.OpenId,
IdentityServerConstants.StandardScopes.Profile,
IdentityServerConstants.StandardScopes.Email,
"api"
}
}
};
}
}
}
The client gets a token with this config:
let authorizationUrl = 'https://x/connect/authorize';
let client_id = 'dashboard';
let redirect_uri = 'http://localhost:3000';
let response_type = 'id_token token';
let scope = 'openid profile email api';
let nonce = 'N' + Math.random() + '' + Date.now();
let state = Date.now() + '' + Math.random();
But this is the only thing I get in the token:
amr: Array[1]
at_hash: "oCCDRt3rYRcqT7ylxOFBgw"
aud: "dashboard"
auth_time: 1486849529
exp: 1486849829iat: 1486849529
idp: "aad"
iss: "https://x"
nbf: 1486849529
nonce: "N0.203362378446941381486849520370"
sid: "96a3acb933b2cdcf8c5fca84f5be83cf"
sub: "6fb95502af6dd720d46073075bc4ab0e413f69f8b7c63f3d9fd88802b0297d3d"
For some reason I can't get the claims like email and name into the token. The /connect/userinfo endpoint only shows sub.
Am I doing it wrong?
and you get those claims back from AAD? And how do you store them so that IdentityServer can deliver them back to your client?
@leastprivilege I read from this doc: https://github.com/IdentityServer/IdentityServer4/blob/dev/docs/quickstarts/4_external_authentication.rst
The following sentence:
The quickstart UI auto-provisions external users. IOW - if an external user logs in for the first time, a new local user is created, all the external claims are copied over and associated with the new user.
So I assume that when I use AzureAD the external claims are automatically copied over.
From your reaction I sense that I need to manually copy over the claims I need? Are there any docs available for that?
I do receive additional claims from Azure as you can see here:

The quick start UI is just a sample - if you want to see what's going on, put a breakpoint into the ExternalLoginCallback method on the account controller - another interesting breakpoint would be your profile service' GetProfileDataAsync method.
Any update on the issue? closing for now - feel free to re-open if it needs further discussion.
I wonder if this could not be re-opened. I am having this exact issue. Only a sub-set of the claims returned from AAD get passed on, in this case I am specifically looking for the email address. I am forcing the addition of the claim in the ExternalLoginCallBack to the claims. The claim type returned by AAD is http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress. I am adding a standard type of email to the claims collection that has the same value but neither of the 2 claims get returned to the authenticating client. I have also tried adding it to the additionalClaims list without success.
Thank you.
Returned from AAD:
{aio: AUQAu,'8GAAAACQFVVTzq8KJ2Dbrn3VZDU5qYyf80z8cCk----------------------------------S6C}
{http://schemas.microsoft.com/claims/authnmethodsreferences: pwd}
{http://schemas.xmlsoap.org/ws/2D05/05/identlty/claims/emailaddress: [email protected]}
{http://schemas.xmlsoap.org/ws/2D05/05/identity/claims/surname: Pienaar}
{http://schemas.xmlsoap.org/ws/2D05/05/identity/claims/givenname: Karl}
{http://schemas.microsoft.com/identity/claims/identityprovider: live.com}
{name: Karl Pienaar}
{http://schemas.microsoft.com/identity/claims/objectidentifier: 8e6848ba-3ga2-4c5g-6283-AAAAAAAAAAAA}
{http://schemas.microsoft.com/identity/claims/tenantid: 2a74AAAA-AAAA-486a-g2gI-SSSSgf2fc7be}
{http://schemas.xmIsoap.org/ws/2D05/05/identity/claims/name: live.com#[email protected]}
{uti: iloUmNkMXkeySSSSQIQEAA}
Received by MVC client:
{sid: fd5cgSSSSSSSSSS643cd8a37ec81c570}
{sub: e121ba35a14d75b2aSSSSSSSSSSa8ga3cgcb5dgc5d89044d8dc2861ge3d26ba}
{idp: oidc}
{family name: Pienaar}
{given_name: Karl}
{name: Karl Pienaar}
{name: live.com#[email protected]}
@karlpienaar I solved this by adding the external provider's (i.e AAD) claims in ExternalLoginCallback (like you did). I added all the claims from the external provider to additional claims (additionalClaims.AddRange(claims). I'm not sure if this is bad practice, and I might change this later, but it made them available in the next step...
Then I created a custom profile service, used it to add the claims I needed (in this case, the User Principal Name).
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentityServer()
// ...
.AddProfileService<CustomProfileService>();
}
public class CustomProfileService : IProfileService
{
public Task GetProfileDataAsync(ProfileDataRequestContext context)
{
var upnClaim = context.Subject.FindFirst(c => c.Type == ClaimTypes.Upn);
context.IssuedClaims.Add(upnClaim);
return Task.CompletedTask;
}
public Task IsActiveAsync(IsActiveContext context)
{
context.IsActive = true;
return Task.CompletedTask;
}
}
I also found if you create a custom IdentityResource you can get them added to your id_token, but I ran into an issue where it would not show up on my OWIN API when using app.UseIdentityServerBearerTokenAuthentication from IdentityServer3 even if I added my custom IdentityResource to the RequiredScopes of the options.
public static List<IdentityResource> GetIdentityResources()
{
return new List<IdentityResource>
{
new IdentityResources.OpenId(),
new IdentityResource
{
Name = "username",
DisplayName = "Username",
Required = true,
UserClaims = new[] {
ClaimTypes.Upn
}
},
new IdentityResources.Profile(),
new IdentityResources.Email(),
};
}
@leastprivilege Does this sound like the correct approach?
@techniq Thanks a ton for this. I haven't had a chance to try it yet but it certainly looks like the ticket. I'll let you know once I've had a chance to give it a go.
@techniq You sir, are a star. Works as intended. Have a beer on me 🥇
@karlpienaar glad I could help. I don't drink, but cheers 🍺
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.