Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reference for Constants.ClaimTypes

I've got IdentityServer3 running as a standalone identity server.

I have a separate MVC client that uses Cookies and OpenIdConnect for authentication. I'm trying to set up claims transformations amongst other things, and would like to reference the different claims types like so:

var givenName = id.FindFirst(Constants.ClaimTypes.GivenName);
var familyName = id.FindFirst(Constants.ClaimTypes.FamilyName);
var sub = id.FindFirst(Constants.ClaimTypes.Subject);
var roles = id.FindAll(Constants.ClaimTypes.Role);

On the IdentityServer3, I reference these using Thinktecture.IdentityServer.Core.Constants however on my MVC client I don't think I should need to reference Thinktecture.IdentityServer3 just for these string constants? Is there a client library that is recommended to be used in this case? I've tried Thinktecture.IdentityModel and some .NET references but none seem to replicate the ClaimTypes in Thinktecture.IdentityServer.Core.Constants. The best I've found is System.Security.Claims.ClaimTypes but that seems to have several missing e.g. FamilyName.

The first placed I looked was Thinktecture.IdentityModel but was surprised these aren't there.

So what's the magic reference? Or is it appropriate to load Thinktecture.IdentityServer3 just for these strings?

Thanks

EDIT: So I've found Thinktecture.IdentityModel.Client which contains a JwtClaimTypes that seems to mirror ClaimTypes. Why is this named with a Jwt prefix though?

like image 953
Ibraheem Avatar asked Jul 30 '15 11:07

Ibraheem


3 Answers

The IdentityServer ClaimType constants are just a map of the OpenID Connect standard claims.

You'd be best off making your own class for these constants, as you said there's no point pulling in the full Identity Server 3 package and I don't think they are available in any other packages...

Do note that the claims come across via JSON in the JWT as snake case. For example FamilyName will be family_name.

like image 77
Scott Brady Avatar answered Nov 14 '22 14:11

Scott Brady


You can install the Microsoft.AspNetCore.Authentication.JwtBearer package. It includes the JwtRegisteredClaimNames struct which you can use like:

using static Microsoft.IdentityModel.JsonWebTokens.JwtRegisteredClaimNames;
⋮
var userId = User.FindFirstValue(Sub);
like image 24
crgolden Avatar answered Nov 14 '22 14:11

crgolden


Agree with Scott Brady, the best way to go about this is to create your constants class. We have created a shared library for this purpose, which contains claim type constants and have used them both in the server and the client projects.

NB: Apart from 'id_token' and 'sub' claim types, you can use your custom claim types in the implementation of 'IUserService'. This gives better clarity to claim type names also as you can use specific names based on your implementation.

like image 30
BK Tarun V Dalwani Avatar answered Nov 14 '22 13:11

BK Tarun V Dalwani