簡體   English   中英

使用Windows身份驗證的asp.net SQL Server身份驗證

[英]asp.net SQL Server Authentication using Windows Authentication

我目前正在使用ASP.NET管理員信息中心。 該IIS網站的應用程序池與域用戶一起運行,該域用戶用於連接到包含該網頁的各種數據的SQL Server。

現在,在此網站上,我想使用SQL Server身份驗證(如果SQL Server在不同的域中)或Windows身份驗證(每個SQL Server都有一個單獨的Windows帳戶)連接到許多不同的SQL Server。

在SQL Server Reporting Services中,我們可以創建數據源並定義如何連接到數據庫-基本上我想做同樣的事情,但是要在ASP.NET代碼中進行。 身份驗證信息將存儲在應用程序池用戶訪問的SQL表(加密)中。

而且-我不能使用SQL Server Reporting Services =)

此外,我需要連接從2005年到2017年或任何即將推出的新版本的SQL Server。 我將需要查詢5-100台服務器(取決於方案)。

我怎樣才能做到這一點? 有人實現過這樣的東西嗎? 您有任何教程或參考資料嗎?

提前致謝

為此,我需要使用“ UserImpersonation” nuget包提供的UserImpersonation類。

這是我測試了三種身份驗證方法的代碼段:

  • 應用程序池標識
  • SQL Server身份驗證
  • Windows模擬身份驗證

     public string TestLogin(string UserName, string Password, string Auth, string Domain) { string thisLogin = ""; //App Pool Identity if (Auth.Equals("local")) { using (SqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["sqlConn"].ToString())) { SqlCommand cmd = new SqlCommand("SELECT SUSER_SNAME()", cn); cn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { thisLogin = rdr[0].ToString(); } cn.Close(); } } if(Auth.Equals("SQL")) { using (SqlConnection cn = new SqlConnection("Data Source = .\\\\D01; Initial Catalog = master; Integrated Security = false; User ID = " + UserName + "; Password=" + Password + ";")) { SqlCommand cmd = new SqlCommand("SELECT SUSER_SNAME()", cn); cn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while(rdr.Read()) { thisLogin = rdr[0].ToString(); } cn.Close(); } } if(Auth.Equals("Remote")) { string login = UserName; string domain = Domain; string password = Password; using (UserImpersonation user = new UserImpersonation(login, domain, password)) { if (user.ImpersonateValidUser()) { using (SqlConnection cn = new SqlConnection("Data Source = .\\\\D01; Initial Catalog = master; Integrated Security = SSPI;")) { SqlCommand cmd = new SqlCommand("SELECT SUSER_SNAME()", cn); cn.Open(); SqlDataReader rdr = cmd.ExecuteReader(); while (rdr.Read()) { thisLogin = rdr[0].ToString(); } cn.Close(); } } } } if(thisLogin.Equals("")) { thisLogin = "User failed"; } return thisLogin; } 

這不是完美的-但應該可以。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM