Browse Source

another logout fix

ganahrhr 3 years ago
parent
commit
87a07f8a9b

+ 17 - 11
Areas/Identity/Pages/Account/LogOut.cshtml

@@ -1,15 +1,21 @@
 @page
-@using Microsoft.AspNetCore.Identity
-@attribute [IgnoreAntiforgeryToken]
-@inject SignInManager<IdentityUser> SignInManager
-@functions {
-    public async Task<IActionResult> OnPost()
-    {
-        if (SignInManager.IsSignedIn(User))
+@model LogoutModel
+@{
+    ViewData["Title"] = "Log out";
+}
+
+<header>
+    <h1>@ViewData["Title"]</h1>
+    @{
+        if (User.Identity.IsAuthenticated)
         {
-            await SignInManager.SignOutAsync();
+            <form class="form-inline" asp-area="Identity" asp-page="/Account/Logout" asp-route-returnUrl="@Url.Page("/", new { area = "" })" method="post">
+                <button type="submit" class="nav-link btn btn-link text-dark">Click here to Logout</button>
+            </form>
+        }
+        else
+        {
+            <p>You have successfully logged out of the application.</p>
         }
-
-        return Redirect("~/");
     }
-}
+</header>

+ 55 - 0
Areas/Identity/Pages/Account/Login.cshtml

@@ -0,0 +1,55 @@
+@page
+@model LoginModel
+
+@{
+    ViewData["Title"] = "Log in";
+}
+
+<h1>@ViewData["Title"]</h1>
+<div class="row">
+    <div class="col-md-4">
+        <section>
+            <form id="account" method="post">
+                <h4>Use a local account to log in.</h4>
+                <hr />
+                <div asp-validation-summary="All" class="text-danger"></div>
+                <div class="form-group">
+                    <label asp-for="Input.Email"></label>
+                    <input asp-for="Input.Email" class="form-control" />
+                    <span asp-validation-for="Input.Email" class="text-danger"></span>
+                </div>
+                <div class="form-group">
+                    <label asp-for="Input.Password"></label>
+                    <input asp-for="Input.Password" class="form-control" />
+                    <span asp-validation-for="Input.Password" class="text-danger"></span>
+                </div>
+                <div class="form-group">
+                    <div class="checkbox">
+                        <label asp-for="Input.RememberMe">
+                            <input asp-for="Input.RememberMe" />
+                            @Html.DisplayNameFor(m => m.Input.RememberMe)
+                        </label>
+                    </div>
+                </div>
+                <div class="form-group">
+                    <button type="submit" class="btn btn-primary">Log in</button>
+                </div>
+                <div class="form-group">
+                    <p>
+                        <a id="forgot-password" asp-page="./ForgotPassword">Forgot your password?</a>
+                    </p>
+                    <p>
+                        <a asp-page="./Register" asp-route-returnUrl="@Model.ReturnUrl">Register as a new user</a>
+                    </p>
+                    <p>
+                        <a id="resend-confirmation" asp-page="./ResendEmailConfirmation">Resend email confirmation</a>
+                    </p>
+                </div>
+            </form>
+        </section>
+    </div>    
+</div>
+
+@section Scripts {
+    <partial name="_ValidationScriptsPartial" />
+}

+ 110 - 0
Areas/Identity/Pages/Account/Login.cshtml.cs

@@ -0,0 +1,110 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel.DataAnnotations;
+using System.Linq;
+using System.Text.Encodings.Web;
+using System.Threading.Tasks;
+using Microsoft.AspNetCore.Authorization;
+using Microsoft.AspNetCore.Authentication;
+using Microsoft.AspNetCore.Identity;
+using Microsoft.AspNetCore.Identity.UI.Services;
+using Microsoft.AspNetCore.Mvc;
+using Microsoft.AspNetCore.Mvc.RazorPages;
+using Microsoft.Extensions.Logging;
+
+namespace HyperCube.Areas.Identity.Pages.Account
+{
+    [AllowAnonymous]
+    public class LoginModel : PageModel
+    {
+        private readonly UserManager<IdentityUser> _userManager;
+        private readonly SignInManager<IdentityUser> _signInManager;
+        private readonly ILogger<LoginModel> _logger;
+
+        public LoginModel(SignInManager<IdentityUser> signInManager, 
+            ILogger<LoginModel> logger,
+            UserManager<IdentityUser> userManager)
+        {
+            _userManager = userManager;
+            _signInManager = signInManager;
+            _logger = logger;
+        }
+
+        [BindProperty]
+        public InputModel Input { get; set; }
+
+        //public IList<AuthenticationScheme> ExternalLogins { get; set; }
+
+        public string ReturnUrl { get; set; }
+
+        [TempData]
+        public string ErrorMessage { get; set; }
+
+        public class InputModel
+        {
+            [Required]
+            [EmailAddress]
+            public string Email { get; set; }
+
+            [Required]
+            [DataType(DataType.Password)]
+            public string Password { get; set; }
+
+            [Display(Name = "Remember me?")]
+            public bool RememberMe { get; set; }
+        }
+
+        public async Task OnGetAsync(string returnUrl = null)
+        {
+            if (!string.IsNullOrEmpty(ErrorMessage))
+            {
+                ModelState.AddModelError(string.Empty, ErrorMessage);
+            }
+
+            returnUrl ??= Url.Content("~/");
+
+            // Clear the existing external cookie to ensure a clean login process
+            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
+
+            //ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
+
+            ReturnUrl = returnUrl;
+        }
+
+        public async Task<IActionResult> OnPostAsync(string returnUrl = null)
+        {
+            returnUrl ??= Url.Content("~/");
+
+            //ExternalLogins = (await _signInManager.GetExternalAuthenticationSchemesAsync()).ToList();
+        
+            if (ModelState.IsValid)
+            {
+                // This doesn't count login failures towards account lockout
+                // To enable password failures to trigger account lockout, set lockoutOnFailure: true
+                var result = await _signInManager.PasswordSignInAsync(Input.Email, Input.Password, Input.RememberMe, lockoutOnFailure: false);
+                if (result.Succeeded)
+                {
+                    _logger.LogInformation("User logged in.");
+                    return LocalRedirect(returnUrl);
+                }
+                if (result.RequiresTwoFactor)
+                {
+                    return RedirectToPage("./LoginWith2fa", new { ReturnUrl = returnUrl, RememberMe = Input.RememberMe });
+                }
+                if (result.IsLockedOut)
+                {
+                    _logger.LogWarning("User account locked out.");
+                    return RedirectToPage("./Lockout");
+                }
+                else
+                {
+                    ModelState.AddModelError(string.Empty, "Invalid login attempt.");
+                    return Page();
+                }
+            }
+
+            // If we got this far, something failed, redisplay form
+            return Page();
+        }
+    }
+}

+ 35 - 0
Areas/Identity/Pages/Account/Logout.cshtml.cs

@@ -0,0 +1,35 @@
+using System;
+using System.Collections.Generic;
+using System.Diagnostics;
+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 Console = HyperCube.Utils.AdvConsole;
+
+namespace HyperCube.Areas.Identity.Pages.Account
+{
+    [AllowAnonymous]
+    public class LogoutModel : PageModel
+    {
+        private readonly SignInManager<IdentityUser> _signInManager;
+        //private readonly ILogger<LogoutModel> _logger;
+
+        public LogoutModel(SignInManager<IdentityUser> signInManager, ILogger<LogoutModel> logger)
+        {
+            _signInManager = signInManager;
+            //_logger = logger;
+        }
+
+        public async Task<IActionResult> OnGet()
+        {
+            await _signInManager.SignOutAsync();
+            //_logger.LogInformation("User logged out.");
+            Console.WriteLine("User logged out.");
+
+            return LocalRedirect("~/");
+        }       
+    }
+}

+ 25 - 5
Pages/Index.razor

@@ -17,10 +17,10 @@
                     <a href="Identity/Account/Register" class="nav__link">Регистрация</a>
                 </nav>
                 <form class="login-form">
-                    <input type="text" class="form-control login-form__input">
-                    <input type="text" class="form-control login-form__input">
-                    @*<button type="button" class="login-form__btn" @onclick="@Login">Войти</button>*@
-                    <a href="Identity/Account/Login" class="login-form__btn">Войти</a>
+                    @*<input type="text" class="form-control login-form__input" >
+                    <input type="password" class="form-control login-form__input">*@
+                    <button type="button" class="login-form__btn" @onclick="@Login">Войти</button>
+                    @*<a href="Identity/Account/Login" class="login-form__btn">Войти</a>*@
                 </form>
             </header>
 
@@ -78,4 +78,24 @@
             </footer>
         </div>
     </NotAuthorized>
-</AuthorizeView>
+</AuthorizeView>
+
+@code{
+
+    //string email, pass;
+
+    async Task Login()
+    {
+        /// redirecting to asp login page
+        NavigationManager.NavigateTo("Identity/Account/Login", true);
+
+        //var result = await SignInManager.PasswordSignInAsync(email, pass, false, lockoutOnFailure: false);
+        //if (result.Succeeded)
+        //{
+        //    Console.WriteLine("User logged in.");
+
+        //    /// redirecting to asp login page
+        //    NavigationManager.NavigateTo("Identity/Account/Login", true);
+
+    }
+}

+ 11 - 9
Pages/_Host.cshtml

@@ -22,16 +22,18 @@
     <component type="typeof(App)" render-mode="Server" />
 
     @*<div id="blazor-error-ui">
-        <environment include="Staging,Production">
-            An error has occurred. This application may no longer respond until reloaded.
-        </environment>
-        <environment include="Development">
-            An unhandled exception has occurred. See browser dev tools for details.
-        </environment>
-        <a href="" class="reload">Reload</a>
-        <a class="dismiss">🗙</a>
-    </div>*@
+            <environment include="Staging,Production">
+                An error has occurred. This application may no longer respond until reloaded.
+            </environment>
+            <environment include="Development">
+                An unhandled exception has occurred. See browser dev tools for details.
+            </environment>
+            <a href="" class="reload">Reload</a>
+            <a class="dismiss">🗙</a>
+        </div>*@
 
     <script src="_framework/blazor.server.js"></script>
+    @*<script src="~/js/swiper-bundle.min.js"></script>
+    <script src="~/js/script.js"></script>*@
 </body>
 </html>

+ 13 - 0
wwwroot/js/script.js

@@ -0,0 +1,13 @@
+const swiper = new Swiper('.swiper-container', {
+  pagination: {
+    el: '.slider__pagination',
+    clickable: true,
+    bulletClass: 'slider__pagination-bullet',
+    bulletActiveClass: 'slider__pagination-bullet_active'
+  },
+  navigation: {
+    lockClass: 'slider__btn_locked',
+    nextEl: '.slider__btn-next',
+    prevEl: '.slider__btn-prev'
+  },
+});

+ 0 - 1
wwwroot/js/script.min.js

@@ -1 +0,0 @@
-const swiper=new Swiper(".swiper-container",{pagination:{el:".slider__pagination",clickable:true,bulletClass:"slider__pagination-bullet",bulletActiveClass:"slider__pagination-bullet_active"},navigation:{lockClass:"slider__btn_locked",nextEl:".slider__btn-next",prevEl:".slider__btn-prev"}});