1

I spent quite a bit of time on this. So, I think I'm making a fundamental mistake.

It's easy to explain, but I'm not sure how much code I need to provide.

It's .NET Core 7, MVC, where the Base Controller overrides OnActionExecutionAsync, and additionally methods that are decorated with an ActionFilterAttribute, that also overrides OnActionExecutionAsync.

Everything was working as expected with AddIdentity, and all I did was try replacing it with AddDefaultIdentity:

Before (service initializer code):

services.AddIdentity<ApplicationUser, ApplicationRole>(o =>
    {
        o.User.RequireUniqueEmail = true;
    })
    .AddEntityFrameworkStores<SocraticMembershipsContext>()
    .AddUserStore<ApplicationUserStore>()
    .AddRoleStore<ApplicationRoleStore>()
    .AddUserManager<ApplicationUserManager>()
    .AddRoleManager<ApplicationRoleManager>()
    .AddSignInManager<ApplicationSignInManager>()
    .AddDefaultTokenProviders();

After (service initializer code):

services.AddDefaultIdentity<ApplicationUser>(o =>
{
    o.User.RequireUniqueEmail = true;
})
   .AddRoles<ApplicationRole>()
   .AddEntityFrameworkStores<SocraticMembershipsContext>()
   .AddUserStore<ApplicationUserStore>()
   .AddRoleStore<ApplicationRoleStore>()
   .AddUserManager<ApplicationUserManager>()
   .AddRoleManager<ApplicationRoleManager>()
   .AddSignInManager<ApplicationSignInManager>();
   //.AddDefaultTokenProviders(); 

<-- not sure this needed to be commented out, but I read it wasn't used...

Another change I needed to make was with AddAuthentication: Before:

services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)

After:

services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})

Upon successful login, this code is executed:

    private ActionResult RedirectToLocal(string returnUrl)
    {
        if (returnUrl != null && (Url.IsLocalUrl(returnUrl)
            || returnUrl.Contains(Host))) // IsLocalUrl doesn't qualify the entire url (including http...) which seems to be needed for multi-tenant app.
        {
            return Redirect(returnUrl);
        }
        else
        {
            RedirectToActionResult res = Redirect("Index", "Home");
            **return res;**
        }
    }

Which directs the user to the Home controller's Index, which looks like this:

**[ServiceFilter(typeof(NavigationAttribute))]**
public async Task<IActionResult> Index()
{
    return await GetActionResultAsync(GetPageAsync);
}

NavigationAttribute is an ActionFilterAttribute and OnActionExecutionAsync starts off like this:

 public override async Task OnActionExecutionAsync(ActionExecutingContext filterContext, ActionExecutionDelegate next)
 {
     NullCheck.ValidateIsNotNull(filterContext, "filterContext");
     **var baseController = ((BaseController)filterContext.Controller);**
     _langId = baseController.LangId;
     var currentPanelSetting = await baseController.GetCurrentPanelSettingAsync();
     var currentPanel = baseController.CurrentPanel;

...

Before, using AddIdentity, when RedirectToLocal() returned its RedirectToActionResult, the baseController variable was Home controller, as expected, since this is the controller that is decorated with NavigationAttribute, at this point.

After, using AddDefaultIdentity, it remains as the original controller which was redirecting to Home. I don't understand why making such a small change would have such a big impact. So, it never loads the Home page.

I tried to replace AddIdentity with AddDefaultIdentity, and my understanding is that the application would behave the same way.

1
  • I think I might have a clue. The HomeController is decorated with [Authorize]. So it seems that the attempt to get to HomeController's Index failed and the user was returned to the Login screen. It must be the roles are not consistent, and I have to rework that part. Commented Jan 5, 2024 at 0:09

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.