1
0
mirror of https://git.teknik.io/Teknikode/Teknik.git synced 2023-08-02 14:16:22 +02:00

Fixed 404/403 errors redirecting to error page instead of loading it within the same Request context to keep URL intact.

This commit is contained in:
Uncled1023 2018-11-19 21:07:56 -08:00
parent 62e4476897
commit 91108e2c31
6 changed files with 34 additions and 25 deletions

View File

@ -18,6 +18,7 @@ using Teknik.Utilities;
using Teknik.ViewModels; using Teknik.ViewModels;
using Teknik.Logging; using Teknik.Logging;
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
namespace Teknik.Areas.Admin.Controllers namespace Teknik.Areas.Admin.Controllers
{ {
@ -58,7 +59,7 @@ namespace Teknik.Areas.Admin.Controllers
model.AccountStatus = info.AccountStatus.Value; model.AccountStatus = info.AccountStatus.Value;
return View(model); return View(model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpGet] [HttpGet]
@ -137,7 +138,7 @@ namespace Teknik.Areas.Admin.Controllers
await UserHelper.EditAccountType(_dbContext, _config, username, accountType); await UserHelper.EditAccountType(_dbContext, _config, username, accountType);
return Json(new { result = new { success = true } }); return Json(new { result = new { success = true } });
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpPost] [HttpPost]
@ -150,7 +151,7 @@ namespace Teknik.Areas.Admin.Controllers
await UserHelper.EditAccountStatus(_dbContext, _config, username, accountStatus); await UserHelper.EditAccountStatus(_dbContext, _config, username, accountStatus);
return Json(new { result = new { success = true } }); return Json(new { result = new { success = true } });
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpPost] [HttpPost]
@ -169,7 +170,7 @@ namespace Teknik.Areas.Admin.Controllers
return Json(new { result = new { code = inviteCode.Code } }); return Json(new { result = new { code = inviteCode.Code } });
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpPost] [HttpPost]

View File

@ -27,6 +27,7 @@ namespace Teknik.Areas.Error.Controllers
{ {
public ErrorController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { } public ErrorController(ILogger<Logger> logger, Config config, TeknikEntities dbContext) : base(logger, config, dbContext) { }
[AllowAnonymous]
public IActionResult HttpError(int statusCode) public IActionResult HttpError(int statusCode)
{ {
switch (statusCode) switch (statusCode)
@ -42,6 +43,7 @@ namespace Teknik.Areas.Error.Controllers
} }
} }
[AllowAnonymous]
public IActionResult HttpGeneral(int statusCode) public IActionResult HttpGeneral(int statusCode)
{ {
ViewBag.Title = statusCode + " - " + _config.Title; ViewBag.Title = statusCode + " - " + _config.Title;

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
using Teknik.Areas.Help.ViewModels; using Teknik.Areas.Help.ViewModels;
@ -39,7 +40,7 @@ namespace Teknik.Areas.Help.Controllers
ViewBag.Title = service + " API " + version + " Help - " + _config.Title; ViewBag.Title = service + " API " + version + " Help - " + _config.Title;
return View("~/Areas/Help/Views/Help/API/" + version + "/" + service + ".cshtml", model); return View("~/Areas/Help/Views/Help/API/" + version + "/" + service + ".cshtml", model);
} }
return RedirectToRoute("Error.Http404"); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[AllowAnonymous] [AllowAnonymous]

View File

@ -1,4 +1,5 @@
using Microsoft.AspNetCore.Authorization; using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc; using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.Logging; using Microsoft.Extensions.Logging;
@ -41,7 +42,7 @@ namespace Teknik.Areas.Shortener.Controllers
_dbContext.SaveChanges(); _dbContext.SaveChanges();
return Redirect(shortUrl.OriginalUrl); return Redirect(shortUrl.OriginalUrl);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpPost] [HttpPost]

View File

