C
C#10mo ago
Ares

How do i get the users profile picture when using ASP.NET CORE Google Auth?

Im using this package: https://www.nuget.org/packages/Microsoft.AspNetCore.Authentication.Google/ In my login controller, I have some of the following code:
public async Task<IActionResult> GoogleResponse() //authentication
{
try
{
var result = await HttpContext.AuthenticateAsync(
CookieAuthenticationDefaults.AuthenticationScheme
);

if (result.Principal == null)
throw new Exception("Could not authenticate");

var claims = result
.Principal.Identities.FirstOrDefault()
?.Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
})
.ToList();
public async Task<IActionResult> GoogleResponse() //authentication
{
try
{
var result = await HttpContext.AuthenticateAsync(
CookieAuthenticationDefaults.AuthenticationScheme
);

if (result.Principal == null)
throw new Exception("Could not authenticate");

var claims = result
.Principal.Identities.FirstOrDefault()
?.Claims.Select(claim => new
{
claim.Issuer,
claim.OriginalIssuer,
claim.Type,
claim.Value
})
.ToList();
claims object contains 5 properties , that have the email, firstname, lastname, etc. but it doesnt have the link for the user profile picture. I'm trying to figure out how to get the users profile picture while logging in. Is there a way i can do that?
Microsoft.AspNetCore.Authentication.Google 8.0.1
ASP.NET Core contains middleware to support Google's OpenId and OAuth 2.0 authentication workflows. This package was built from the source code at https://github.com/dotnet/aspnetcore/tree/8e941eb42f819adb116b881195158b3887a70a1c
6 Replies
Unknown User
Unknown User10mo ago
Message Not Public
Sign In & Join Server To View
Ares
AresOP10mo ago
hkm ok i was looking into it seem a bit complicated than i thought maybe i wont bother then, cause i remember i was doing it in a previous project and it was super easy to do, i thought it would be the same here
leowest
leowest10mo ago
as far as I understand u need to add GoogleOptions with the scope needed for the pfp https://learn.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.authentication.google.googleoptions?view=aspnetcore-8.0 there is an example on this one using claims https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-8.0 if you already have the scope try
var picture = result.Principal.FindFirstValue("picture");
var picture = result.Principal.FindFirstValue("picture");
Ares
AresOP10mo ago
ah i think that actually worked lol. i added
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.ClaimActions.MapJsonKey("urn:google:locale", "locale", "string");
options.ClaimActions.MapJsonKey("urn:google:picture", "picture", "url");
options.ClaimActions.MapJsonKey("urn:google:locale", "locale", "string");
to program.cs and that was all i needed
No description
Ares
AresOP10mo ago
yea i put the link in a new tab and its my pfp. so thanks guys
Want results from more Discord servers?
Add your server