我目前正在將WebForms/MVP應用程序的某些組件遷移到MVC中。到目前爲止,除了授權之外,一切都在運行。無論什麼時候,當我瀏覽到登錄頁面的MVC版本,我重定向到在Web.config
設置aspx頁面:我已經使用AllowAnonymous
試圖在混合MVC/WebForms Web應用程序中配置授權
<authentication mode="Forms">
<forms name=".MyWebSite" enableCrossAppRedirects="true" loginUrl="Login.aspx" timeout="60" path="/" defaultUrl="~/Pages/Landing.aspx"></forms>
</authentication>
但現在看來,在web表單配置正在優先考慮。這裏是我的登錄控制:
[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登錄頁面。無論用戶如何通過身份驗證,身份驗證標誌都是相同的。是否有理由同時擁有一個aspx和一個mvc登錄頁面? – tintyethan
@tintyethan - 這是選擇作爲第一頁遷移到MVC,不幸從我手中,除非這在技術上是不可能的。最終,我們將退出ASPX實施,但ASPX身份驗證需要保持原樣。 – SB2055