简体   繁体   English

如何使用域帐户通过C#连接数据库?

[英]How do I use a domain account to connect to a database using c#?

I want to create a database using the custom action in the website setup project. 我想使用网站设置项目中的自定义操作创建数据库。 but i'm facing a problem doing that in the "website setup project", but when using the regular "setup project", every thing went fine without a problem... 但是我在“网站安装项目”中这样做时遇到了问题,但是当使用常规的“安装项目”时,一切都很好,没有问题...

the problem was from the authentication when I want to access the database. 问题出在我想访问数据库时来自身份验证。 It seems that when I use the "setup project" it use my actual username,pass, & the domain. 看来,当我使用“安装项目”时,它会使用我的实际用户名,密码和域。 while in the "website setup project" it uses my computer name & for that reason it will not work. 而在“网站设置项目”中,它使用了我的计算机名,因此它不起作用。 since, it doesn't have access to the database. 因为,它无权访问数据库。

So, I need a way to tell the website to use my account (I will pass it through the custom action) that include my username,pass, & the domain. 因此,我需要一种方法告诉网站使用我的帐户(我将通过自定义操作将其传递),其中包括我的用户名,密码和域。

How I can do that in the C#? 我如何在C#中做到这一点? is there is an API to do that? 是否有API可以做到这一点?

Thanks 谢谢

You can't pass Windows credentials in a connection string. 您不能在连接字符串中传递Windows凭据。 How you would do this is to run your website in IIS(7 and above) and configure your app pool Identity to run under the account you want. 您将如何在IIS(7及更高版本)中运行您的网站,并将您的应用程序池标识配置为在所需帐户下运行。 See how here: http://technet.microsoft.com/en-us/library/cc771170(WS.10).aspx 在此处查看方法: http : //technet.microsoft.com/zh-cn/library/cc771170(WS.10).aspx

This is not how it is done normally though. 但是,这不是正常完成的方式。 Normally you would create a sql account in your sql server and then in your connection string you would do something like this: 通常,您将在sql服务器中创建一个sql帐户,然后在连接字符串中执行以下操作:

"Data Source=[ServerName]; Initial Catalog=[DatabaseName]; User Id=[SqlAccount]; Password=[SqlAccountPassword]" “数据源= [服务器名称];初始目录= [数据库名称];用户ID = [SqlAccount];密码= [SqlAccountPassword]”

You can then go once step further in sql and configure what access to give to the website account etc. 然后,您可以在sql中更进一步,并配置对网站帐户的访问权限等。

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

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