Database permission denial when using IIS - asp.net

Not entirely sure how to phrase this, so I'll just give as much information as possible.
I've got an asp.net application that uses SELECT on a database table. The connection string is something to the tune of:
"Data Source=Data\Source;Initial Catalog=DataStore; Integrated Security=True"
This works when I use the application locally. Query is executed, information is retrieved, everything's gravy.
The problem is when I try to publish my website using iis. After I publish it, I get the following error when I try to execute the query:
The SELECT permission was denied on the object 'object', database 'DataStore', schema 'dbo'.
I'm kind of stumped here and I'm not that familiar with IIS or security. Any help is appreciated, and I'll try to answer any questions you might have as quickly as possible.

You have the classic double-hop problem, you can read about it many places, like here and read how to do it right here, or go the easy route and put sql authentication user name and passwords in clear text in your config file.

Related

Problem with ASP.NET MVC application deployment

I'm currently deploying my ASP.NET MVC5 project. I already deployed project files, but I got some problems with, I guess, the database. I did the following steps to deploy the database:
Update-database -script in Visual Studio to get the SQL script of my local database (I developed it by code-first)
Connect to the SQL Server database I created in my hosting panel.
Run the script I generated in 1.
Change the connection string in my web.config to this:
<connectionStrings>
<add name="DefaultConnection"
connectionString="Data Source=xxxxxx.xxxwebix.xx,2401;Initial Catalog=xxxxx;User ID=xxxxx;Password=xxxxx"
providerName=".NET Framework Data Provider for SQL Server"
xdt:Transform="SetAttributes" xdt:Locator="Match(name)"/>
</connectionStrings>
So basically I filled it in with the same credentials I had used to connect to the DB to run the script.
Unfortunately, I'm unable to register or login. I get an error
An error occurred while processing your request.
500 (Internal Server Error)
in the console, which doesn't exactly say what's wrong. So basically, I know that the problem probably concerns the database, but I don't know what's wrong and how to get rid of it. This is my website (maybe it will help somehow):
http://gymmanager.bartoszchodyla.pl.hostingasp.pl/
I'd be thankful for any help.
EDIT
I found the problem - providerName. That's weird because I took it from the database properties after I connected to it in Visual Studio. I changed it to default "System.Data.SqlClient" and it worked. Does someone knows why?
There are a lot of things that could be going on here. The fact it is throwing a 500 error means either some code is causing a major error and/or you do not have friendly errors turned on.
There are some things you can do to figure out exactly where the issue is. In your console, make sure friendly errors are turned on, and you should get a bit more information. You can also add try ... catches to where you think the issue is and avoid the exception and write a message instead (this is fairly crude, but quick and easy). Turning on tracing can also work, as long as you can actually spin up the application without the 500. You add trace statements before and after each routine and then go an examine the trace file. The same can be done, in more crude fashion, by writing to a file. The use of this method is you can turn trace on and off at will.
The fact you used code first is non-important if you are creating a SQL Script the correct way. When you run it, as long as it works (creates and possibly seeds?), you have the correct schema.
So, does the app throw the error up front or only when accessing data? If the latter, you have a good place to start seeking out errors.
BTW, you may be able to test locally against your providers database by changing your local connection strings. It will depend on having certain ports open, which you might even be able to do if they are not for security reasons. You can then debug the exception locally.

Creating a database project with Visual Studio - can't find database after running?

I am following this tutorial (http://www.asp.net/mvc/overview/getting-started/database-first-development/setting-up-database) in order to create a database-first app with Entity Framework.
I hit "Run" and everything seems to be working, but in the next part of the tutorial (https://www.asp.net/mvc/overview/getting-started/database-first-development/creating-the-web-application), when I try to add a connection, it's not in the drop-down menu in the "Connection Properties" box under "Select or enter a database name".
Which server is the database on? I didn't specify anything about the server when I created the project for the first tutorial, and I figured that it would be on the default LocalDB that comes with Visual Studio (server name: (localdb)\MSSQLLocalDB), but the database isn't there. Any ideas where I should look or what to try?
Maybe use . (a dot) as Server Name?
Open your Web.config file and check your <connectionStrings> this is where the data source of the database will be. As shown:
However, which version of SQL Server do you have? As this can determine what the correct connection string will be.
Hit Windows+R. And run the following:
%LOCALAPPDATA%\Microsoft\Microsoft SQL Server Local DB\Instances
You may have multiple instances, it would be worth checking each one. So it would be:
(localdb)(name of instance)
This article may also be of use to you:
http://rajchel.pl/2015/10/solving-connection-issue-with-sql-server-2014-localdb/
What could be the case is that you have to run the application you created, and then do something to touch the database like creating / registering a user. After registration you can find the database on the specified location in your web.config settings. This was the solution in my case.

SqlException (0x80131904): Invalid object name 'PlanningRow'

I have an asp.NET MVC 3 application which uses nHibernate and SQL Server Standard Edition. The production-environment works fine, and was developed by someone else.
I'm trying to set up a test-environment on my local PC, and restored a backup from the live Db to my local SQL Express. Changed the connection strings, and got the sessionfactory running. (I'm still learning the different aspects of asp.NET MVC, so I'm not totally confident I've checked everything that needs to be checked).
Unfortunately, now when the index.cshtml is loaded, it fails with the mentioned error (SqlException (0x80131904): Invalid object name 'PlanningRow'). It fails on the line:
planningRows = Planner.GetCurrentSession().QueryOver<PlanningRow>().List();
I've seen similar problems posted on StackOverflow and elsewhere, and I know the error means it can't find the table- but the Db and the table are on my local SQL Express. But it is very well possible the database or table is not in the right schema or the schema does not exist. Unfortunately, I don't know what that actually means, let alone how to fix it, and I can't really find hints about how to do this.
(f.i. SqlException (0x80131904): Invalid object name 'dbo.Categories' gives some interesting options- especially option 2- but no way how to check or fix this).
Can anyone point me in the right direction?
double check the connection string points to the correct database
enable SQL-logging with "NHibernate.Sql" in DEBUG mode
check the mapped schema in PlanningRow mapping and default schema in hibernate.cfg file or config code

Creating Membership DB (ASPNETDB) on remote server?

I'm building a web application which soon I'll be promoting to production. This application has a login screen, as you know I used the integrated DB for developing. Now I want to move this database to a full SQL Server 2005. I'm using the aspnet_regsql.exe to create my membership tables and stored procedures in the SQL Server 2005 but I'm getting an error:
An error occurred during the execution of the SQL file 'InstallMembership.sql'. The SQL error number is 207 and the SqlException message is: Invalid column name 'FailedPasswordAnswerAttemptWindowStart'.
I google this for half of the day already and I can't seem to fine what's wrong, does anybody have ANY ideas on what am I doing wrong here? Any help will be really appreciate it.
The easiest way to deploy a brand new database to a remote server is to just publish the database from within Visual Studio. Once you do that you have the scheme and data all in one script. You then access your server either via a local SQL manager connection or a remote manager and run the script on your remote database.
The site 4GuysFromRolla has a nice little walk through with graphics which describe how to accomplish the publishing of your local database to your hosted one.
Good luck, and hope this helps you out some.
Go through the *.sql script file and see where that column reference is. Then you'll be able to see what exactly the script is doing to cause this error. My guess would be that it is getting caught up on referential integrity. If it is trying to create a foreign key constraint that that table/column does not exist then you'll see that error.
But back to the original question at hand. You don't need to recreate your whole database. That script file is to lay down the Membership for an empty database. If you have a working dev database you can just back it up and restore it to a production instance. No need to rebuild the membership database objects.

Unable to connect to SQL database in AppData when deployed to web server

I have a ASP.Net program written in C# that uses a database stored in the AppData directory, the program works perfectly on my development (Win7\SQL 2008R2) when I deploy to a Win2008R2\SQl 2008 R2 server the program is unable to connect to the database. I'm using the connection string below. Several sites suggest I set the Application Pool to use 'Network Service' as this has Modify permission to the AppData directory, I have done this but it still won't work, another site suggested I Application Pool to "Load User Profile = True" but still no joy.
I'm pulling my hair out with what else to try, can anyone help?
data source=.\SQLEXPRESS;Integrated
Security=SSPI;AttachDBFilename=|DataDirectory|\DB.mdf;User
Instance=true
I get "Object reference not set to an instance of an object." in my app and "An attempt to attach an auto-named database for file C:\inetpub\wwwroot\ADPhonebook\App_Data\DB.mdf failed. A database with the same name exists, or specified file cannot be opened, or it is located on UNC share." in the Application Event Log
I really appreciate any advice, thanks.
Steven
As Martin points out in the comments, it would be interesting if you posted the exact error message (and maybe the full exception detail) you are getting.
But problems like this have mostly to do with rights at the OS level.
So make sure that the user (identity) of the Application Pool has full access (ntfs) rights to the .mdf file, and try again.
Edit (after you posted the exact message):
Ok, I guess this means that on the server you're deploying to, there is already a database called DB on that Sql Server instance. So you will have to remove or rename that one in order to be able to attach a database with that name.
I think the user rights problem is also still possible though.

Resources