-1

My project was rebuilt into folders called "area", everything worked before and the user was authorized to the account management panel. After adding these areas, the project cannot see where it should go.

My structure project:

enter image description here

Structure in Area "Authorize":

enter image description here

I tried to work with a route for areas in Startup.cs:

...
            services.AddMvc( options =>
            options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
...
            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=Home}/{action=Index}/{id?}");
                routes.MapAreaRoute(
                    name: "MyAreaAuthorize",
                    areaName: "Authorize",
                    template: "Authorize/{controller=Account}/{action=Index}/{id?}");
                routes.MapAreaRoute(
                    name: "MyAreaManage",
                    areaName: "Authorize",
                    template: "Authorize/{controller=Manage}/{action=Index}/{id?}");
                routes.MapAreaRoute(
                    name: "MyAreaAdminAccount",
                    areaName: "AdminAccount",
                    template: "AdminAccount/{controller=Admin}/{action=Index}/{id?}");
            });

I also copied _ViewImports.cshtml to folders with views for each area.

My file looks like this:

@using Example
@using Example.Models
@using Example.AccountArea.Models
@using Microsoft.AspNetCore.Identity
@addTagHelper Example.TagHelpers.*, Example
@addTagHelper Identity.TagHelpers.*, Identity
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers

View in Area "Authorize":

@using Example.AccountArea.Models;
@using Microsoft.AspNetCore.Identity;
@model Example.AccountArea.Models.AccountModel.LoginModel;
@inject SignInManager<UserApp> Sign
@{
    ViewData["Title"] = "Zaloguj się";
}

<form asp-area="Authorize" asp-controller="Account" asp-action="Login" method="post">
                <div asp-validation-summary="ModelOnly" class="text-danger"></div>
                <div>
                    <input asp-for="Email" placeholder="Email" />
                    <span asp-validation-for="Email" class="text-danger margin-text-form"></span>
                </div>
                <div>
                    <input asp-for="Password" placeholder="Hasło" />
                    <span asp-validation-for="Password" class="text-danger margin-text-form"></span>
                </div>
                <div class="group">
                    <div class="checkbox">
                        <input type='checkbox' id='my-checkbox'>
                        <label for='my-checkbox'>@Html.DisplayNameFor(m => m.RememberMe)</label>
                    </div>
                </div>
                <div class="form-group">
                    <button type="submit" class="btn-dark">Login</button>
                </div>
                <div class="group">
                    <p>
                        <a asp-area="Authorize" asp-controller="Account" asp-action="ForgotPassword">Forgot?</a>
                    </p>
                    <p>
                        <a asp-controller="Account" asp-action="Register">Register</a>
                    </p>
                </div>
            </form>
        </section>
</div>

@section Scripts {
    <partial name="_ValidationScriptsPartial" />
}

Controller Account in Area "Authorize:

The controller has included info for the class that it belongs to the "Authorize" area

using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Authentication;
using System;
using Example.AccountArea.Models;
using Example.AccountArea.Models.AccountModel;

namespace Example.AccountArea.Controllers
{
    [Area("Authorize")]
    public class AccountController : Controller
    {
        ...My code is irrelevant to the problem...

        [HttpGet]
        [AllowAnonymous]
        public async Task<IActionResult> Login()
        {
            await HttpContext.SignOutAsync(IdentityConstants.ExternalScheme);
            return View();
        }

        [HttpPost]
        [AllowAnonymous]
        [ValidateAntiForgeryToken]
        public async Task<IActionResult> Login(LoginModel login)
        {
            if (ModelState.IsValid)
            {
                var result = await _signInManager.PasswordSignInAsync(login.Email, login.Password, login.RememberMe, false);
                if (result.Succeeded)
                {
                    _logger.LogInformation("...");
                    return RedirectToAction(nameof(ManageController.Index), "Manage");
                }
                if (result.IsLockedOut)
                {
                    _logger.LogWarning("...");
                    return RedirectToAction(nameof(LockoutOptions));
                }
                else
                {
                    ModelState.AddModelError(string.Empty, "...");
                    return View("../Home/Index");
                }
            }

            // If we got this far, something failed, redisplay form
            return View("../Home/Index", login);
        }

        ...My code is irrelevant to the problem...
    }
}

After starting the application, the login form is displayed to me, the links do not work, the message is similar to the one below, it does not pass data validation and after clicking "Log in" it displays:

enter image description here

Physical transition of pages on the browser:

From login:

enter image description here

To:

enter image description here

Please do not inform me that it is copied from and there are similar solutions because I tried to find solutions in other similar posts first!

Any ideas ? Thank you if you help me..

  • The login page URL is now located at localhost:44340/Authorize/Account/Login. You'll need to update your code so it knows the new location of the login page. – phuzi Oct 04 '19 at 09:03
  • @phuzi, where ? in View, Controller ? Startup ? –  Oct 04 '19 at 10:18
  • not exactly sure but `services.ConfigureApplicationCookie(options => options.LoginPath = "/Authorize/Account/Login");` seems like a reasonable place (startup.cs / ConfigureServices) assuming you're using auth cookies – phuzi Oct 04 '19 at 11:51
  • @DominikN: can you check if there is a reference to you login page in the web.config (if you have any). In some legacy projects there can be behaviour defined to redirect to a login page which might be moved. – Stefan Oct 04 '19 at 13:30
  • @Stefan unfortunately I don't have web.config –  Oct 04 '19 at 15:41

1 Answers1

0

You do have two Area routes for your Authorize with different default controllers... I believe you should only have one. Not sure if that's the cause of the issue though. But I think that would likely conflict, it also looks unnecessary because any controllers in that area will be included by default with the single route.

My aspnet core 2.2 app area registration looks like this:

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "Admin",
                template: "{area:exists}/{controller=Admin}/{action=Index}/{id?}");
...
Topher
  • 1,011
  • 11
  • 19