Add authentication

This commit is contained in:
Robert Marshall 2020-01-03 13:32:20 +00:00
parent 8f0c4c0a45
commit a2d84e182d
11 changed files with 206 additions and 21 deletions

View file

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
@ -27,15 +28,18 @@ namespace Website
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddSingleton<IConfiguration>(Configuration);
services.AddSingleton(Configuration);
RegisterRepositories(services);
services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme).AddCookie();
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
}
private void RegisterRepositories(IServiceCollection services) =>
services.AddSingleton<IDatabaseProvider, MySQLDatabaseProvider>()
.AddSingleton<BlogRepository, BlogRepository>();
.AddSingleton<BlogRepository, BlogRepository>()
.AddSingleton<UserRepository, UserRepository>();
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
@ -51,24 +55,23 @@ namespace Website
app.UseHsts();
}
app.UseStatusCodePagesWithReExecute("/Error/PageNotFound");
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "blogPages",
template: "blog/{action}/{page:int}",
defaults: new { controller = "Blog", action = "Page", page = 1 });
routes.MapRoute(
name: "blogView",
template: "blog/view/{url}",
defaults: new { controller = "Blog", action = "Entry"});
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.UseStatusCodePagesWithReExecute("/Error/PageNotFound")
.UseHttpsRedirection()
.UseStaticFiles()
.UseAuthentication()
.UseMvc(routes => {
routes.MapRoute(
name: "blogPages",
template: "blog/{action}/{page:int}",
defaults: new {controller = "Blog", action = "Page", page = 1});
routes.MapRoute(
name: "blogView",
template: "blog/view/{url}",
defaults: new {controller = "Blog", action = "Entry"});
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}