C
C#7mo ago
F

✅ 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
F
FOP7mo ago
It's an ASP.NET Project and the relevant code looks like this
F
FOP7mo ago
BlazeBin - efjmobmpapzn
A tool for sharing your source code with the world!
F
FOP7mo ago
I get the JWT Token as per usual, however the Email is always null in the Json
Jimmacle
Jimmacle7mo 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
Angius7mo ago
Just go through claims There should even be ClaimTypes.Email enum value
F
FOP7mo ago
I'll check, hold on 😮
Angius
Angius7mo ago
claimsPrincipal.FindFirstValue(ClaimTypes.Email) should do it Something like that
F
FOP7mo ago
Do i need any implementation in my program.cs for this as well? I cant seem to get it to work o_o
F
FOP7mo ago
No description
F
FOP7mo ago
here's how i tried to implement it; Now it's throwing a huge exception
F
FOP7mo ago
No description
Angius
Angius7mo ago
I guess principal is null here, huh
Jimmacle
Jimmacle7mo ago
doesn't AuthenticateAsync need you to pass an identity to it for it to actually have one?
F
FOP7mo ago
O_O i just started taking this, so im a little out of the loop
Angius
Angius7mo 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
Jimmacle7mo 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
F
FOP7mo ago
Hmm; Do you have any examples that could put me on the right track? The documentation from Microsoft is a little vague
Jimmacle
Jimmacle7mo ago
i have a manual oauth example in a personal project, i can share it when i get home
F
FOP7mo ago
it's been like a week since we started with it I appreciate that 🙂 It's strictly for learning purposes
Jimmacle
Jimmacle7mo 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
F
FOP7mo ago
hmmm i see, thats different from the Jwt token that i actually got?
Jimmacle
Jimmacle7mo ago
you didn't get a JWT anywhere, you're making one yourself with no actual info in it
F
FOP7mo ago
Isnt that relevant in this process? Or is that JWT token wrong? oh
Jimmacle
Jimmacle7mo 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
F
FOP7mo 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
Jimmacle7mo 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
Jimmacle7mo 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
F
FOP7mo 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
Jimmacle7mo ago
if you're using identity there should be a plug and play provider for google auth
Jimmacle
Jimmacle7mo 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.
F
FOP7mo ago
Thats the exact documentation i've been following, however it didnt work for me in the end 😮
Jimmacle
Jimmacle7mo 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
F
FOP7mo 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
Jimmacle7mo ago
maybe the general oauth docs will help? https://oauth.net/2/
F
FOP7mo ago
I can give that an honest try, thanks again 🙂 I think i solved it now 😄 Thanks a lot @Jimmacle @ZZZZZZZZZZZZZZZZZZZZZZZZZ 🙂
Angius
Angius7mo ago
:Ok:
Want results from more Discord servers?
Add your server