当登录的用户请求访问不满足最低要求角色的操作方法时,错误操作方法从IExceptionHandlerPathFeature返回一个空对象,如果它在开发,登台或生产中运行,它也不会真正更改任何内容注意,当我尝试使用默认例外页面时,返回的例外页面为空白,现在我不得不使用自定义例外页面来避免这种情况,我不确定这是否以某种方式关联
Startup.cs
public class Startup
{
private IConfiguration _config;
public Startup(IConfiguration config)
{
_config = config;
}
public void ConfigureServices(IServiceCollection services)
{
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{ options.Password.RequiredLength = 8;
options.Password.RequireDigit = true;
options.Password.RequiredUniqueChars = 2;
options.Password.RequireNonAlphanumeric = true;
}).AddEntityFrameworkStores<AppDbContext>()/*.AddDefaultTokenProviders()*/;
services.AddDbContextPool<AppDbContext>(options => options.UseSqlServer(_config.GetConnectionString("EmployeeDBConnection")));
services.AddMvc(options=>
{
var policy = new AuthorizationPolicyBuilder().RequireAuthenticatedUser().Build();
options.Filters.Add(new AuthorizeFilter(policy));
}).AddXmlDataContractSerializerFormatters();
//services.AddSingleton<IEmployeeRepository, EmployeeRepository>();
services.AddScoped<IEmployeeRepository, SQLEmployeeRepository>();
services.AddSingleton<IDepartmentRepository, DepartmentRepository>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage(new DeveloperExceptionPageOptions()
{
SourceCodeLineCount = 0,
});
}
else
{
//app.UseStatusCodePagesWithReExecute("/Error/{0}").UseMiddleware<ErrorController>("/Error/{0}");
//app.UseStatusCodePagesWithReExecute("/Error/");
app.UseExceptionHandler("/Error");
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseMvc(route =>
{
route.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
app.UseStaticFiles();
}
}
ErrorController.cs
public class ErrorController : Controller
{
private readonly IHostingEnvironment hostingEnvironment;
private readonly ILogger<ErrorController> logger;
public ErrorController(IHostingEnvironment _hostingEnvironment, ILogger<ErrorController> _logger)
{
logger = _logger;
hostingEnvironment = _hostingEnvironment;
}
// GET: /<controller>/
[Route("Error/{statusCode}")]
public IActionResult HTTPCodeStatusHandler(int? statusCode)
{
if (statusCode != null && (((int)statusCode) >= int.MinValue && ((int)statusCode) <= int.MaxValue))
{
if (hostingEnvironment.IsDevelopment())
{
return View($"~/Views/shared/error{statusCode}.cshtml");
}
else
{
var statusCodeResult = HttpContext.Features.Get<IStatusCodeReExecuteFeature>();
if (statusCode == 404)
{
ViewBag.ErrorMessage = "Sorry, the page you have requested cannot be found";
if (statusCodeResult != null)
{
//ViewBag.Path = statusCodeResult.OriginalPath;
//ViewBag.QS = statusCodeResult.OriginalQueryString;
logger.LogWarning($"404 Error ocurred.Path :{((statusCodeResult.OriginalPath is "/") ? "/Home": statusCodeResult.OriginalPath )} And Query String {((statusCodeResult.OriginalQueryString is null) ? "/Home":statusCodeResult.OriginalQueryString) }");
}
return View($"~/Views/shared/error{statusCode}.cshtml");
}
}
return View();
}
return Error();
}
[Route("Error")]
[AllowAnonymous]
[HttpGet]
public IActionResult Error()
{
var exceptionDetails = HttpContext.Features.Get<IExceptionHandlerPathFeature>();
logger.LogError($"The path {exceptionDetails.Path} threw andexception {exceptionDetails.Error}");
ViewBag.ExceptionPath = exceptionDetails.Path;
ViewBag.ExceptionMessage = exceptionDetails.Error.Message;
ViewBag.StackTrace = exceptionDetails.Error.StackTrace;
return View("~/Views/shared/Error.cshtml");
}
}
答案 0 :(得分:-1)
配置可解决此问题的cookie AccessDenied Path选项
services.ConfigureApplicationCookie(options =>
{
options.AccessDeniedPath = new PathString("/Account/AccessDenied");
});