Hi,
How we can ridirect the current page When user access the Unauthorized content.
Index Page
When I click on the "Click" button in Index Page, Then It will redirect to Home controller.
<div class="row"><div class="col-md-12"><h1>Welcome to ASP.NET Core Identity !!</h1><p>Security !!</p><a asp-controller="Home" asp-action="Home" class="btn btn-success">Click</a></div></div>
Home Controller
We already set Authorize in Home Action in Home Controller. So the click event will fire Home Action method. But it showing blank page !! 401
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Mvc; using RegistrationForm.Models; namespace RegistrationForm.Controllers.WebApp { public class HomeController : Controller { private IRegRepository _repository; public HomeController(IRegRepository repository) { _repository = repository; } [Authorize] public IActionResult Home() { var data = _repository.GetAllRegistrations(); return View(data); } public IActionResult Index() { return View(); } } }
Startup.cs
using System; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using RegistrationForm.Models; using Microsoft.Extensions.Configuration; using Microsoft.AspNetCore.Identity.EntityFrameworkCore; using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Http; namespace RegistrationForm { public class Startup { private IConfigurationRoot _config; private IHostingEnvironment _env; public Startup(IHostingEnvironment env) { _env = env; var ConfigBuilder = new ConfigurationBuilder().SetBasePath(env.ContentRootPath) .AddJsonFile("appsettings.json"); _config = ConfigBuilder.Build(); } // This method gets called by the runtime. Use this method to add services to the container. // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940 public void ConfigureServices(IServiceCollection services) { services.AddIdentity<RegistrationUser, IdentityRole>(config => { config.User.RequireUniqueEmail = true; config.Password.RequiredLength = 8; config.Cookies.ApplicationCookie.LoginPath = "/Auth/login"; }).AddEntityFrameworkStores<RegContext>(); services.AddMvc(config => { if (_env.IsProduction()) { config.Filters.Add(new RequireHttpsAttribute()); } }); services.AddSingleton(_config); services.AddEntityFrameworkSqlServer().AddDbContext<RegContext>(); services.AddScoped<IRegRepository, RegRepository>(); services.AddTransient<RegContextSeedData>(); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, RegContextSeedData seeder) { loggerFactory.AddConsole(); app.UseIdentity(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseStaticFiles(); app.UseMvc(config => { config.MapRoute( name: "Default", template: "{controller}/{action}/{id?}", defaults: new { controller = "Home", action = "Index" } ); }); try { seeder.SeedData().Wait(); } catch (Exception ex) { throw ex; } } } }