Database connections work fine when application is

2019-09-07 14:43发布

问题:

I have an application which connects to a database, retrieves a username from a user's table and matches it against the username retrieved with System.Security.Principal.WindowsIdentity.GetCurrent.Name

On my localhost, everything works fine. The database exists on the development server but the application lies on my localhost. All of my authorization and authentication techniques are running smoothly.

However, when I publish my application to the development server, I'm faced with the following error.

Cannot open database requested in login 'databaseName'. Login fails.
Login failed for user 'DevelopmentServerName\ASPNET'. 

I can't put my finger onto what would cause this. Any help would be greatly appreciated.

Thanks!

Edit: Here is the connection string!

  <add name="connectionStringName" connectionString="Initial Catalog=myDatabase;Data Source=DevelopmentServerName;Integrated Security=True"
     providerName="System.Data.SqlClient" />

Also, for context. This authentication needs to grab the user's Windows username and match it against the username in the database. Users will have the Computername\Myname username built into the database (if they are authorized to use the required section of the program, that is).

Thanks again :)

回答1:

It appears that your application is attempting to connect to the database under the ASPNET account, which may have limited permissions on the development server, as opposed to logging in on your own (you local machine may actually be using your windows identity). I can see two potential solutions.

  1. Make sure to add into the system.web section of your web.config file.

  2. Check with the system administrator and the SQL administrator to make sure the ASPNET account has proper authorization to connect to the database, if indeed your environment allows this account to connect.

Adding some additional code to your question, such as your connection string may help things out as well.

EDIT: Okay, you are indeed using IntegratedSecurity, so typically with this kind of setup (using impersonation), you need to make sure you are getting prompted to add your Username and Password to authenticate against.

We have a similar setup, and to do this, we have to go to the IIS settings for the virtual directory, select the Directory Security tab, and click the Edit button under Anonymous access and authentication control.

Make sure Anonymous access is unchecked, and you may will most likely need to enable the proper authentication for your environment. Unfortunately we're still using Basic authentication (clear text) here, but Integrated Windows authentication may will work for you too. It depends on your environment.

I'm adding this comment to the main post since this seemed to have done the trick...

I just found this post which may help you get the proper configuration setup to handle what you need based on your IIS environment.



回答2:

The answer may lay with your connection string. My guess would be that you are using integrated authentication to log into the database. This works fine when it's your machine because the application is using your credentials. When you publish to the development server you would be using the aspNet user and wouldn't have the right credentials to login. I would either add this user to your database server or change your connection string to use SQL authentication.



回答3:

It could be a firewall setting that's preventing your server from seeing your database.

It might also have something to do with your connection string. If you're using anything besides a username/password combo in your web.config file, you will probably require additional configuration to convince the database server to let you connect.



回答4:

It seems that what you want to do is impersonate the caller of the web page. You need to add a line to your web.config to do this:

<identity impersonate="true" />

See this article for an explanation.