Persist additional claims and tokens from external providers in ASP.NET Core

68 Views Asked by At

I'm trying to follow the steps in this article: https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-7.0

To get the user image (large) and the user birthday and gender from external login using Facebook and Google.

I've tried so many approaches but none of them worked and it seems like Identity fails to assign the proper scopes and claims when configuring the application.

I'm using .Net Core 7 with Identity. Please advice.

// Persist additional claims and tokens from external providers in ASP.NET Core // https://learn.microsoft.com/en-us/aspnet/core/security/authentication/social/additional-claims?view=aspnetcore-7.0 builder.Services.AddAuthentication() .AddGoogle(options => { IConfigurationSection googleAuthNSection = builder.Configuration.GetSection("Authentication:Google"); options.ClientId = googleAuthNSection.GetValue<string>("ClientId")!; options.ClientSecret = googleAuthNSection.GetValue<string>("ClientSecret")!; //options.AccessDeniedPath = new PathString("/"); //options.Scope.Add("https://www.googleapis.com/auth/user.birthday.read"); options.SaveTokens = true; options.Events.OnCreatingTicket = (context) => { string? picture = context.User.GetProperty("picture").GetString(); //string? dob = context.User.GetProperty("birthdate").GetString(); context.Identity!.AddClaim(new Claim("picture", picture!)); List<AuthenticationToken> tokens = context.Properties.GetTokens().ToList(); tokens.Add(new AuthenticationToken() { Name = "TicketCreated", Value = DateTime.UtcNow.ToString() }); context.Properties.StoreTokens(tokens); return Task.CompletedTask; }; })

0

There are 0 best solutions below