diff --git a/.gitignore b/.gitignore index f0d200d..6fdd47f 100644 --- a/.gitignore +++ b/.gitignore @@ -338,3 +338,5 @@ ASALocalRun/ # BeatPulse healthcheck temp database healthchecksdb **/data.db +/template/BlazorWithIdentity.Server/data.db-shm +/template/BlazorWithIdentity.Server/data.db-wal diff --git a/template/BlazorWithIdentity.Client/BlazorWithIdentity.Client.csproj b/template/BlazorWithIdentity.Client/BlazorWithIdentity.Client.csproj index 9205d29..80d4e59 100644 --- a/template/BlazorWithIdentity.Client/BlazorWithIdentity.Client.csproj +++ b/template/BlazorWithIdentity.Client/BlazorWithIdentity.Client.csproj @@ -2,6 +2,8 @@ net6.0 + enable + enable diff --git a/template/BlazorWithIdentity.Client/Pages/Counter.razor b/template/BlazorWithIdentity.Client/Pages/Counter.razor index e06b3ee..a917c26 100644 --- a/template/BlazorWithIdentity.Client/Pages/Counter.razor +++ b/template/BlazorWithIdentity.Client/Pages/Counter.razor @@ -1,6 +1,8 @@ @page "/counter" @attribute [Authorize] +Counter +

Counter

Current count: @currentCount

diff --git a/template/BlazorWithIdentity.Client/Pages/FetchData.razor b/template/BlazorWithIdentity.Client/Pages/FetchData.razor index 1f2ee5b..f44867d 100644 --- a/template/BlazorWithIdentity.Client/Pages/FetchData.razor +++ b/template/BlazorWithIdentity.Client/Pages/FetchData.razor @@ -3,6 +3,8 @@ @using BlazorWithIdentity.Shared @inject HttpClient Http +Weather forecast +

Weather forecast

This component demonstrates fetching data from the server.

diff --git a/template/BlazorWithIdentity.Client/Pages/Index.razor b/template/BlazorWithIdentity.Client/Pages/Index.razor index d67bba2..9229eb3 100644 --- a/template/BlazorWithIdentity.Client/Pages/Index.razor +++ b/template/BlazorWithIdentity.Client/Pages/Index.razor @@ -1,6 +1,8 @@ @page "/" @attribute [Authorize] +Index +

Hello @context.User.Identity.Name !!

diff --git a/template/BlazorWithIdentity.Client/Pages/Login.razor b/template/BlazorWithIdentity.Client/Pages/Login.razor index 3f877fa..c9bdb66 100644 --- a/template/BlazorWithIdentity.Client/Pages/Login.razor +++ b/template/BlazorWithIdentity.Client/Pages/Login.razor @@ -3,6 +3,8 @@ @inject NavigationManager navigationManager @inject IdentityAuthenticationStateProvider authStateProvider +Login +

Blazor with Identity Sample

diff --git a/template/BlazorWithIdentity.Client/Pages/Register.razor b/template/BlazorWithIdentity.Client/Pages/Register.razor index d0169a8..71d079c 100644 --- a/template/BlazorWithIdentity.Client/Pages/Register.razor +++ b/template/BlazorWithIdentity.Client/Pages/Register.razor @@ -3,6 +3,8 @@ @inject NavigationManager navigationManager @inject IdentityAuthenticationStateProvider authStateProvider +Register +

Blazor with Identity Sample

diff --git a/template/BlazorWithIdentity.Client/Program.cs b/template/BlazorWithIdentity.Client/Program.cs index 557a1fa..15a5fb6 100644 --- a/template/BlazorWithIdentity.Client/Program.cs +++ b/template/BlazorWithIdentity.Client/Program.cs @@ -1,33 +1,20 @@ -using BlazorWithIdentity.Client.Services.Contracts; +using BlazorWithIdentity.Client; +using BlazorWithIdentity.Client.Services.Contracts; using BlazorWithIdentity.Client.Services.Implementations; using BlazorWithIdentity.Client.States; +using Microsoft.AspNetCore.Components.Web; using Microsoft.AspNetCore.Components.Authorization; using Microsoft.AspNetCore.Components.WebAssembly.Hosting; -using Microsoft.Extensions.DependencyInjection; -using System; -using System.Net.Http; -using System.Threading.Tasks; -namespace BlazorWithIdentity.Client -{ - public class Program - { - public static async Task Main(string[] args) - { - var builder = WebAssemblyHostBuilder.CreateDefault(args); - builder.RootComponents.Add("#app"); +var builder = WebAssemblyHostBuilder.CreateDefault(args); +builder.RootComponents.Add("#app"); +builder.RootComponents.Add("head::after"); - builder.Services.AddOptions(); - builder.Services.AddAuthorizationCore(); - builder.Services.AddScoped(); - builder.Services.AddScoped(s => s.GetRequiredService()); - builder.Services.AddScoped(); +builder.Services.AddOptions(); +builder.Services.AddAuthorizationCore(); +builder.Services.AddScoped(); +builder.Services.AddScoped(s => s.GetRequiredService()); +builder.Services.AddScoped(); +builder.Services.AddScoped(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); - builder.Services.AddTransient(sp => new HttpClient { BaseAddress = new Uri(builder.HostEnvironment.BaseAddress) }); - - - var host = builder.Build(); - await host.RunAsync(); - } - } -} +await builder.Build().RunAsync(); \ No newline at end of file diff --git a/template/BlazorWithIdentity.Client/Properties/launchSettings.json b/template/BlazorWithIdentity.Client/Properties/launchSettings.json index f29121d..2258f5a 100644 --- a/template/BlazorWithIdentity.Client/Properties/launchSettings.json +++ b/template/BlazorWithIdentity.Client/Properties/launchSettings.json @@ -8,20 +8,20 @@ } }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", + "BlazorWithIdentity": { + "commandName": "Project", + "dotnetRunMessages": "true", "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, - "BlazorWithIdentity": { - "commandName": "Project", - "dotnetRunMessages": "true", + "IIS Express": { + "commandName": "IISExpress", "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/template/BlazorWithIdentity.Server/BlazorWithIdentity.Server.csproj b/template/BlazorWithIdentity.Server/BlazorWithIdentity.Server.csproj index 560d552..5da93ab 100644 --- a/template/BlazorWithIdentity.Server/BlazorWithIdentity.Server.csproj +++ b/template/BlazorWithIdentity.Server/BlazorWithIdentity.Server.csproj @@ -2,6 +2,8 @@ net6.0 + enable + enable diff --git a/template/BlazorWithIdentity.Server/Controllers/AuthorizeController.cs b/template/BlazorWithIdentity.Server/Controllers/AuthorizeController.cs index c06fa76..53ee460 100644 --- a/template/BlazorWithIdentity.Server/Controllers/AuthorizeController.cs +++ b/template/BlazorWithIdentity.Server/Controllers/AuthorizeController.cs @@ -3,10 +3,6 @@ using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace BlazorWithIdentity.Server.Controllers { diff --git a/template/BlazorWithIdentity.Server/Controllers/SampleDataController.cs b/template/BlazorWithIdentity.Server/Controllers/SampleDataController.cs index c0531fc..0dc68d8 100644 --- a/template/BlazorWithIdentity.Server/Controllers/SampleDataController.cs +++ b/template/BlazorWithIdentity.Server/Controllers/SampleDataController.cs @@ -1,10 +1,6 @@ using BlazorWithIdentity.Shared; using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; namespace BlazorWithIdentity.Server.Controllers { diff --git a/template/BlazorWithIdentity.Server/Program.cs b/template/BlazorWithIdentity.Server/Program.cs index 1207505..acb43d7 100644 --- a/template/BlazorWithIdentity.Server/Program.cs +++ b/template/BlazorWithIdentity.Server/Program.cs @@ -1,26 +1,82 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Threading.Tasks; -using Microsoft.AspNetCore.Hosting; -using Microsoft.Extensions.Configuration; -using Microsoft.Extensions.Hosting; -using Microsoft.Extensions.Logging; - -namespace BlazorWithIdentity.Server +using BlazorWithIdentity.Server.Data; +using BlazorWithIdentity.Server.Models; +using Microsoft.AspNetCore.Identity; +using Microsoft.EntityFrameworkCore; + +var builder = WebApplication.CreateBuilder(args); + +// Add services to the container. +builder.Services.AddDbContext(options => + options.UseSqlite("Filename=data.db")); + +builder.Services.AddIdentity>() + .AddEntityFrameworkStores() + .AddDefaultTokenProviders(); + +builder.Services.Configure(options => +{ + // Password settings + options.Password.RequireDigit = true; + options.Password.RequiredLength = 6; + options.Password.RequireNonAlphanumeric = true; + options.Password.RequireUppercase = true; + options.Password.RequireLowercase = false; + + // Lockout settings + options.Lockout.DefaultLockoutTimeSpan = TimeSpan.FromMinutes(30); + options.Lockout.MaxFailedAccessAttempts = 10; + options.Lockout.AllowedForNewUsers = true; + + // User settings + options.User.RequireUniqueEmail = false; +}); + +builder.Services.ConfigureApplicationCookie(options => +{ + options.Cookie.HttpOnly = true; + options.Events.OnRedirectToLogin = context => + { + context.Response.StatusCode = 401; + return Task.CompletedTask; + }; +}); + + +builder.Services.AddControllersWithViews(); +builder.Services.AddRazorPages(); + +var app = builder.Build(); + +// Configure the HTTP request pipeline. +if (app.Environment.IsDevelopment()) { - public class Program + using (var serviceScope = app.Services.GetRequiredService().CreateScope()) { - public static void Main(string[] args) - { - CreateHostBuilder(args).Build().Run(); - } - - public static IHostBuilder CreateHostBuilder(string[] args) => - Host.CreateDefaultBuilder(args) - .ConfigureWebHostDefaults(webBuilder => - { - webBuilder.UseStartup(); - }); + //Note: Microsoft recommends to NOT migrate your database at Startup. + //You should consider your migration strategy according to the guidelines + serviceScope.ServiceProvider.GetService().Database.Migrate(); } + + app.UseWebAssemblyDebugging(); +} +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.UseBlazorFrameworkFiles(); +app.UseStaticFiles(); + +app.UseRouting(); +app.UseAuthentication(); +app.UseAuthorization(); + +app.MapRazorPages(); +app.MapControllers(); +app.MapFallbackToFile("index.html"); + +app.Run(); diff --git a/template/BlazorWithIdentity.Server/Properties/launchSettings.json b/template/BlazorWithIdentity.Server/Properties/launchSettings.json index d255631..2555c09 100644 --- a/template/BlazorWithIdentity.Server/Properties/launchSettings.json +++ b/template/BlazorWithIdentity.Server/Properties/launchSettings.json @@ -8,20 +8,20 @@ } }, "profiles": { - "IIS Express": { - "commandName": "IISExpress", + "BlazorWithIdentity.Server": { + "commandName": "Project", + "dotnetRunMessages": true, "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", + "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } }, - "BlazorWithIdentity.Server": { - "commandName": "Project", - "dotnetRunMessages": "true", + "IIS Express": { + "commandName": "IISExpress", "launchBrowser": true, "inspectUri": "{wsProtocol}://{url.hostname}:{url.port}/_framework/debug/ws-proxy?browser={browserInspectUri}", - "applicationUrl": "https://localhost:5001;http://localhost:5000", "environmentVariables": { "ASPNETCORE_ENVIRONMENT": "Development" } diff --git a/template/BlazorWithIdentity.Server/appsettings.Development.json b/template/BlazorWithIdentity.Server/appsettings.Development.json index 8983e0f..0c208ae 100644 --- a/template/BlazorWithIdentity.Server/appsettings.Development.json +++ b/template/BlazorWithIdentity.Server/appsettings.Development.json @@ -2,8 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Microsoft.AspNetCore": "Warning" } } } diff --git a/template/BlazorWithIdentity.Server/appsettings.json b/template/BlazorWithIdentity.Server/appsettings.json index 3c37399..b665055 100644 --- a/template/BlazorWithIdentity.Server/appsettings.json +++ b/template/BlazorWithIdentity.Server/appsettings.json @@ -2,8 +2,7 @@ "Logging": { "LogLevel": { "Default": "Information", - "Microsoft": "Warning", - "Microsoft.Hosting.Lifetime": "Information" + "Microsoft.AspNetCore": "Warning" } }, "AllowedHosts": "*" diff --git a/template/BlazorWithIdentity.Server/data.db b/template/BlazorWithIdentity.Server/data.db deleted file mode 100644 index 3c972b8..0000000 Binary files a/template/BlazorWithIdentity.Server/data.db and /dev/null differ diff --git a/templatepack.csproj b/templatepack.csproj index 77dcbb9..7e3b287 100644 --- a/templatepack.csproj +++ b/templatepack.csproj @@ -3,7 +3,7 @@ Template $(VersionSuffix) - 1.2.0 + 1.3.0 $(Version)-$(VersionSuffix) LICENSE.txt https://github.com/stavroskasidis/BlazorWithIdentity