简体   繁体   English

使用Windows身份验证的asp.net SQL Server身份验证

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

I am currently working on a ASP.NET admin dashboard. 我目前正在使用ASP.NET管理员信息中心。 The app pool for this IIS website is running with a domain user, which is used to connect to a SQL Server that holds various data for the webpage. 该IIS网站的应用程序池与域用户一起运行,该域用户用于连接到包含该网页的各种数据的SQL Server。

Now, on this website I want to connect to many different SQL Servers using either SQL Server Authentication (if the SQL Server is in a different domain) or Windows Authentication (a separate Windows Account for each SQL Server). 现在,在此网站上,我想使用SQL Server身份验证(如果SQL Server在不同的域中)或Windows身份验证(每个SQL Server都有一个单独的Windows帐户)连接到许多不同的SQL Server。

In SQL Server Reporting Services we can create data sources and define how to connect to the databases - basically I want to do the same, but within my ASP.NET code. 在SQL Server Reporting Services中,我们可以创建数据源并定义如何连接到数据库-基本上我想做同样的事情,但是要在ASP.NET代码中进行。 The authentication information will be stored in a SQL Table (encrypted) accessed by the app pool user. 身份验证信息将存储在应用程序池用户访问的SQL表(加密)中。

And no - I can't use SQL Server Reporting Services =) 而且-我不能使用SQL Server Reporting Services =)

Additionally, I need to connect to SQL Servers starting 2005 up to 2017 or any upcoming new version. 此外,我需要连接从2005年到2017年或任何即将推出的新版本的SQL Server。 I will have 5 - 100 Servers that I need to query (depending on the scenario). 我将需要查询5-100台服务器(取决于方案)。

How can I do that? 我怎样才能做到这一点? Did anybody implement something like this? 有人实现过这样的东西吗? Do you have any tutorials or references? 您有任何教程或参考资料吗?

Thanks in advance 提前致谢

In order to that, I need to use an UserImpersonation Class provided by the "UserImpersonation" nuget package. 为此,我需要使用“ UserImpersonation” nuget包提供的UserImpersonation类。

Here is a code snippet with which I've test the three authentication methods: 这是我测试了三种身份验证方法的代码段:

  • App Pool Identity 应用程序池标识
  • SQL Server Authentication SQL Server身份验证
  • Windows Authentication with Impersonation 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; } 

It's not perfect - but it should work. 这不是完美的-但应该可以。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM