【发布时间】:2015-03-03 08:19:29
【问题描述】:
我目前正在将 WebForms / MVP 应用程序的一些组件迁移到 MVC 中。到目前为止,除了授权之外,一切正常。无论如何,当我导航到登录页面的 MVC 版本时,我会被重定向到在 Web.config 中设置的 aspx 页面:
<authentication mode="Forms">
<forms name=".MyWebSite" enableCrossAppRedirects="true" loginUrl="Login.aspx" timeout="60" path="/" defaultUrl="~/Pages/Landing.aspx"></forms>
</authentication>
我尝试过使用AllowAnonymous,但似乎网络表单配置优先。这是我的登录控制器:
[RouteArea("User", AreaPrefix = "")]
public class AuthenticationController : Controller {
[Route("Login")]
[AllowAnonymous]
public ActionResult Login() {
return View();
}
}
我的目录结构如下所示:
> Web Project
> Areas
> User
> Controllers
> AuthController
> Views
> Login.cshtml
在我的 web.config 中,我看到以下允许匿名访问错误页面:
<location path="Error">
<system.web>
<authorization>
<allow users="*" />
</authorization>
</system.web>
</location>
但是,为Areas 路径复制此路径不起作用(可能是因为 cshtml 文件实际上并不像 aspx 页面那样位于那里?)。
现在,如果我已登录(通过 aspx 版本的登录)并且我的用户已通过身份验证,我就可以正常访问 MVC 实现。路由和渲染工作得很好。它只是允许未经身份验证的用户访问 MVC 页面(而不重定向到 aspx 实现),这似乎是一个挑战。我究竟做错了什么?
编辑 我发现的一个非常老套的部分解决方案(基于Turning off ASP.Net WebForms authentication for one sub-directory)如下:
protected void Application_BeginRequest(object sender, EventArgs e) {
// lots of existing web.config controls for which webforms folders can be accessed
// read the config and skip checks for pages that authorise anon users by having
// <allow users="?" /> as the top rule.
// https://stackoverflow.com/questions/4616524/turning-off-asp-net-webforms-authentication-for-one-sub-directory
// check local config
var localAuthSection = ConfigurationManager.GetSection("system.web/authorization") as AuthorizationSection;
// this assumes that the first rule will be <allow users="?" />
var localRule = localAuthSection.Rules[0];
if (localRule.Action == AuthorizationRuleAction.Allow && localRule.Users.Contains("?")) {
// then skip the rest
return;
}
// get the web.config and check locations
var conf = WebConfigurationManager.OpenWebConfiguration("~");
foreach (ConfigurationLocation loc in conf.Locations) {
// find whether we're in a location with overridden config
// get page name
var currentPath = Path.GetFileName(this.Request.Path);
if (currentPath.Equals(loc.Path, StringComparison.OrdinalIgnoreCase)) {
// get the location's config
var locConf = loc.OpenConfiguration();
var authSection = locConf.GetSection("system.web/authorization") as AuthorizationSection;
if (authSection != null) {
// this assumes that the first rule will be <allow users="?" />
var rule = authSection.Rules[0];
if (rule.Action == AuthorizationRuleAction.Allow && rule.Users.Contains("?")) {
// then skip the rest
return;
}
}
}
}
}
这意味着我可以像这样指定“登录”:
<location path="Login">
<system.web>
<authorization>
<allow users="?" />
</authorization>
</system.web>
</location>
但是所有相关的 CSS/JS 都不会被渲染,除非我为这些文件类型添加规则。 必须对此进行更优雅的解决。
【问题讨论】:
-
我有一个混合环境,我都使用旧的 aspx 登录页面。无论用户如何通过身份验证,auth 标志都是相同的。是否有理由同时拥有 aspx 和 mvc 登录页面?
-
@tintyethan - 它被选为迁移到 MVC 的第一页,不幸的是我无法控制,除非这在技术上是不可能的。最终我们将停用 ASPX 实现,但 ASPX 身份验证需要保持原样。
标签: asp.net asp.net-mvc asp.net-mvc-4 webforms