﻿# 解決 IE10 瀏覽器無法使用 ASP.NET 表單驗證登入的問題

今天凌晨在客戶端上線，當程式佈署到正式機後發現我們的網站唯獨只有 IE10 瀏覽器無法成功登入，任何其他瀏覽器版本或使用較低的 IE 版本都可以正常登入，使用 IE 相容性檢視也都可以正常登入，想說會不會又是罄竹難書的 IE10 搞的鬼。後來用 [Fiddler](http://www.fiddler2.com/fiddler2/) 工具檢查後發現 IE10 送出的 HTTP 標頭完全沒問題，該有的 **.AUTH** Cookie 都有送出，但使用**表單驗證機制** ([FormsAuthentication](http://msdn.microsoft.com/zh-tw/library/system.web.security.formsauthentication.aspx)) 就是無法判斷該使用者已經登入，還真的再次鬼打牆，最後還是研究出解決方法。

簡單來說，這算是個 ASP.NET 2.0、3.5 與 4.0 的 Bugs，因為這兩個版本無法識別 IE10 的 User-Agent 標頭字串，所以無法正確識別用戶端的 IE10 瀏覽器版本，因而導致 ASP.NET 特定功能失效，在這個例子中，出問題的地方在於 ASP.NET 以為瀏覽器不支援 Cookies 功能，所以 ASP.NET 將不處裡任何與 Cookies 相關的程式碼！

以下是 IE10 的 User-Agent 標頭字串：

```
User-Agent: Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; Trident/6.0)
```

這個問題早在 2011/4/16 就有人到 [Microsoft Connect](http://connect.microsoft.com) 網站[回報](http://connect.microsoft.com/VisualStudio/feedback/details/662275/asp-net-user-agent-sniffing-and-ie10-internet-explorer-10)過，而且經微軟官方確認這是個 Bug，爾後還發布了兩個 Hotfix 專門用來解決這個問題，各位如果遇到一樣的問題可以參考以下兩篇 KB 說明：

-   KB2600088: [Hotfix 適用於在 Microsoft.NET Framework 4.0 中的 ASP.NET 瀏覽器定義檔](https://web.archive.org/web/20150227223619/http://support.microsoft.com:80/kb/2600088) \[[英文](https://web.archive.org/web/20150227223619/http://support.microsoft.com:80/kb/2600088)\]
-   KB2608565: [Hotfix 適用於在 .NET Framework 3.5.1 中的 ASP.NET 瀏覽器定義檔](http://support.microsoft.com/kb/2608565) \[[英文](http://support.microsoft.com/kb/2608565/en-us)\]
-   KB2600100: [Hotfix 適用於在 .NET Framework 3.5 SP1 與 .NET Framework 2.0 SP2 中的 ASP.NET 瀏覽器定義檔](http://support.microsoft.com/kb/2600100) \[[英文](http://support.microsoft.com/kb/2600100/en-us)\]

在這問題發現之後的 ASP.NET 版本已經修復了這個問題，所以我們在 Windows 8 上開發網站並不會有這個錯誤出現，所以上到客戶的正式機才出問題。

在此分享一個更簡易的解決方法，只要在網站目錄新增一個 **瀏覽器定義檔** (browser definition file) 即可解決此問題，步驟如下：

1\. 先在專案目錄或正式機網站跟目錄下新增一個 **App\_Browsers** 資料夾

2\. 新增一個副檔名為 \*.browser 的檔案，例如：**IE10Fix.browser**，檔案內容如下：

```xml
<browsers>
  <browser refID="Default">
    <capabilities><!-- To avoid wrong detections of e.g. IE10 -->
      <capability name="cookies" value="true" />
      <capability name="ecmascriptversion" value="3.0" />
    </capabilities>
  </browser>
</browsers>
```

設定完成後，問題自然解決！

**相關連結**

-   [ASP.NET user-agent sniffing and IE10 (Internet Explorer 10) | Microsoft Connect](http://connect.microsoft.com/VisualStudio/feedback/details/662275/asp-net-user-agent-sniffing-and-ie10-internet-explorer-10)
-   [Bug and Fix: ASP.NET fails to detect IE10 causing \_doPostBack is undefined JavaScript error or maintain FF5 scrollbar position](http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx)
-   **Microsoft KB (Hotfix)**

-   [A hotfix is available for the ASP.NET browser definition files in the Microsoft .NET Framework 4.0](https://web.archive.org/web/20150227223619/http://support.microsoft.com:80/kb/2600088)
-   [A hotfix is available for the ASP.NET browser definition files in the .NET Framework 3.5.1](http://support.microsoft.com/kb/2608565/en-us)
-   [A hotfix is available for the ASP.NET browser definition files in the .NET Framework 2.0 SP2 and in the .NET Framework 3.5 SP1](http://support.microsoft.com/kb/2600100/en-us)

-   [IE10 User-Agent causes ASP.Net to not send back Set-Cookie (IE10 not setting cookies)](http://stackoverflow.com/questions/6983732/ie10-user-agent-causes-asp-net-to-not-send-back-set-cookie-ie10-not-setting-coo)
-   [Browser Definition File Schema (browsers Element)](http://msdn.microsoft.com/en-us/library/ms228122\(v=VS.100\).aspx)
-   [The IE10 User-Agent String](https://learn.microsoft.com/en-us/archive/blogs/ie/the-ie10-user-agent-string)
-   [Detecting Windows Internet Explorer More Effectively](http://msdn.microsoft.com/en-us/library/ms537509\(v=VS.85\).aspx)
-   [FormsAuthentication 類別](http://msdn.microsoft.com/zh-tw/library/system.web.security.formsauthentication.aspx)
-   [Mastering Browser Definition Files](http://forums.asp.net/t/955969.aspx/1)
-   [ASP.NET Browser Definition Files](https://web.archive.org/web/20171016085017/http://aspnet.codeplex.com/releases/view/41420) ( ASP.NET 4.0 的瀏覽器定義檔版本 )
-   [Mobile Support in ASP.NET](http://www.asp.net/mobile)
