|
@@ -4,9 +4,13 @@ using System.Linq;
|
|
|
using System.Reflection;
|
|
using System.Reflection;
|
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
using FluentValidation.AspNetCore;
|
|
using FluentValidation.AspNetCore;
|
|
|
|
|
+using GreenTree.Strohrmann.ERP.Core.Helper;
|
|
|
using GreenTree.Strohrmann.ERP.Domain.Model;
|
|
using GreenTree.Strohrmann.ERP.Domain.Model;
|
|
|
|
|
+using GreenTree.Strohrmann.ERP.Services.Authentication;
|
|
|
using GreenTree.Strohrmann.ERP.Services.Authorization;
|
|
using GreenTree.Strohrmann.ERP.Services.Authorization;
|
|
|
using GreenTree.Strohrmann.ERP.Services.Notification;
|
|
using GreenTree.Strohrmann.ERP.Services.Notification;
|
|
|
|
|
+using Microsoft.AspNetCore.Authentication;
|
|
|
|
|
+using Microsoft.AspNetCore.Authentication.Cookies;
|
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Builder;
|
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
@@ -29,6 +33,7 @@ namespace GreenTree.Strohrmann.ERP.Web
|
|
|
/// </summary>
|
|
/// </summary>
|
|
|
public static readonly string[] _availablePolicies =
|
|
public static readonly string[] _availablePolicies =
|
|
|
{
|
|
{
|
|
|
|
|
+ "ViewDashboard",
|
|
|
"ViewUser",
|
|
"ViewUser",
|
|
|
"ChangeUser",
|
|
"ChangeUser",
|
|
|
"DeleteUser"
|
|
"DeleteUser"
|
|
@@ -52,6 +57,17 @@ namespace GreenTree.Strohrmann.ERP.Web
|
|
|
// Add option handling
|
|
// Add option handling
|
|
|
services.AddOptions();
|
|
services.AddOptions();
|
|
|
|
|
|
|
|
|
|
+ // Add the HttpContextAccessor as Singleton
|
|
|
|
|
+ services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
+
|
|
|
|
|
+ // Add global administration notification options
|
|
|
|
|
+ var administrationOptions = Configuration.GetSection("AdministrationOptions").Get<AdministrationOptions>();
|
|
|
|
|
+
|
|
|
|
|
+ if (administrationOptions == null)
|
|
|
|
|
+ throw new Exception("The appsettings.json does not contain administration options.");
|
|
|
|
|
+
|
|
|
|
|
+ services.AddSingleton(administrationOptions);
|
|
|
|
|
+
|
|
|
// Add global mail notification options
|
|
// Add global mail notification options
|
|
|
var mailNotificationOptions = Configuration.GetSection("MailNotificationOptions").Get<MailNotificationOptions>();
|
|
var mailNotificationOptions = Configuration.GetSection("MailNotificationOptions").Get<MailNotificationOptions>();
|
|
|
|
|
|
|
@@ -79,12 +95,27 @@ namespace GreenTree.Strohrmann.ERP.Web
|
|
|
options.UseLazyLoadingProxies();
|
|
options.UseLazyLoadingProxies();
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
|
|
+ // Add user helper service
|
|
|
|
|
+ services.AddScoped<IUserHelper, UserHelper>();
|
|
|
|
|
+
|
|
|
// Add MVC with FluentValidation reference
|
|
// Add MVC with FluentValidation reference
|
|
|
services.AddMvc()
|
|
services.AddMvc()
|
|
|
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly()));
|
|
.AddFluentValidation(fv => fv.RegisterValidatorsFromAssembly(Assembly.GetExecutingAssembly()));
|
|
|
|
|
|
|
|
// Add authentication
|
|
// Add authentication
|
|
|
- services.AddAuthentication();
|
|
|
|
|
|
|
+ services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
|
|
|
|
|
+ .AddCookie(options =>
|
|
|
|
|
+ {
|
|
|
|
|
+ options.Cookie.HttpOnly = true;
|
|
|
|
|
+ options.Cookie.SecurePolicy = CookieSecurePolicy.SameAsRequest;
|
|
|
|
|
+ options.Cookie.SameSite = SameSiteMode.Strict;
|
|
|
|
|
+ options.LoginPath = "/Account/Login";
|
|
|
|
|
+ options.LogoutPath = "/Account/Logoff";
|
|
|
|
|
+ options.ExpireTimeSpan = new TimeSpan(0, 24, 0, 0);
|
|
|
|
|
+ });
|
|
|
|
|
+
|
|
|
|
|
+ // Add the default custom authentication service
|
|
|
|
|
+ services.AddScoped<Services.Authentication.IAuthenticationService, DbContextAuthenticationService>();
|
|
|
|
|
|
|
|
// Add the default authorization handler
|
|
// Add the default authorization handler
|
|
|
services.AddScoped<IAuthorizationHandler, DefaultAuthorizationHandler>();
|
|
services.AddScoped<IAuthorizationHandler, DefaultAuthorizationHandler>();
|
|
@@ -95,16 +126,15 @@ namespace GreenTree.Strohrmann.ERP.Web
|
|
|
{
|
|
{
|
|
|
options.AddPolicy(policy, a =>
|
|
options.AddPolicy(policy, a =>
|
|
|
{
|
|
{
|
|
|
|
|
+ a.AuthenticationSchemes.Add(CookieAuthenticationDefaults.AuthenticationScheme);
|
|
|
|
|
+ a.RequireAuthenticatedUser();
|
|
|
a.AddRequirements(new DefaultAuthorizationPolicy(policy));
|
|
a.AddRequirements(new DefaultAuthorizationPolicy(policy));
|
|
|
});
|
|
});
|
|
|
}
|
|
}
|
|
|
});
|
|
});
|
|
|
|
|
|
|
|
- // Add the Windows authorization service
|
|
|
|
|
- services.AddScoped<Services.Authorization.IAuthorizationService, DbAuthorizationService>();
|
|
|
|
|
-
|
|
|
|
|
- // Add the HttpContextAccessor as Singleton
|
|
|
|
|
- services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
|
|
|
|
|
|
|
+ // Add the DbContext custom authorization service
|
|
|
|
|
+ services.AddScoped<Services.Authorization.IAuthorizationService, CookieAuthorizationService>();
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
|
|
@@ -123,6 +153,8 @@ namespace GreenTree.Strohrmann.ERP.Web
|
|
|
app.UseRouting();
|
|
app.UseRouting();
|
|
|
|
|
|
|
|
app.UseAuthorization();
|
|
app.UseAuthorization();
|
|
|
|
|
+ app.UseAuthentication();
|
|
|
|
|
+ app.UseCookiePolicy();
|
|
|
|
|
|
|
|
app.UseEndpoints(endpoints =>
|
|
app.UseEndpoints(endpoints =>
|
|
|
{
|
|
{
|