How can I configure the database in the host when I had to change its name there - asp.net

I have designed a simple cms(well it is not like dnn joomla its simple) and most certainly I have a database where I put my stored procs and tables and I hashed and salted the pwds and so. I sent it to a host using filezilla, everything is alright but one thing: "it doesn't work".
System.Exception: Cannot open database "OrganizerDB" requested by the login. The login failed.
Login failed for user 'IIS APPPOOL\D_mydomain.com
I have some doubts it might be I had to change the database name because there was already a registered db name there with my db name,I don't know where in the code except my connection strings the db is declared or whatever...its a dilemma...every where in the code i corrected the database but it doesn't work...and I changed the db name in the host cpanel and I even recreated the database using generated scripts and changed it,if any of you professionals could give me a hand here I'd be thankful

The IIS worker process is running in the context of virtual account (in your case: IIS APPPOOL\D_mydomain.com), that account needs access to your database.
If you prefer you can create your own user and make application pool to run under its context, then grant access to database for that user.
More details here
Quick fix:
Create local user myUserName
Add this to web.config: <identity impersonate="true" userName="myUserName" password="myPassword" />
Give required permission to user myUserName on the database

Related

Cannot open database. Login failed for user 'NT AUTHORITY\NETWORK SERVICE'

I'm trying to deploy my first ASP.NET app on IIS, but I'm facing an issue whenever I connect to database. Connecting to database works just fine running the app on visual studio.
I keep getting the following:
SqlException: Cannot open database "RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad" requested by the login. The login failed.
Login failed for user 'NT AUTHORITY\NETWORK SERVICE'.
My connection string looks like this:
<add name="LocalSqlServer"
connectionString="Server=(localdb)\MSSQLLocalDB;Database=RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad;Integrated Security=true"
providerName="System.Data.SqlClient" />
Like stated in the article here: https://learn.microsoft.com/en-us/archive/blogs/sqlexpress/using-localdb-with-full-iis-part-2-instance-ownership I have tried #1 "Run IIS as our Windows user" and #2 "Use LocalDB Shared Instance" of the approaches without any luck so I've moved to trying NetworkService instead of ApplicationPoolIdentity which is where I'm facing the same error (using ApplicationPoolIdentity it fails with
Login failed for user 'IIS APPPOOL.NET v4.5'") just under different user.
In my SQL Server Management Studio I've created a new user with "NT AUTHORITY\NETWORK SERVICE" name and made sure I gave "db owner" permission for the database and ticked all Server Roles. Most of the answers point out to giving permissions, but that still does not address this for me.
Any insights would be appreciated
Problem in your ConnectionString, you haven't any user content CS for Login Database.
First you must add user for database your then set your connectionString
Two Options
Create new user on your database then this user set access for RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad Create User
Enable sa user, by default disabled sa user, Enable Sa user
Finally Change connection string
if you use option 2
Server=(localdb)\MSSQLLocalDB;Database=RazorPagesMovieContext-ae4522b8-14f6-4dd9-9e71-fb8009d965ad; user=sa; password=yourPassword
**For enable **
The solution in the above link is useful, Run IIS as our Windows user or Use LocalDB shared instance is an approach to the issue.
I am here just to discuss the approach provided in the documentation. In solution1, firstly we should ensure that the VS is running properly because we should ensure the Database instance also is running properly. Then change the application pool identity to the Windows account running the Visual Studio.
In solution2, since Visual Studio2017 has updated the name of the local database instance, we should change the command accordingly.
sqllocaldb share instance mssqllocaldb IIS_DB
Subsequently, apply it to the connection string.
Data Source=(localdb)\.\IIS_DB;Initial Catalog=mydatabase;Integrated Security=True
Here is a detailed description of this issue.
https://codemegeek.com/2018/05/13/configure-iis-to-us-localdb/
Feel free to let me know if there is anything I can help with.

Can't gain access to local SQL for modification

I've been given a backup of a production SQL database to begin writing an application against.
I've got it running as a local instance and using Windows Authentication and I can READ the information, however I cannot figure out how to get IIS to use my Windows Authentication to log into the SQL server when running a local ASP.Net website.
I don't have the authority on the DB to add users, and I'm getting an error of System.Data.SqlClient.SqlException: Login failed for user 'NT AUTHORITY\NETWORK SERVICE'. when I try to access a page that reads the database.
I don't think I will be able to communicate with the DB admin for a while to get him to fix this up for me. Is there anything I can do here?
Your asp code is using the same identity that IIS runs under - the Network Service account. To get it to use your credentials, you can either hard-code them into a connection string or you can use impersonation (see this link: http://msdn.microsoft.com/en-us/library/aa292118%28VS.71%29.aspx)
The gist of the link:
Put this in your web.config file: <identity impersonate="true" />
-t.
You need to create an account in SQL Server for the Network Service account,ex via SSMS via the "Security" section, right click on "Logins" and select add. You will be adding a Windows Account, you can then lookup and validate the name "NETWORK SERVICE". Then switch to the "User Mapping" section and grant permission to view your database.
reference : http://msdn.microsoft.com/en-us/library/ms998320.aspx#paght000015%5Fsqlserver
2nd Alternative
If "integrated security=sspi" is there in your connection string than you should:
Turn on integrated auth in IIS
Turn on windows auth in asp.net
Turn on imerposation in asp.net
Hopefully it will help.

Give Asp.net mvc app permission to drop and create SQL Server Database

I am using SQL Server 2008 R2 and am facing a problem. The application that I have developed needs to be tested at client's site which is at different locality. So I plan to configure the client's machine once and then for any changes related to application I will just distribute a asp.net mvc deployment package which client can deploy on IIS. For that, I need to provide my asp.net application ability to drop and create database (through codefirst entity framework). In the present configuration, I am facing permission issue related to dropping the database. The Application somehow is unable to drop the database. Here is summary of IIS and SQL Server configuration that I am using.
For IIS, I have set the Application Pool Identity to "Local Service" as per the standard practice. The connection string in asp.net web.config file is given below.
connectionString="Server=.\SQLEXPRESS;Database=SomeDatabase;Trusted_Connection=true;User Id=someuser;Password=somepassword" />
For SQL Server Service, I have provided "Local Service" as log on, again providing the minimum access here for the service. For SQL Server Instance Logins I have defined the user and password and given complete authority ("sysadmin") role.
With this configuration in place I was expecting my IIS application to connect using the user and password created above and have the ability to drop and create the SQL Server database. But I am getting permission denied for Dropping Database. The Exception is given below.
System.Data.SqlClient.SqlException (0x80131904): Cannot drop the database 'SomeDatabase', because it does not exist or you do not have permission.
I have checked that the database exists so it boils down to permissions. Am I missing out some configuration ?
To be clear, your connection string is a bit malformed, and may not be behaving as you expect.
When you specify Integrated Security=true in your connection string, then Windows Authentication occurs. Any user id= attribute in the connection string will be ignored.
Switch to SQL Server authentication mode by dropping your Integrated Security=true attribute.
Server=.\SQLEXPRESS;Database=SomeDatabase;
User Id=someuser;Password=somepassword;
Further, the DROP DATABASE command can be executed by the database owner, a user who's a member of the db_owner role, or a user in a server admin role.
Add the database user someuser to the db_owner role.
EXEC sp_addrolemember 'db_owner', 'SomeUser';
Alternatively, if you determine that the account above should NOT be in this role (i.e. restrictive security environment, policies, etc), consider creating and using another account just for this purpose. This would likely mean maintaining another connection string. If the separation of users/roles is important enough for you, perhaps this second option will work.
I think that the real account being used on the Sql connection is the 'Local Service' because you defined Trusted_Connection=True in the connection string. Try to remove it and see what happens. If I'm not wrong, this parameter will make use of a Windows Integrated Account, the Local Service in your case.
While specifying credentials in the connection string, you either need to omit Trusted_Connection part or set it to False
Data Source =myServerAddress; Initial Catalog =myDataBase; User Id =myUsername; Password =myPassword;
OR
Server =myServerAddress; Database =myDataBase; User ID =myUsername; Password =myPassword; Trusted_Connection =False;
Refer http://connectionstrings.com/sql-server-2008 for more details.

Can't connect to database on server

I cannot connect to my SQL Server database when running app on server.
Everything runs fine when debugging but when I run on the server as ASPNET the user is unable to login.
I created the dataabse in a test project then simply connected to this db. My connection string is
Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Temp\Barry\fsr\FSR.mdf;
Integrated Security=True;Connect Timeout=30;User Instance=True
So this asp app runs on IIS 5 and when deployed the initial select works fine as my gridview that I have a binding to shows data but when I run the program (update the db) I hit the following:
[SqlException (0x80131904): Cannot open user default database.
Login failed.
Login failed for user 'hostxxxxx\ASPNET'.]
I am not creating this database programmatically as mentioned previously, simply connecting to an existing database.
The database is located on my c: - but I have added user ASPNET. How can I add this db to the program as a resource rather than reference a copy on c:?
My first question is this: If you have control of the server, why are you using an attached database. From:
AttachDbFilename=C:\Temp\Barry\fsr\FSR.mdf
There is no reason to attach if you can control the server. Attach the database to the actual instance in SQL Server, not with the bastardized version you have above. The bastardized version is useful on an ISP that does not give you access to SQL tools. Beyond that, it is more work than it is worth.
The second problem you have is authentication. There are a couple of ways to handle this. The easiest is set up a SQL user to access the database. If every user will have login credentials, you can keep the Windows Authentication method, but you have to turn off anonymous access, so every user GETS authenticated. As long as anon is a choice in IIS, it will default to anon and you will have issues. The third way is to impersonate a user for database access. I guess the fourth is open your database wide open, but I don't suggest destruction of security to make something "work".
If you have your database on a server, you need to use a server-based connection string - something like:
Data Source=servername\SQLEXPRESS;database=FSR;
Integrated Security=True;Connect Timeout=30;
Your user needs to have a login on the server, and a user in the appropriate database, in order to connect successfully.
See the ConnectionStrings.com web site for a huge list of possible connection strings - and what their settings mean and how you can change those.
You need to get into your database and assign the proper privileges to the account that is trying to access the database, which in this case looks like the built-in ASPNET account. Instead of the ASPNET account, you should use the NETWORK SERVICE account. You can change this through IIS.

Database connections work fine when application is run from localhost. Login fails from dev server

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 :)
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.
Make sure to add into the system.web section of your web.config file.
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.
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.
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.
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.

Resources