Skip to content

Instantly share code, notes, and snippets.

@SaahilClaypool
Created January 12, 2021 19:22
Show Gist options
  • Save SaahilClaypool/b7111f2d48714b1e0c840723deccf742 to your computer and use it in GitHub Desktop.
Save SaahilClaypool/b7111f2d48714b1e0c840723deccf742 to your computer and use it in GitHub Desktop.
Google JWT (without secret validation)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.AspNetCore.Authentication.JwtBearer;
using System.IdentityModel.Tokens.Jwt;
using Microsoft.IdentityModel.Tokens;
using System.Security.Claims;
namespace API
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
services.AddAuthentication();
services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddJwtBearer(jwtOptions =>
{
IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");
// jwtOptions.Audience = "{the OAuth 2.0 client ID credential from google api developer console}";
System.Console.WriteLine(googleAuthNSection["ClientId"]);
jwtOptions.Audience = googleAuthNSection["ClientId"];
jwtOptions.Authority = "https://accounts.google.com";
jwtOptions.TokenValidationParameters = new TokenValidationParameters();
jwtOptions.TokenValidationParameters.ValidIssuers = new List<string>()
{
"https://accounts.google.com",
"accounts.google.com"
};
});
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllers();
endpoints.MapRazorPages();
});
}
}
}
@SaahilClaypool
Copy link
Author

// using     "Hellang.Authentication.JwtBearer.Google" Version="2.0.0" 
// https://github.com/khellang/Middleware
            services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
                .AddJwtBearer(x =>
                {
                    IConfigurationSection googleAuthNSection = Configuration.GetSection("Authentication:Google");
                    x.UseGoogle(
                    clientId: googleAuthNSection["ClientId"]);
                });

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment