Created
January 12, 2021 19:22
-
-
Save SaahilClaypool/b7111f2d48714b1e0c840723deccf742 to your computer and use it in GitHub Desktop.
Google JWT (without secret validation)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | |
}); | |
} | |
} | |
} |
Author
SaahilClaypool
commented
Jan 13, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment