137 lines
5.3 KiB
C#
137 lines
5.3 KiB
C#
using GWMS.Data;
|
|
using GWMS.User.Areas.Identity;
|
|
using GWMS.User.Data;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Builder;
|
|
using Microsoft.AspNetCore.Components;
|
|
using Microsoft.AspNetCore.Components.Authorization;
|
|
using Microsoft.AspNetCore.Hosting;
|
|
using Microsoft.AspNetCore.HttpsPolicy;
|
|
using Microsoft.AspNetCore.Identity;
|
|
using Microsoft.AspNetCore.Identity.UI;
|
|
using Microsoft.AspNetCore.Identity.UI.Services;
|
|
using Microsoft.EntityFrameworkCore;
|
|
using Microsoft.Extensions.Configuration;
|
|
using Microsoft.Extensions.DependencyInjection;
|
|
using Microsoft.Extensions.Hosting;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
|
|
namespace GWMS.User
|
|
{
|
|
public class Startup
|
|
{
|
|
#region Public Constructors
|
|
|
|
public Startup(IConfiguration configuration)
|
|
{
|
|
Configuration = configuration;
|
|
}
|
|
|
|
#endregion Public Constructors
|
|
|
|
#region Public Properties
|
|
|
|
public IConfiguration Configuration { get; }
|
|
|
|
#endregion Public Properties
|
|
|
|
#region Public Methods
|
|
|
|
// 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();
|
|
app.UseMigrationsEndPoint();
|
|
}
|
|
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.MapBlazorHub();
|
|
endpoints.MapFallbackToPage("/_Host");
|
|
});
|
|
}
|
|
|
|
// This method gets called by the runtime. Use this method to add services to the container.
|
|
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
|
|
public void ConfigureServices(IServiceCollection services)
|
|
{
|
|
string connString = Configuration.GetConnectionString("AuthConnection");
|
|
var serverVersion = DbConfig.MysqlServerVersion(connString);
|
|
|
|
// abilitazione x email management con MailKit
|
|
services.AddTransient<IEmailSender, MailKitEmailSender>();
|
|
services.Configure<MailKitEmailSenderOptions>(options =>
|
|
{
|
|
options.Host_Address = Configuration["ExternalProviders:MailKit:SMTP:Address"];
|
|
options.Host_Port = Convert.ToInt32(Configuration["ExternalProviders:MailKit:SMTP:Port"]);
|
|
options.Host_Username = Configuration["ExternalProviders:MailKit:SMTP:Account"];
|
|
options.Host_Password = Configuration["ExternalProviders:MailKit:SMTP:Password"];
|
|
options.Sender_EMail = Configuration["ExternalProviders:MailKit:SMTP:SenderEmail"];
|
|
options.Sender_Name = Configuration["ExternalProviders:MailKit:SMTP:SenderName"];
|
|
});
|
|
|
|
// cookie applicazione da 14 gg (defaul) a 30
|
|
services.ConfigureApplicationCookie(o =>
|
|
{
|
|
o.ExpireTimeSpan = TimeSpan.FromDays(30);
|
|
o.SlidingExpiration = true;
|
|
});
|
|
// token di sicurezza dati a 3h
|
|
services.Configure<DataProtectionTokenProviderOptions>(o =>
|
|
o.TokenLifespan = TimeSpan.FromHours(3));
|
|
|
|
// DB management
|
|
services.AddDbContext<UserIdentityDbContext>(options =>
|
|
options.UseMySql(connString, serverVersion));
|
|
|
|
// identity management
|
|
services.AddDefaultIdentity<IdentityUser>(options => options.SignIn.RequireConfirmedAccount = true)
|
|
.AddRoles<IdentityRole>()
|
|
.AddEntityFrameworkStores<UserIdentityDbContext>();
|
|
|
|
services.AddRazorPages();
|
|
|
|
// rif auth
|
|
// https://stackoverflow.com/questions/60687879/require-authorization-on-all-blazor-pages/60688109#60688109
|
|
// https://www.c-sharpcorner.com/article/understand-basic-of-authorization-in-blazor-server-app/#:~:text=Authentication%20is%20a%20process%20of%20validating%20a%20user,UI%20option%20can%20be%20accessible%20by%20the%20user.
|
|
|
|
// non funziona --> messo attributo in _Imports.razor e esclusione in Index page
|
|
#if false
|
|
// richiesta esplicita autorizzazione
|
|
services.AddAuthorization(options =>
|
|
{
|
|
options.FallbackPolicy = new AuthorizationPolicyBuilder()
|
|
.RequireAuthenticatedUser()
|
|
.Build();
|
|
});
|
|
#endif
|
|
|
|
services.AddServerSideBlazor();
|
|
services.AddScoped<AuthenticationStateProvider, RevalidatingIdentityAuthenticationStateProvider<IdentityUser>>();
|
|
services.AddDatabaseDeveloperPageExceptionFilter();
|
|
services.AddSingleton<WeatherForecastService>();
|
|
}
|
|
|
|
#endregion Public Methods
|
|
}
|
|
} |