diff --git a/src/GoogleIncrementalMvcSample/Controllers/HomeController.cs b/src/GoogleIncrementalMvcSample/Controllers/HomeController.cs
index 125fa55..59b5d51 100644
--- a/src/GoogleIncrementalMvcSample/Controllers/HomeController.cs
+++ b/src/GoogleIncrementalMvcSample/Controllers/HomeController.cs
@@ -4,7 +4,7 @@
using System.Linq;
using System.Security.Claims;
using System.Threading.Tasks;
-using Google.Apis.Auth.AspNetCore;
+using Google.Apis.Auth.AspNetCore3;
using Google.Apis.Classroom.v1;
using Google.Apis.Classroom.v1.Data;
using Google.Apis.Services;
@@ -18,35 +18,34 @@ namespace GoogleIncrementalMvcSample.Controllers
{
public class HomeController : Controller
{
- private readonly IGoogleAuthProvider _auth;
- public HomeController(IGoogleAuthProvider auth)
+ public HomeController()
{
- _auth = auth;
}
///
/// Display the home page.
///
- public async Task Index()
+ public async Task Index([FromServices] IGoogleAuthProvider _auth)
{
- var model = await LoadUserInfoAsync();
+ var model = await LoadUserInfoAsync(_auth);
return View(model);
}
- ///
- /// Display an error page.
- ///
- public IActionResult Error()
- {
- return View(new ErrorViewModel {RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier});
- }
+ ///
+ /// Display an error page.
+ ///
+ [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
+ public IActionResult Error()
+ {
+ return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
+ }
- ///
- /// User Sign In action.
- ///
- [Authorize]
+ ///
+ /// User Sign In action.
+ ///
+ [Authorize]
public IActionResult SignIn()
{
return RedirectToAction("Index");
@@ -62,16 +61,19 @@ public async Task SignOut()
return RedirectToAction("Index");
}
- ///
- /// Display the list of the the user's Google Classroom classes. Scopes
- /// requested: email, profile, and ClassroomService.Scope.ClassroomCoursesReadonly.
- ///
- [GoogleScopedAuthorize("https://www.googleapis.com/auth/classroom.courses.readonly")]
- public async Task ListCourses()
+ ///
+ /// Display the list of the the user's Google Classroom classes. Scopes
+ /// requested: email, profile, and ClassroomService.Scope.ClassroomCoursesReadonly.
+ ///
+ //[GoogleScopedAuthorize("https://www.googleapis.com/auth/classroom.courses.readonly")]
+
+ [Authorize]
+
+ public async Task ListCourses([FromServices] IGoogleAuthProvider _auth)
{
var cred = await _auth.GetCredentialAsync();
- var model = await LoadUserInfoAsync();
+ var model = await LoadUserInfoAsync(_auth);
try
{
@@ -120,7 +122,7 @@ public async Task ListCourses()
}
}
- private async Task LoadUserInfoAsync()
+ private async Task LoadUserInfoAsync(IGoogleAuthProvider auth)
{
var model = new IndexModel();
@@ -129,7 +131,7 @@ private async Task LoadUserInfoAsync()
model.UserEmail = User.Claims.SingleOrDefault(c => c.Type == ClaimTypes.Email)?.Value;
model.UserName = User.Claims.SingleOrDefault(c => c.Type == "name")?.Value;
- model.Scopes = await _auth.GetCurrentScopesAsync();
+ model.Scopes = await auth.GetCurrentScopesAsync();
}
return model;
diff --git a/src/GoogleIncrementalMvcSample/GoogleIncrementalMvcSample.csproj b/src/GoogleIncrementalMvcSample/GoogleIncrementalMvcSample.csproj
index 86e086d..028fade 100644
--- a/src/GoogleIncrementalMvcSample/GoogleIncrementalMvcSample.csproj
+++ b/src/GoogleIncrementalMvcSample/GoogleIncrementalMvcSample.csproj
@@ -1,18 +1,17 @@
- netcoreapp2.1
- d151a932-920a-42b9-a2a7-1ba24bbc8a7e
+ netcoreapp3.1
-
-
-
+
-
+
+
+
diff --git a/src/GoogleIncrementalMvcSample/Program.cs b/src/GoogleIncrementalMvcSample/Program.cs
index 1d3bb5e..7536747 100644
--- a/src/GoogleIncrementalMvcSample/Program.cs
+++ b/src/GoogleIncrementalMvcSample/Program.cs
@@ -1,19 +1,20 @@
-using Microsoft.ApplicationInsights.Extensibility.Implementation;
-using Microsoft.AspNetCore;
-using Microsoft.AspNetCore.Hosting;
+using Microsoft.AspNetCore.Hosting;
+using Microsoft.Extensions.Hosting;
namespace GoogleIncrementalMvcSample
{
public class Program
{
- public static void Main(string[] args)
- {
- TelemetryDebugWriter.IsTracingDisabled = true;
- CreateWebHostBuilder(args).Build().Run();
- }
-
- public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
- WebHost.CreateDefaultBuilder(args)
- .UseStartup();
+ public static void Main(string[] args)
+ {
+ CreateHostBuilder(args).Build().Run();
}
+
+ public static IHostBuilder CreateHostBuilder(string[] args) =>
+ Host.CreateDefaultBuilder(args)
+ .ConfigureWebHostDefaults(webBuilder =>
+ {
+ webBuilder.UseStartup();
+ });
+ }
}
diff --git a/src/GoogleIncrementalMvcSample/Startup.cs b/src/GoogleIncrementalMvcSample/Startup.cs
index f08c9fd..fb0acd8 100644
--- a/src/GoogleIncrementalMvcSample/Startup.cs
+++ b/src/GoogleIncrementalMvcSample/Startup.cs
@@ -8,6 +8,10 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
+using Microsoft.AspNetCore.HttpsPolicy;
+using Microsoft.Extensions.Hosting;
+using Google.Apis.Auth.AspNetCore3;
+
namespace GoogleIncrementalMvcSample
{
public class Startup
@@ -22,23 +26,35 @@ public Startup(IConfiguration configuration)
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
- services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
- .AddCookie(options => options.ExpireTimeSpan = TimeSpan.FromMinutes(2))
- .AddGoogleOpenIdConnect(options =>
- {
- options.ClientId = Configuration["Authentication:Google:ClientId"];
- options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
- });
-
- services.AddMvc()
- .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
- }
+ services.AddControllersWithViews();
+ services.AddRazorPages();
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- app.UseAuthentication();
+ // This configures Google.Apis.Auth.AspNetCore3 for use in this app.
+ services
+ .AddAuthentication(o =>
+ {
+ // This forces challenge results to be handled by Google OpenID Handler, so there's no
+ // need to add an AccountController that emits challenges for Login.
+ o.DefaultChallengeScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
+ // This forces forbid results to be handled by Google OpenID Handler, which checks if
+ // extra scopes are required and does automatic incremental auth.
+ o.DefaultForbidScheme = GoogleOpenIdConnectDefaults.AuthenticationScheme;
+ // Default scheme that will handle everything else.
+ // Once a user is authenticated, the OAuth2 token info is stored in cookies.
+ o.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
+ })
+ .AddCookie()
+ .AddGoogleOpenIdConnect(options =>
+ {
+ options.ClientId = Configuration["Authentication:Google:ClientId"];
+ options.ClientSecret = Configuration["Authentication:Google:ClientSecret"];
+ });
+
+ }
+ // 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();
@@ -52,12 +68,18 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env)
app.UseHttpsRedirection();
app.UseStaticFiles();
- app.UseMvc(routes =>
- {
- routes.MapRoute(
- name: "default",
- template: "{controller=Home}/{action=Index}/{id?}");
- });
- }
+ app.UseRouting();
+
+ app.UseAuthentication();
+ app.UseAuthorization();
+
+ app.UseEndpoints(endpoints =>
+ {
+ endpoints.MapControllerRoute(
+ name: "default",
+ pattern: "{controller=Home}/{action=Index}/{id?}");
+ endpoints.MapRazorPages();
+ });
}
+ }
}