Asp.net-Mvc

ASP.NET MVC 5:使用站點模板無限重定向到登錄頁面

  • September 27, 2014

我剛開始使用 ASP.NET MVC 5(我已經大量使用了以前的版本),但我遇到了一個非常奇怪的問題:我使用 Visual Studio 2013(完全更新)的 ASP.NET 模板創建了一個新網站。對於模板選項,我選擇了 MVC 模板、“個人使用者帳戶”身份驗證類型、沒有云託管,並且除了核心 MVC 庫之外沒有其他組件。驗證選項後,我會更新所有 NuGet 包。之後我按 F5(不打開或修改任何新項目文件)。

由於無限的重定向循環,瀏覽器僅打開以顯示錯誤頁面:URL 顯示:

http://localhost:24585/Account/Login?ReturnUrl=%2FAccount%2FLogin%3FReturnUrl%3D%252FAccount%252FLogin%253FReturnUrl%253D%25252FAccount%25252FLogin%25253FReturnUrl%25253D%2525252FAccount%252525<snip>

同樣,這是使用庫存的、未修改的 ASP.NET MVC 模板。我確實檢查了登錄 URL 是否在 cookie 身份驗證選項中定義,並且看起來不錯:

// Configure the sign in cookie
app.UseCookieAuthentication(new CookieAuthenticationOptions
{
   AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
   LoginPath = new PathString("/Account/Login"),
   Provider = new CookieAuthenticationProvider
   {
       // Enables the application to validate the security stamp when the user logs in.
       // This is a security feature which is used when you change a password or add an external login to your account.  
       OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
           validateInterval: TimeSpan.FromMinutes(30),
           regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
   }
});            

唯一可能會破壞預設網站的是全域 web.config/machine.config 文件中的某些內容,儘管我通常避免在我的開發框中弄亂它們。在不更新 NuGet 包的情況下啟動模板並不能解決問題。ASP.NET 身份庫可能有問題,但我真的不知道,也找不到與我的問題相關的任何相關資訊。

問題:有誰知道問題是什麼,或者至少解決此問題的最佳方法是什麼?

謝謝

好的,經過一夜好眠,我最終發現了問題所在:問題出在 IIS Express 配置中,當我創建一個新項目時由於某種原因沒有重置它,並且可能繼承了以前項目的設置。它禁用了匿名身份驗證並啟用了 Windows 身份驗證,這與模板創建的預設 web.config 不兼容,因為它的身份驗證模式設置為None. 因此,IIS 在提供任何資源之前期望進行 Windows 身份驗證,並且該網站沒有要求提供 Windows 憑據。我們有一個無限循環。

TL;DR 如果您有同樣的問題,請檢查項目屬性(在解決方案資源管理器中選擇項目,然後按 F4)。Anonymous Authentication條目應設置為Enabled,條目Windows Authentication應設置為Disabled。請注意,這僅在您未在模板設置中選擇 Windows 身份驗證時有效!

引用自:https://stackoverflow.com/questions/26064668