C
C#2mo ago
Fady

✅ Anyone good with ClaimsPrincipal? Trying to print the User's email

So i'm trying to set up the SSO for Google and i've managed to get a working response and i've converted it to a JWT Token. However i need to get the User's Email address so i can map it to the User in the Database. But i can't seem to figure it out.
36 Replies
Fady
Fady2mo ago
It's an ASP.NET Project and the relevant code looks like this
Fady
Fady2mo ago
BlazeBin - efjmobmpapzn
A tool for sharing your source code with the world!
Fady
Fady2mo ago
I get the JWT Token as per usual, however the Email is always null in the Json
Jimmacle
Jimmacle2mo ago
are you sure http://schemas.xmlsoap.org/ws/2005/05/identity/claims/emailaddress is the right claim type? look through the whole collection of claims to see what's there
Angius
Angius2mo ago
Just go through claims There should even be ClaimTypes.Email enum value
Fady
Fady2mo ago
I'll check, hold on 😮
Angius
Angius2mo ago
claimsPrincipal.FindFirstValue(ClaimTypes.Email) should do it Something like that
Fady
Fady2mo ago
Do i need any implementation in my program.cs for this as well? I cant seem to get it to work o_o
Fady
Fady2mo ago
No description
Fady
Fady2mo ago
here's how i tried to implement it; Now it's throwing a huge exception
Fady
Fady2mo ago
No description
Angius
Angius2mo ago
I guess principal is null here, huh
Jimmacle
Jimmacle2mo ago
doesn't AuthenticateAsync need you to pass an identity to it for it to actually have one?
Fady
Fady2mo ago
O_O i just started taking this, so im a little out of the loop
Angius
Angius2mo ago
As a side note, it looks to be a very... manual implementation. I'd think getting login with Google is just telling Identity to use the Google provider
Jimmacle
Jimmacle2mo ago
it seems like you're missing the code that actually gets the authentication information from the google login and uses it to build an identity for your user if someone does GET /googleresponse they get automatically logged into your app whether they actually logged in with google or not
Fady
Fady2mo ago
Hmm; Do you have any examples that could put me on the right track? The documentation from Microsoft is a little vague
Jimmacle
Jimmacle2mo ago
i have a manual oauth example in a personal project, i can share it when i get home
Fady
Fady2mo ago
it's been like a week since we started with it I appreciate that 🙂 It's strictly for learning purposes
Jimmacle
Jimmacle2mo ago
basically, google should be redirecting the user to that endpoint with a code or some other information in the query string that you then use to request a token from the google API and that token will contain the user details assuming they logged in successfully
Fady
Fady2mo ago
hmmm i see, thats different from the Jwt token that i actually got?
Jimmacle
Jimmacle2mo ago
you didn't get a JWT anywhere, you're making one yourself with no actual info in it
Fady
Fady2mo ago
Isnt that relevant in this process? Or is that JWT token wrong? oh
Jimmacle
Jimmacle2mo ago
if you want to use a JWT you'll make one for your app like you have now, but you have to actually put some claims in it they don't appear from nothing, you have to complete your end of the oauth process
Fady
Fady2mo ago
I see 😮 i had hardcoded 4 claims previously var claims = result.Principal.Identities.FirstOrDefault().Claims.Select(claim => new { claim.Issuer, claim.OriginalIssuer, claim.Type, claim.Value }); i believe they were these I imagined it was the same but there's very little documentation on how this would work in practice. These things we havent touched on it with school; But we have a personal project in my group where we figured it would be fun to implement it on our own time in our project. So im just trying to learn.
Jimmacle
Jimmacle2mo ago
my code is kind of messy with app specific stuff, but these are the google docs on using oauth https://developers.google.com/identity/protocols/oauth2/web-server
Jimmacle
Jimmacle2mo ago
tl;dr 1. redirect user to google login 2. google redirects user to your callback url with a code 3. you use the code to make a call to google's auth to get an access token 4. read the claims in the token and use them to build yours
Fady
Fady2mo ago
How do you know which Claims you need? Other than the Email one? We have set up Microsoft Identity; I just dont know how to connect Google SSO OAuth to it If that route is simpler Thanks btw 🙂
Jimmacle
Jimmacle2mo ago
if you're using identity there should be a plug and play provider for google auth
Jimmacle
Jimmacle2mo ago
Google external login setup in ASP.NET Core
This tutorial demonstrates the integration of Google account user authentication into an existing ASP.NET Core app.
Fady
Fady2mo ago
Thats the exact documentation i've been following, however it didnt work for me in the end 😮
Jimmacle
Jimmacle2mo ago
re: which claims, whichever claims you need to get the information you need about the user there's no specific list, it's up to you
Fady
Fady2mo ago
I just feel like the documentation is very vague on all of this and its hard to find a way to tackle this; ChatGPT is also pretty useless hahaha Any chance you could walk me through it? I assume its not a lot of code; And the next thing is to explain it further to my peers at school. As we're all stuck with this (its not a homework kind of thing or anything)
Jimmacle
Jimmacle2mo ago
maybe the general oauth docs will help? https://oauth.net/2/
Fady
Fady2mo ago
I can give that an honest try, thanks again 🙂 I think i solved it now 😄 Thanks a lot @Jimmacle @ZZZZZZZZZZZZZZZZZZZZZZZZZ 🙂
Angius
Angius2mo ago
:Ok: