From 7459c47b36fb49db34fe8842138fe00afa0d1a4c Mon Sep 17 00:00:00 2001 From: Denis Prokhorchik Date: Sun, 30 Jan 2022 05:04:38 +0300 Subject: [PATCH] feat(issue-182): update pages for is4 --- .../Extensions/ServiceCollectionExtensions.cs | 27 ++++++ .../Helpers/DummyEmailSender.cs | 27 ++++++ .../Logging/ElasticJsonFormatter.cs | 7 +- .../O2NextGen.Auth.Web.csproj | 7 ++ .../Pages/AccessDenied.cshtml | 19 +++++ .../Pages/AccessDenied.cshtml.cs | 12 +++ .../Pages/Account/Index.cshtml | 19 +++++ .../Pages/Account/Index.cshtml.cs | 12 +++ .../Pages/{Account => }/ConfirmEmail.cshtml | 0 .../{Account => }/ConfirmEmail.cshtml.cs | 0 .../Pages/{Account => }/Login.cshtml | 2 +- .../Pages/{Account => }/Login.cshtml.cs | 18 ++-- .../O2NextGen.Auth.Web/Pages/Logout.cshtml | 11 +++ .../O2NextGen.Auth.Web/Pages/Logout.cshtml.cs | 41 ++++++++++ .../Pages/{Account => }/Register.cshtml | 0 .../Pages/{Account => }/Register.cshtml.cs | 10 ++- .../auth/O2NextGen.Auth.Web/Startup.cs | 82 +++++++------------ .../auth/O2NextGen.Auth.Web/appsettings.json | 8 ++ 18 files changed, 236 insertions(+), 66 deletions(-) create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Extensions/ServiceCollectionExtensions.cs create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Helpers/DummyEmailSender.cs create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml.cs create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml.cs rename src/Services/auth/O2NextGen.Auth.Web/Pages/{Account => }/ConfirmEmail.cshtml (100%) rename src/Services/auth/O2NextGen.Auth.Web/Pages/{Account => }/ConfirmEmail.cshtml.cs (100%) rename src/Services/auth/O2NextGen.Auth.Web/Pages/{Account => }/Login.cshtml (97%) rename src/Services/auth/O2NextGen.Auth.Web/Pages/{Account => }/Login.cshtml.cs (83%) create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml create mode 100644 src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml.cs rename src/Services/auth/O2NextGen.Auth.Web/Pages/{Account => }/Register.cshtml (100%) rename src/Services/auth/O2NextGen.Auth.Web/Pages/{Account => }/Register.cshtml.cs (93%) diff --git a/src/Services/auth/O2NextGen.Auth.Web/Extensions/ServiceCollectionExtensions.cs b/src/Services/auth/O2NextGen.Auth.Web/Extensions/ServiceCollectionExtensions.cs new file mode 100644 index 00000000..b7952aef --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Extensions/ServiceCollectionExtensions.cs @@ -0,0 +1,27 @@ +using System; +using Microsoft.AspNetCore.Http; +using Microsoft.Extensions.Configuration; +using Microsoft.Extensions.DependencyInjection; +using O2NextGen.Auth.Web.Services; + +namespace O2NextGen.Auth.Web.Extensions +{ + public static class ServiceCollectionExtensions + { + public static IServiceCollection AddApplicationServices(this IServiceCollection services,IConfiguration configuration) + { + //register delegating handlers + // services.AddTransient(); + services.AddSingleton(); + + //register http services + services + .AddHttpClient("E-Sender", client => + { + client.BaseAddress = new Uri(configuration.GetValue("urls:ESenderUrl")); + }); + + return services; + } + } +} \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Helpers/DummyEmailSender.cs b/src/Services/auth/O2NextGen.Auth.Web/Helpers/DummyEmailSender.cs new file mode 100644 index 00000000..0d9bf391 --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Helpers/DummyEmailSender.cs @@ -0,0 +1,27 @@ +using System.Threading.Tasks; +using System.Web; +using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.Extensions.Logging; +using O2NextGen.Auth.Web.Services; + +namespace O2NextGen.Auth.Web.Helpers +{ + internal class DummyEmailSender : IEmailSender + { + private readonly ILogger _logger; + private readonly IESenderService _service; + + public DummyEmailSender(ILogger logger, IESenderService service) + { + _logger = logger; + _service = service; + } + public Task SendEmailAsync(string email, string subject, string htmlMessage) + { + _logger.LogWarning("EmailSender implementation is being used!!!!"); + _logger.LogWarning($"htmlMessage = { HttpUtility.HtmlDecode(htmlMessage)}"); + _service.Send(email,subject,htmlMessage); + return Task.CompletedTask; + } + } +} \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Logging/ElasticJsonFormatter.cs b/src/Services/auth/O2NextGen.Auth.Web/Logging/ElasticJsonFormatter.cs index cc174663..bb66de74 100644 --- a/src/Services/auth/O2NextGen.Auth.Web/Logging/ElasticJsonFormatter.cs +++ b/src/Services/auth/O2NextGen.Auth.Web/Logging/ElasticJsonFormatter.cs @@ -36,8 +36,11 @@ public void Format(LogEvent logEvent, TextWriter output) private void FormatBody(LogEvent logEvent, TextWriter output) { + using (var writer = new JsonTextWriter(output)) + { + + // add json body - var writer = new JsonTextWriter(output); writer.WriteStartObject(); // write level @@ -80,7 +83,7 @@ private void FormatBody(LogEvent logEvent, TextWriter output) writer.WriteEndObject(); writer.Flush(); - } + } } private static string FormatLogLevel(LogEventLevel level) { diff --git a/src/Services/auth/O2NextGen.Auth.Web/O2NextGen.Auth.Web.csproj b/src/Services/auth/O2NextGen.Auth.Web/O2NextGen.Auth.Web.csproj index 2456a634..9c19954c 100644 --- a/src/Services/auth/O2NextGen.Auth.Web/O2NextGen.Auth.Web.csproj +++ b/src/Services/auth/O2NextGen.Auth.Web/O2NextGen.Auth.Web.csproj @@ -36,6 +36,7 @@ ResXFileCodeGenerator Login.en.Designer.cs + @@ -62,14 +63,20 @@ True Login.en.resx + + + + + + diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml b/src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml new file mode 100644 index 00000000..4a22a1b8 --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml @@ -0,0 +1,19 @@ +@page +@model O2NextGen.Auth.Web.Pages.Account.AccessDenied + +@{ + Layout = null; +} + + + + + + + + +
+

Access Denied!!!s

+
+ + \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml.cs b/src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml.cs new file mode 100644 index 00000000..7e1acff4 --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/AccessDenied.cshtml.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace O2NextGen.Auth.Web.Pages.Account +{ + public class AccessDenied : PageModel + { + public void OnGet() + { + + } + } +} \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml b/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml new file mode 100644 index 00000000..a3ea5348 --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml @@ -0,0 +1,19 @@ +@page +@model O2NextGen.Auth.Web.Pages.Account.Index + +@{ + Layout = null; +} + + + + + + + + +
+ Test index page +
+ + \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml.cs b/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml.cs new file mode 100644 index 00000000..b38e15af --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Index.cshtml.cs @@ -0,0 +1,12 @@ +using Microsoft.AspNetCore.Mvc.RazorPages; + +namespace O2NextGen.Auth.Web.Pages.Account +{ + public class Index : PageModel + { + public void OnGet() + { + + } + } +} \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/ConfirmEmail.cshtml b/src/Services/auth/O2NextGen.Auth.Web/Pages/ConfirmEmail.cshtml similarity index 100% rename from src/Services/auth/O2NextGen.Auth.Web/Pages/Account/ConfirmEmail.cshtml rename to src/Services/auth/O2NextGen.Auth.Web/Pages/ConfirmEmail.cshtml diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/ConfirmEmail.cshtml.cs b/src/Services/auth/O2NextGen.Auth.Web/Pages/ConfirmEmail.cshtml.cs similarity index 100% rename from src/Services/auth/O2NextGen.Auth.Web/Pages/Account/ConfirmEmail.cshtml.cs rename to src/Services/auth/O2NextGen.Auth.Web/Pages/ConfirmEmail.cshtml.cs diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Login.cshtml b/src/Services/auth/O2NextGen.Auth.Web/Pages/Login.cshtml similarity index 97% rename from src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Login.cshtml rename to src/Services/auth/O2NextGen.Auth.Web/Pages/Login.cshtml index cf0b827b..9b7a314a 100644 --- a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Login.cshtml +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/Login.cshtml @@ -32,7 +32,7 @@
Or
-
+
signInManager, ILogger logge [BindProperty] public InputModel Input { get; set; } - public IList ExternalLogins { get; set; } + //Todo: will create to login with Fb, Vk, MicrosoftId, Google Account + // public IList ExternalLogins { get; set; } public string ReturnUrl { get; set; } @@ -63,10 +60,11 @@ public async Task OnGetAsync(string returnUrl = null) returnUrl = returnUrl ?? Url.Content("~/"); + //Todo: will create to login with Fb, Vk, MicrosoftId, Google Account // Clear the existing external cookie to ensure a clean login process - await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); - - ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); + // await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme); + + // ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList(); ReturnUrl = returnUrl; } @@ -83,7 +81,7 @@ public async Task OnPostAsync(string returnUrl = null) if (result.Succeeded) { _logger.LogInformation("User logged in."); - return LocalRedirect(returnUrl); + return Redirect(returnUrl); } if (result.RequiresTwoFactor) { diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml b/src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml new file mode 100644 index 00000000..a6c6b8ed --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml @@ -0,0 +1,11 @@ +@page +@model O2NextGen.Auth.Web.Pages.Account.LogoutModel + +@{ + Layout = null; +} + +
+

@ViewData["Title"]

+

You have successfully logged out of the application.

+
\ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml.cs b/src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml.cs new file mode 100644 index 00000000..28959cba --- /dev/null +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/Logout.cshtml.cs @@ -0,0 +1,41 @@ +using System.Threading.Tasks; +using Microsoft.AspNetCore.Authorization; +using Microsoft.AspNetCore.Identity; +using Microsoft.AspNetCore.Mvc; +using Microsoft.AspNetCore.Mvc.RazorPages; +using Microsoft.Extensions.Logging; +using O2NextGen.Auth.Web.Data; + +namespace O2NextGen.Auth.Web.Pages.Account +{ + [AllowAnonymous] + public class LogoutModel : PageModel + { + private readonly SignInManager _signInManager; + private readonly ILogger _logger; + + public LogoutModel(SignInManager signInManager, ILogger logger) + { + _signInManager = signInManager; + _logger = logger; + } + + public void OnGet() + { + } + + public async Task OnPost(string returnUrl = null) + { + await _signInManager.SignOutAsync(); + _logger.LogInformation("User logged out."); + if (returnUrl != null) + { + return Redirect(returnUrl); + } + else + { + return Page(); + } + } + } +} \ No newline at end of file diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Register.cshtml b/src/Services/auth/O2NextGen.Auth.Web/Pages/Register.cshtml similarity index 100% rename from src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Register.cshtml rename to src/Services/auth/O2NextGen.Auth.Web/Pages/Register.cshtml diff --git a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Register.cshtml.cs b/src/Services/auth/O2NextGen.Auth.Web/Pages/Register.cshtml.cs similarity index 93% rename from src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Register.cshtml.cs rename to src/Services/auth/O2NextGen.Auth.Web/Pages/Register.cshtml.cs index a784c8aa..19ef8499 100644 --- a/src/Services/auth/O2NextGen.Auth.Web/Pages/Account/Register.cshtml.cs +++ b/src/Services/auth/O2NextGen.Auth.Web/Pages/Register.cshtml.cs @@ -89,7 +89,15 @@ await _emailSender.SendEmailAsync(Input.Email, "Confirm your email", await _signInManager.SignInAsync(user, isPersistent: false); - return LocalRedirect(returnUrl); + if (string.IsNullOrWhiteSpace(returnUrl)) + { + LocalRedirect("~/"); + } + else + { + Redirect(returnUrl); + } + } foreach (var error in result.Errors) { diff --git a/src/Services/auth/O2NextGen.Auth.Web/Startup.cs b/src/Services/auth/O2NextGen.Auth.Web/Startup.cs index e2ce157e..21c3c6df 100644 --- a/src/Services/auth/O2NextGen.Auth.Web/Startup.cs +++ b/src/Services/auth/O2NextGen.Auth.Web/Startup.cs @@ -1,20 +1,16 @@ -using System; -using System.Text.Encodings.Web; -using System.Threading.Tasks; -using System.Web; +using System.Text.Encodings.Web; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; -using Microsoft.AspNetCore.Http; using Microsoft.AspNetCore.Identity; using Microsoft.AspNetCore.Identity.UI.Services; +using Microsoft.AspNetCore.Mvc; using Microsoft.EntityFrameworkCore; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; -using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; using O2NextGen.Auth.Web.Data; using O2NextGen.Auth.Web.Extensions; -using O2NextGen.Auth.Web.Services; +using O2NextGen.Auth.Web.Helpers; namespace O2NextGen.Auth.Web { @@ -28,20 +24,39 @@ public Startup(IConfiguration configuration) public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { - - services.AddConfiguredLocalization(); - + services.AddMvc() + .SetCompatibilityVersion(CompatibilityVersion.Version_2_2) + .AddRazorPagesOptions(options => + { + options.Conventions.AuthorizeFolder("/Account"); + } ); + services.AddDbContext(options => - options.UseSqlServer("Server=localhost;Initial Catalog=O2NextGen.AuthDb;Persist Security Info=False;User ID=sa;Password=your@Password;Connection Timeout=30;")); + options.UseSqlServer(Configuration["ConnectionString"])); + //Todo: will change vars to Auth-Config envs services - .AddIdentity() + .AddIdentity(options => + { + options.Password.RequireDigit = true; + options.Password.RequireLowercase = true; + options.Password.RequireNonAlphanumeric = false; + options.Password.RequireUppercase = true; + options.Password.RequiredLength = 6; + }) .AddEntityFrameworkStores() .AddDefaultTokenProviders(); services.AddApplicationServices(Configuration); - services.AddSingleton(); + services.ConfigureApplicationCookie(options => + { + options.LoginPath = "/Login"; + options.LogoutPath = "/Logout"; + options.AccessDeniedPath = "/AccessDenied"; + }); + services.AddConfiguredLocalization(); + services.AddSingleton(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env) @@ -55,47 +70,10 @@ public void Configure(IApplicationBuilder app, IHostingEnvironment env) var v = app.ApplicationServices .GetRequiredService>().Value; app.UseRequestLocalization(v); + app.UseCookiePolicy(); + app.UseAuthentication(); app.UseMvcWithDefaultRoute(); - - - } - } - - public static class ServiceCollectionExtensions - { - public static IServiceCollection AddApplicationServices(this IServiceCollection services,IConfiguration configuration) - { - //register delegating handlers - // services.AddTransient(); - services.AddSingleton(); - - //register http services - services - .AddHttpClient("E-Sender", client => - { - client.BaseAddress = new Uri(configuration.GetValue("urls:ESenderUrl")); - }); - - return services; - } - } - internal class DummyEmailSender : IEmailSender - { - private readonly ILogger _logger; - private readonly IESenderService _service; - - public DummyEmailSender(ILogger logger, IESenderService service) - { - _logger = logger; - _service = service; - } - public Task SendEmailAsync(string email, string subject, string htmlMessage) - { - _logger.LogWarning("EmailSender implementation is being used!!!!"); - _logger.LogWarning($"htmlMessage = { HttpUtility.HtmlDecode(htmlMessage)}"); - _service.Send(email,subject,htmlMessage); - return Task.CompletedTask; } } } diff --git a/src/Services/auth/O2NextGen.Auth.Web/appsettings.json b/src/Services/auth/O2NextGen.Auth.Web/appsettings.json index 5b50742f..98f6bbeb 100644 --- a/src/Services/auth/O2NextGen.Auth.Web/appsettings.json +++ b/src/Services/auth/O2NextGen.Auth.Web/appsettings.json @@ -27,5 +27,13 @@ "AllowedHosts": "*", "urls": { "ESenderUrl": "https://e-sender" + }, + "ConnectionString": "Server=localhost;Initial Catalog=O2NextGen.AuthDb;Persist Security Info=False;User ID=sa;Password=your@Password;Connection Timeout=30;", + "Auth-Config": { + "RequireDigit": "true", + "RequireLowercase": "true", + "RequireNonAlphanumeric": "true", + "RequireUppercase": "true", + "RequiredLength": 6 } }