6

【茶包射手日記】IIS 整合式登入 MSSQL 之 AppPool 身分疑雲

 3 years ago
source link: https://blog.darkthread.net/blog/apppool-login-mssql-fail-msg/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
【茶包射手日記】IIS 整合式登入 MSSQL 之 AppPool 身分疑雲-黑暗執行緒

前天提到我在 IIS 試跑 ASP.NET Core 開源專案 - Kandu,它的資料庫是用 MSSQL,本機跑測試我懶得開帳號,便把連線字串設成 Integrated Security=SSPI 打算走整合式驗證,用 AppPool 的身分連 SQL。就我的理解,開了名為 Kandu 的專屬 AppPool,IIS 會用專屬帳號 IIS AppPool\Kandu 跑程式,連上 SQL Server 自然也是用這個帳號。

網站架好還沒在 SQL 設 IIS AppPoll 權限前先測了一下,預期會看到「IIS AppPool\Kandu」帳號存取被拒的訊息,但出乎意料,我看到的是:

SqlException: Cannot open database "Kandu" requested by the login. The login failed.
Login failed for user 'DOMAIN\COMPUTER$'.

居然是機器帳號登入失敗! 起初以為是 ASP.NET Core OutOfProcess 模式的執行身分比較特別,但切換成 InProcess後訊息依舊,開始覺得事情不單純。

爬文在 stackoverflow 查到有網友提到這是 MSSQL 有名的誤導式錯誤訊息:If you are connecting to a local SQL Server using the app pool identity, the error message will (confusingly) claim that you are connecting with the machine account. 其實不管 OutOfProcess 或 InProcess 都會用 IIS AppPool\Kandu 身份登入沒錯,只是 SQL 遇到登入失敗時會誤報為機器帳號 DOMAIN\COMPUTER$ 。另外找到幾篇討論,也證實 SQL Server 在登入失敗回報 DOMAIN\COMPUTER$ 是已知的誤導行為: 12,且錯誤也確定在設好 IIS AppPool\Kandu 的 SQL 權限後消失。

學到經驗:

在 IIS AppPool 用整合式驗證登入 MSSQL 遇到 DOMAIN\COMPUTER$ 登入失敗訊息別被誤導,請保持信心堅定,仔細檢查 IIS AppPool\AppPoolName 權限設定就對了。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK