This is what the browser shows me:
This is my _Layout
:
@using Microsoft.AspNetCore.Mvc.Localization
@inject IViewLocalizer _localizer
<!DOCTYPE html>
<html lang="ru">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>@_localizer[(string)ViewData["Title"]] - Accounter</title>
<link rel="apple-touch-icon" sizes="180x180" href="icons/apple-touch-icon.png">
<link rel="icon" type="image/png" sizes="32x32" href="icons/favicon-32x32.png">
<link rel="icon" type="image/png" sizes="16x16" href="icons/favicon-16x16.png">
<link rel="manifest" href="icons/site.webmanifest">
<link href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous"/>
<link rel="stylesheet" href="~/css/site.css"/>
<script src="~/lib/jquery/dist/jquery.min.js"></script>
<script src="~/lib/jquery-validation/dist/jquery.validate.min.js"></script>
<script src="~/lib/jquery-validation-unobtrusive/jquery.validate.unobtrusive.min.js"></script>
<link rel="stylesheet" href="https://kendo.cdn.telerik.com/2022.1.412/styles/kendo.bootstrap-v4.min.css"/>
<script src="https://kendo.cdn.telerik.com/2022.1.412/js/kendo.all.min.js"></script>
<script src="https://kendo.cdn.telerik.com/2022.1.412/js/kendo.aspnetmvc.min.js"></script>
</head>
<body>
<header>
<nav >
<div >
<a asp-action="Index">Tasker</a>
<div >
@if (User.Identity.IsAuthenticated)
{
<a asp-controller="Authentication" asp-action="LogOut">@_localizer["Log out"]</a>
}
else
{
<a asp-controller="Authentication" asp-action="Index">@_localizer["Log in"]</a>
}
</div>
<partial name="_SelectLanguagePartial"/>
</div>
</nav>
</header>
<div></div>
<div >
<div >
<main role="main">
@RenderBody()
</main>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
integrity="sha384-ka7Sk0Gln4gmtz2MlQnikT1wXgYsOg OMhuP IlRH9sENBO0LRn5q 8nbTov4 1p" crossorigin="anonymous"></script>
<script src="https://kit.fontawesome.com/cdc2f29cb5.js" crossorigin="anonymous"></script>
@await RenderSectionAsync("Scripts", false)
</body>
</html>
I require user to be authenticated and redirect to the authentication page in ConfigureServices
.
services.AddAuthorization(options =>
{
options.FallbackPolicy = new AuthorizationPolicyBuilder()
.RequireAuthenticatedUser()
.Build();
});
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Authentication";
options.SlidingExpiration = true;
});
AuthenticationController
:
public class AuthenticationController : Controller
{
private readonly IAuthenticationBL _authenticationBL;
public AuthenticationController(IAuthenticationBL authenticationBL)
{
_authenticationBL = authenticationBL;
}
public IActionResult Index()
{
return View();
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Register([Bind("Email", "Password")] AuthenticationVM vm)
{
if (!ModelState.IsValid) return View("Index", vm);
await _authenticationBL.RegisterAsync(vm.Email, vm.Password, Request);
return RedirectToAction("Index", "GeneralInformation");
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> LogIn([Bind("Email", "Password")] AuthenticationVM vm)
{
if (!ModelState.IsValid) return View("Index", vm);
await _authenticationBL.LogInAsync(vm.Email, vm.Password, User);
return RedirectToAction("Index", "GeneralInformation");
}
public async Task<IActionResult> LogOut()
{
await _authenticationBL.LogoutAsync(User);
return RedirectToAction("Index");
}
}
Authentication Index view:
@model Web.Models.VMs.AuthenticationVM
@inject IViewLocalizer _localizer
@using Microsoft.AspNetCore.Mvc.Localization
@{
ViewData["Title"] = "Authentication";
}
<div >
<div >
<h5 >@_localizer["Log in"]</h5>
<form asp-controller="Authentication" id="authentication-form">
<div asp-validation-summary="ModelOnly" ></div>
<div >
<input asp-for="Email" placeholder="Email" autocomplete="email">
<label asp-for="Email">@_localizer["Email"]</label>
<span asp-validation-for="Email" ></span>
</div>
<div >
<input asp-for="Password" placeholder="Password">
<label asp-for="Password">@_localizer["Password"]</label>
<i id="password-toggle"></i>
<span asp-validation-for="Password" ></span>
</div>
<div >
<button type="submit" asp-action="Register">@_localizer["Register"]</button>
<button type="submit" asp-action="LogIn">@_localizer["Log in"]</button>
</div>
</form>
</div>
</div>
@section Scripts {
<script src="~/js/authentication.js"></script>
}
CodePudding user response:
The problem was that I deleted the indication of default action in Startup
:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action}");
});
The framework wasn't able to understand what action to invoke on redirection to a controller, and I didn't get a page. Fix:
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
"default",
"{controller=Home}/{action=Index}");
});