Skip to content

Endpoint routing in SPA templates #11621

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Jul 20, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public void ConfigureServices(IServiceCollection services)
{
#if (IndividualLocalAuth)
services.AddDbContext<ApplicationDbContext>(options =>
#if (UseLocalDB)
#if (UseLocalDB)
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
#else
#else
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
#endif
#endif

services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
Expand All @@ -53,8 +53,10 @@ public void ConfigureServices(IServiceCollection services)
services.AddAuthentication()
.AddIdentityServerJwt();
#endif
services.AddMvc(options => options.EnableEndpointRouting = false);

services.AddControllersWithViews();
#if (IndividualLocalAuth)
services.AddRazorPages();
#endif
// In production, the Angular files will be served from this directory
services.AddSpaStaticFiles(configuration =>
{
Expand Down Expand Up @@ -87,17 +89,24 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
#endif
app.UseStaticFiles();
app.UseSpaStaticFiles();
#if (IndividualLocalAuth)

app.UseRouting();

#if (IndividualLocalAuth)
app.UseAuthentication();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should have UseRouting() above it, and UseAuthorization() after it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With endpoint routing, the auth is handled by middleware now, so the authN/AuthZ middleware sit in between UseRouting and UseEndpoints

app.UseIdentityServer();
#endif

app.UseMvc(routes =>
#if (!NoAuth)
app.UseAuthorization();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think authorization should only get included when you aren't doing any authentication.

#endif
app.UseEndpoints(endpoints =>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also change to be similar to the other templates in ConfigureServices. This should have AddControllersWithViews(), and also AddRazorPages() when Identity is present.

{
routes.MapRoute(
endpoints.MapControllerRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
pattern: "{controller}/{action=Index}/{id?}");
#if (IndividualLocalAuth)
endpoints.MapRazorPages();
#endif
});

app.UseSpa(spa =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ public void ConfigureServices(IServiceCollection services)
{
#if (IndividualLocalAuth)
services.AddDbContext<ApplicationDbContext>(options =>
#if (UseLocalDB)
#if (UseLocalDB)
options.UseSqlServer(
Configuration.GetConnectionString("DefaultConnection")));
#else
#else
options.UseSqlite(
Configuration.GetConnectionString("DefaultConnection")));
#endif
#endif

services.AddDefaultIdentity<ApplicationUser>()
.AddEntityFrameworkStores<ApplicationDbContext>();
Expand All @@ -53,7 +53,11 @@ public void ConfigureServices(IServiceCollection services)
services.AddAuthentication()
.AddIdentityServerJwt();
#endif
services.AddMvc(options => options.EnableEndpointRouting = false);

services.AddControllersWithViews();
#if (IndividualLocalAuth)
services.AddRazorPages();
#endif

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
Expand Down Expand Up @@ -87,17 +91,24 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
#endif
app.UseStaticFiles();
app.UseSpaStaticFiles();
#if (IndividualLocalAuth)

app.UseRouting();

#if (IndividualLocalAuth)
app.UseAuthentication();
app.UseIdentityServer();
#endif

app.UseMvc(routes =>
#if (!NoAuth)
app.UseAuthorization();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think UseAuthorization should only be included when authentication is enabled.

#endif
app.UseEndpoints(endpoints =>
{
routes.MapRoute(
endpoints.MapControllerRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
pattern: "{controller}/{action=Index}/{id?}");
#if (IndividualLocalAuth)
endpoints.MapRazorPages();
#endif
});

app.UseSpa(spa =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ 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.AddMvc(options => options.EnableEndpointRouting = false);
services.AddControllersWithViews();

// In production, the React files will be served from this directory
services.AddSpaStaticFiles(configuration =>
Expand Down Expand Up @@ -55,11 +55,13 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
app.UseStaticFiles();
app.UseSpaStaticFiles();

app.UseMvc(routes =>
app.UseRouting();

app.UseEndpoints(endpoints =>
{
routes.MapRoute(
endpoints.MapControllerRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
pattern: "{controller}/{action=Index}/{id?}");
});

app.UseSpa(spa =>
Expand Down