@ -143,7 +143,7 @@ namespace Teknik.Areas.Upload.Controllers
[HttpGet] [HttpGet]
[AllowAnonymous] [AllowAnonymous]
[ResponseCache(Duration = 31536000, Location = ResponseCacheLocation.Any)] [ResponseCache(Duration = 31536000, Location = ResponseCacheLocation.Any)]
public IActionResult Download(string file) public async Task<IActionResult> Download(string file)
{ {
if (_config.UploadConfig.DownloadEnabled) if (_config.UploadConfig.DownloadEnabled)
{ {
@ -171,16 +171,20 @@ namespace Teknik.Areas.Upload.Controllers
contentType = uploads.ContentType; contentType = uploads.ContentType;
contentLength = uploads.ContentLength; contentLength = uploads.ContentLength;
dateUploaded = uploads.DateUploaded; dateUploaded = uploads.DateUploaded;
//if (User.Identity.IsAuthenticated) if (User.Identity.IsAuthenticated)
//{ {
// User user = UserHelper.GetUser(_dbContext, User.Identity.Name); IdentityUserInfo userInfo = await IdentityHelper.GetIdentityUserInfo(_config, User.Identity.Name);
// premiumAccount = user.AccountType == AccountType.Premium; premiumAccount = userInfo.AccountType == AccountType.Premium;
//} }
//premiumAccount |= (uploads.User != null && uploads.User.AccountType == AccountType.Premium); if (!premiumAccount && uploads.User != null)
{
IdentityUserInfo userInfo = await IdentityHelper.GetIdentityUserInfo(_config, uploads.User.Username);
premiumAccount = userInfo.AccountType == AccountType.Premium;
}
} }
else else
{ {
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
// We don't have the key, so we need to decrypt it client side // We don't have the key, so we need to decrypt it client side
@ -353,10 +357,10 @@ namespace Teknik.Areas.Upload.Controllers
} }
} }
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
[HttpPost] [HttpPost]
@ -440,7 +444,7 @@ namespace Teknik.Areas.Upload.Controllers
} }
return View(model); return View(model);
} }
return RedirectToRoute("Error.Http404"); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpPost] [HttpPost]

View File

@ -340,7 +340,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/ProfileSettings.cshtml", model); return View("/Areas/User/Views/User/Settings/ProfileSettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
public IActionResult AccountSettings() public IActionResult AccountSettings()
@ -361,7 +361,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/AccountSettings.cshtml", model); return View("/Areas/User/Views/User/Settings/AccountSettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
public async Task<IActionResult> SecuritySettings() public async Task<IActionResult> SecuritySettings()
@ -403,7 +403,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/SecuritySettings.cshtml", model); return View("/Areas/User/Views/User/Settings/SecuritySettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
public IActionResult AccessTokenSettings() public IActionResult AccessTokenSettings()
@ -435,7 +435,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/AccessTokenSettings.cshtml", model); return View("/Areas/User/Views/User/Settings/AccessTokenSettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
public IActionResult InviteSettings() public IActionResult InviteSettings()
@ -480,7 +480,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/InviteSettings.cshtml", model); return View("/Areas/User/Views/User/Settings/InviteSettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
public IActionResult BlogSettings() public IActionResult BlogSettings()
@ -503,7 +503,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/BlogSettings.cshtml", model); return View("/Areas/User/Views/User/Settings/BlogSettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
public IActionResult UploadSettings() public IActionResult UploadSettings()
@ -525,7 +525,7 @@ namespace Teknik.Areas.Users.Controllers
return View("/Areas/User/Views/User/Settings/UploadSettings.cshtml", model); return View("/Areas/User/Views/User/Settings/UploadSettings.cshtml", model);
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http403")); return new StatusCodeResult(StatusCodes.Status403Forbidden);
} }
[HttpGet] [HttpGet]
@ -540,7 +540,7 @@ namespace Teknik.Areas.Users.Controllers
{ {
return Content(userClaims.PGPPublicKey, "text/plain"); return Content(userClaims.PGPPublicKey, "text/plain");
} }
return Redirect(Url.SubRouteUrl("error", "Error.Http404")); return new StatusCodeResult(StatusCodes.Status404NotFound);
} }
[HttpPost] [HttpPost]