0

I develop .NET CORE 3.0 Web Application. The Application is has a multilayer as a project in a single solution. Login page in a UI layer. But this controller code in another layer, this controller has a Authorize and Roles Attribute control on the top. Like this:

Controller side:

namespace mytestsolution.ProjectX.Controllers
{
    [Authorize]
    [Authorize(Roles = "ProjectXAdmin")]
    public class AdminHomeController : Controller
    {
      public AdminHomeController( IHttpContextAccessor iHttpContextAccessor, IConfiguration configuration) 
        {
            _configuration = configuration;
            _httpContextAccessor = iHttpContextAccessor;
        }
       
       public IActionResult Index(){....}
    }
}

startup:

namespace mytestsolution.ProjectX
{
   public class Startup
   {
      public Startup(IConfiguration configuration)
      {
        Configuration = configuration;
      }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
          
           app.UseCors(options => options.AllowAnyOrigin());
            app.UseSession();

            var options = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
            app.UseRequestLocalization(options.Value);
              if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");

            }

            app.UseAuthentication();
            app.UseAuthorization();

            app.UseStaticFiles();
            app.UseRouting();

            app.UseMvcWithDefaultRoute();

            app.UseHttpsRedirection();  

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=AdminHome}/{action=Index}/{id?}");
            });

            app.UseCookiePolicy();
        }

   }
}

I want to when unauthorized and authentication from tried to access via link (in locally example: localhost:50111/AdminHome/Index), redirect to login page. My login page another subproject also. Solution project structure is:

---mytestsolution.ProjectX
   --Model
   --View
   --Controller
   --...others...
---mytestsolution.ProjectY
   --Model
   --View
   --Controller
   --...others...
---mytestsolution.UI
   --Model
   --View
   --Controller
     --AccountController
   --...others...
---mytestsolution.Model
---mytestsolution.Data
---mytestsolution.API

But now, when I tried to logout and then access to http:// localhost:50111/AdminHome/Index , it gives me 401 error, when I tried has no AdminRole user login then access to http:// localhost:50111/AdminHome/Index , it gives me 403 error, But I want to try when this error occured, redirected to login page in .net core 3.0 project.

hkyaaa
  • 100
  • 9
  • This is what you are lookin for ? [Add a notify URL](https://stackoverflow.com/questions/2504923/how-to-redirect-authorize-to-loginurl-only-when-roles-are-not-used) – Abubakar Iftikhar Oct 20 '21 at 08:29
  • I took a look but unfortunately not suitable for my sissue, because their usage different from .net core 3.0 version. When I include neccessary Packages(ie: Microsoft.AspNet.Mvc ) maximal compatible version is 5.0.0., but some code part couldnt run this time. Such as: AuthorizeCore, HttpContext, HttpCachePolicyBase,CacheValidateHandler @AbubakarIftikhar – hkyaaa Oct 20 '21 at 11:28

0 Answers0