c# - Asp.net Mvc 5,两个帐户控制器和区域但重定向到错误的登录页面
问题描述
我的 mvc 5 项目中有和 Area 。在区域中,我已经从我的项目的根目录和 AccountController 复制了控制器。
我有一个 NewsController 的创建表单和控制器,它在类的开头具有 [Authorize] 属性。
但是,当未登录并想要创建新闻时,表单会返回到我的项目的 root 登录,而不是区域部分。
这些是我的控制器和视图部分:
项目结构:
- 领域
- 行政
- 控制器
- AboutsController.cs
- 新闻控制器.cs
- 意见
- 创建.cshtml
- 行政
创建.cshtml:
@using (Html.BeginForm("Create", "News", FormMethod.Post,
new { enctype = "multipart/form-data"}))
{
@Html.AntiForgeryToken()
....
新闻控制器.cs:
namespace Project.Areas.Admin.Controllers
{
[Authorize]
public class NewsController : Controller
{
...
}
public ActionResult Create()
{
return View();
}
}
并且 AccountController.cs 在根控制器文件夹中是相同的。
请记住:我已添加
新 {area="Admin"}
形成但是,重定向再次转到root的登录登录页面,而不是Areas/Admin/login
@using (Html.BeginForm("Create", "News", FormMethod.Post,new {area="Admin"},
new { enctype = "multipart/form-data"}))
解决方案
您将被重定向到默认登录路由。如果要更改默认登录路由,请在 App_Start\Startup.Auth.cs 文件中进行配置。
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
// Enable the application to use a cookie to store information for the signed in user
// and to use a cookie to temporarily store information about a user logging in with a third party login provider
// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login")
将LoginPath值更改为所需的值。
要更改特定区域的登录页面: 您需要创建自定义授权属性,例如:
public class AdminAuthorize : AuthorizeAttribute
{
public override void OnAuthorization(AuthorizationContext filterContext)
{
// If you are authorized
if (this.AuthorizeCore(filterContext.HttpContext))
{
base.OnAuthorization(filterContext);
}
else
{
// else redirect to your Area specific login page
filterContext.Result = new RedirectResult("~/Area/Admin/Account/Login");
}
}
}
并将其应用于您的区域控制器,如下所示:
[AdminAuthorize]
public class NewsController : Controller
{
...
}
推荐阅读
- mysql - SequelizeEagerLoadingError : 关联在 sequelize 中不起作用
- python - 斯坦福 CoreNLP 服务器使用 12 个线程,6 个客户端调用它,但有时“每个套接字地址通常只允许一次使用”错误
- reactjs - HTML 到 ReactJs 解析 oembed 不显示任何内容
- javascript - jquery如何获取图片的值
- laravel - 如何将简单的用户输入从 Vue 传递到 Laravel 控制器?
- airflow - 气流:在发生 SLA 未命中事件时停止当前 DAG 执行
- powershell - 在 azure devops 中使用 powershell 从 SQLAzure 获取数据库名称
- xml - 如何在android中编辑文本下方获取错误图标?
- c++ - 如何修复 UClass 没有成员“WeaponMesh”错误?
- react-native - 将本机滚动反应到平面列表顶部 onChangeText