C# 不重定向到未授权的自定义错误页
我正在尝试设置自定义错误页面,到目前为止,我有一个404未找到的错误页面,它工作得很好,但我正在尝试为401 Unauthorized设置一个自定义错误页面,例如:如果您尝试输入需要某种管理员登录的URL,它应该显示401 Unauthorized错误。无论我遇到什么错误,它都会将我重定向到404 Not Found,尽管它应该显示401 Unauthorized错误 这是我犯的错误 到目前为止,我一直在使用ActionFilterAttribute&IAuthorizationFilter,它显示默认浏览器错误,但我希望它显示我的自定义错误页面(.cshtml) 误差控制器C# 不重定向到未授权的自定义错误页,c#,asp.net,asp.net-mvc-5,C#,Asp.net,Asp.net Mvc 5,我正在尝试设置自定义错误页面,到目前为止,我有一个404未找到的错误页面,它工作得很好,但我正在尝试为401 Unauthorized设置一个自定义错误页面,例如:如果您尝试输入需要某种管理员登录的URL,它应该显示401 Unauthorized错误。无论我遇到什么错误,它都会将我重定向到404 Not Found,尽管它应该显示401 Unauthorized错误 这是我犯的错误 到目前为止,我一直在使用ActionFilterAttribute&IAuthorizationFilter,
[HandleError]
public class ErrorController : Controller
{
public ActionResult Error()
{
return View();
}
public ActionResult NotFound()
{
return View();
}
public ActionResult NonSecure()
{
return View();
}
}
用户控制器。所有UserAuthenticationFilter标记都是不可访问的页面,除非您被授权为用户
public class UserController : Controller
{
[UserAuthenticationFilter]
[HttpGet]
public ActionResult Management()
{
using (CarsDBEntities db = new CarsDBEntities())
{
return View(db.Users.ToList());
}
}
[UserAuthenticationFilter]
public ActionResult Register()
{
return View();
}
[UserAuthenticationFilter]
[HttpPost]
public ActionResult Register(User user)
{
if (ModelState.IsValid)
{
using (CarsDBEntities db = new CarsDBEntities())
{
db.Users.Add(user);
db.SaveChanges();
}
ModelState.Clear();
ViewBag.Message = user.FirstName + " " + user.LastName + " successfully registered.";
}
return View();
}
public ActionResult Login()
{
return View();
}
[HttpPost]
public ActionResult Login(User user)
{
using (CarsDBEntities db = new CarsDBEntities())
{
var usr = db.Users.FirstOrDefault(u => u.Email == user.Email && u.Password == user.Password);
if (usr != null)
{
FormsAuthentication.SetAuthCookie(usr.Email, false);
Session["UserId"] = usr.UserId.ToString();
Session["Email"] = usr.Email.ToString();
Session["FirstName"] = usr.FirstName.ToString();
Session["LastName"] = usr.LastName.ToString();
return RedirectToAction("LoggedIn");
}
else
{
ModelState.AddModelError("", "Email or Password is incorrect!");
}
return View();
}
}
[UserAuthenticationFilter]
public ActionResult LoggedIn()
{
if (Session["UserId"] != null)
{
return RedirectToAction("Management");
}
else
{
return RedirectToAction("Login");
}
}
[ValidateAntiForgeryToken]
[Authorize]
[HttpPost]
public ActionResult Logout()
{
FormsAuthentication.SignOut();
Session.Abandon();
return RedirectToAction("Login", "User");
}
}
NonSecure.cshtml
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title = "NonSecure";
}
<div style="background-color: #A52A2A; color: White; height: 10px;">
</div>
<div style="background-color: #F5F5DC; color: red; height: 170px;">
<div style=" padding:20px;">
<h4>
Sorry, the page you are looking for is authorized. You need to login!
</h4>
<h6>@Html.ActionLink("Go Back To Home Page", "Login", "User")</h6>
<br />
<br />
</div>
</div>
<div style="background-color: #A52A2A; color: White; height: 20px;">
</div>
@model System.Web.Mvc.HandleErrorInfo
@{
ViewBag.Title=“不安全”;
}
对不起,您正在查找的页面已授权。你需要登录!
@ActionLink(“返回主页”、“登录”、“用户”)
Web.config
<system.web>
<customErrors mode="On" redirectMode="ResponseRedirect">
<error statusCode="403" redirect="~/Error/NonSecure"/>
<error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors>
<authentication mode="Forms">
<forms timeout="2800"></forms>
</authentication>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<globalization uiCulture="en-US" />
</system.web>
检查此链接:检查此链接:
<system.web>
<customErrors mode="On" redirectMode="ResponseRedirect">
<error statusCode="403" redirect="~/Error/NonSecure"/>
<error statusCode="404" redirect="~/Error/NotFound"/>
</customErrors>
<authentication mode="Forms">
<forms timeout="2800"></forms>
</authentication>
<compilation debug="true" targetFramework="4.6.1" />
<httpRuntime targetFramework="4.6.1" />
<globalization uiCulture="en-US" />
</system.web>