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.