Can't access SQL Server file in the server machine - sql-server

I setup the my WCF service on server machine.
The server OS is Windows 2008 with SQL Server 2008 Express.
I am trying to access to my SQL Server (mdf) file to read data using LINQ to SQL.
I see that the DataContext is OK - but when I try to get information that in one of the table I get an exception
Failed to generate a user instance of
SQL Server due to failure in
retrieving the user's local
application data path. Please make
sure the user has a local user profile
on the computer. The connection will
be closed.
I don't know what i need to define or change in the IIS 7.5 that I'm using in the server side.
Thanks for any help.
Update: the connection to the database file is fine - but getting information from table is thru an exception. The connection string is:
Data Source=.;AttachDbFilename=|DataDirectory|\ServiceData.mdf;
Integrated Security=SSPI;User Instance=True

If you already have a server, why don't you just attach the MDF to the SQL Server (Express) running, and then use it like a normal database on your server?
After you do this, use a connection string like:
server=Server\SQLExpress;Database=YourDatabaseName;Integrated Security=SSPI
I never quite liked the AttachDbFileName= and User Instance approach - seems like a (unreliable) hack to me, which really doesn't make sense if you're running on a server machine.

The reason that you are getting this error message is because your code is in the security context of a user that has never logged on to the server. The user therefore does not have a profile and you therefore get an error when the code attempts to write to a non-existant profile.
You could use marc_s's approach or you could run as a user that has a profile.

Related

Laravel 8 connect to SQL Server [duplicate]

I am trying to host a SQL server database, but whenever I try to connect to it I get this error:
The login is from an untrusted domain and cannot be used with Windows
authentication
I am connecting through Matlab using the following command:
conn = database('Clinical_Data','DoyleLab07\Acc','','com.microsoft.sqlserver.jdbc.SQLServerDriver','jdbc:sqlserver://DOYLELAB07\SQLEXPRESS:54287;database=Clinical_Data;integratedSecurity=true;').
Connecting to the database using matlab worked fine as long as I was using matlab on the computer which I was using to host the server. However, when I use another computer and the same Matlab command I get the error I showed above.
When I look under control panel\system. I notice that no domain is listed on my host PC or the PC I am using to connect to the host, but both computers are in the same workgroup. Would I be able to fix my problem by creating a domain and adding the foreign PC and the host to that domain? If so, how can this be accomplished?
Any suggestions will be very much appreciated.
Thank you for reading my post.
Getting rid of Integrated Security=true worked for me.
In order to use Windows Authentication one of two things needs to be true:
You are executing from the same machine as the database server.
You have an Active Directory environment and the user the application is executing under (usually the logged in user) has rights to connect to that database.
If neither of those are true you have to do one of two things:
Establish a Windows Domain Controller, connect all of the relevant machines to that controller, then fix SQL server to use domain accounts; OR,
Change SQL server to use both Windows and SQL Server accounts.
By FAR the easiest way is to change SQL Server to use both Windows and SQL server accounts. Then you just need to create a sql server user on the DB server and change your connection string to do that.
Best case option 1 will take a full day of installation and configuration. Option 2 ought to take about 5 minutes.
If your SQL Server is on one domain controller and you are trying to connect to it from another domain controller then you will get this error when
IntegratedSecurity = true;
This will happen even if you include a valid SQL Server username and password in your connection string as they will automatically be over-written with your windows login and password. Integrated security means simply - use your windows credentials for login verification to SQL Server. So, if you are logged in to a different domain controller then it will fail. In the case where you are on two different domain controllers then you have no choice but to use
IntegratedSecurity = false;
Now, when Integrated security is false SQL Server will use the SQL Server login and password provided in your connection string. For this to work, the SQL Server instance has to have its authentication mode configured to mixed mode, being, SQL Server and Windows Authentication mode.
To verify or change this setting in SQL Server you can open the SQL Server Management Studio and right-click on your server name and then select Properties. On the pop-up that appears select Security and you will see where to alter this setting if you need to.
I've had this same issue when using DNS aliases and hosts files to connect to a machine using a different domain name.
Say you have a SQL server called sql1 on mydomain.com - which is an Active Directory domain - and you also have a DNS zone for mydomain.net, and - for consistency - you set up a DNS alias (CNAME) record for database.mydomain.net --> sql1.mydomain.com
You'll be able to connect to sql1.mydomain.com using Windows integrated security, but won't be able to connect to database.mydomain.net even though it's the same server because the domain name doesn't match your AD domain.
This error message can also occur if the account you are using to access the SQL server is locked out by the domain.
I was facing the issue while connecting to SQL Always On Listener. Disabling the loop back check resolved the issue.
Edit the registry using regedit. (Start –> Run > Regedit )
Navigate to: HKLM\System\CurrentControlSet\Control\LSA
Add a DWORD value called “DisableLoopbackCheck”
Set this value to 1
https://blog.sqlauthority.com/2017/04/18/sql-server-login-failed-login-untrusted-domain-cannot-used-windows-authentication/
Why not use a SQL Server account and pass both the user name and password?
Here is the reason why.
In short, it looks like you have an authentication issue.
The problem with workgroups is there is no common Access Control List like Active Directory (AD). If you are using ODBC or JDBC, the wrong credentials are passed.
Even if you create a local windows account (LWA) on the machine (SE) that has SQL Express installed (SE\LWA), the credentials that will be passed from your client machine (CM) will be CM\LWA.
As mentioned here, you might need to disable the loopback
Loopback check can be removed by adding a registry entry as follows:
Edit the registry using regedit. (Start –> Run > Regedit )
Navigate to: HKLM\System\CurrentControlSet\Control\LSA
Add a DWORD value called “DisableLoopbackCheck” Set this value to 1
If you using windows authentication make sure that password of the user hasn't expired. An expired password can explain this error. This was the problem in my case.
Same Error with Connection String in Visual Studio dev environment
Our development database server was recently given a self-signed certificate so it automatically became untrusted. This resulted in the login error cited above. I added TrustServerCertificate=True to my connection string and it works now.
"Server=TheServerAddress; Database=TheDataBase; User Id=TheUsername; Password=ThePassword; TrustServerCertificate=True"
NOTE: This certificate configuration is not recommended for production environments.
In my case the Aliases within SQL Native Client 11.0 Configuration were pointing to invalid server/IP. Once updated it worked correctly.
To check:
1. Start "SQL Server Configuration Manager"
2. Navigate to "SQL Native Client 11.0 Configuration" and then "Aliases"
3. Ensure "Alias Name" and "Server" match correctly for TCP/IP
Following worked for me to get access from another machine to SQL Server using Windows Authentication. This approach may be useful only in development/test environment. E.g. you need to update password manually once you change it on your working machine.
On machine with SQL Server go to Control Panel and add new Windows User with same username and password as is on your working machine. Then create SQL Server login for this user:
CREATE LOGIN [SQLSERVERHOST\myuser] FROM WINDOWS;
Now you can use this login for Windows Authentication.
If you receive error 'The login is from an untrusted domain', this may mean that you changed password on your working machine and now need to update password on SQL Server machine.
Just adding my suggestion for a resolution, I had a copy of a VM server for developing and testing, I created the database on that with 'sa' having ownership on the db.
I then restored the database onto the live VM server but I was getting the same error mentioned even though the data was still returning correctly. I looked up the 'sa' user mappings and could see it wasn't mapped to the database when I tried to apply the mapping I got a another error "Fix: Cannot use the special principal ‘sa’. Microsoft SQL Server, Error: 15405". so I ran this instead
ALTER AUTHORIZATION ON DATABASE::dbname TO sa
I rechecked the user mappings and it was now assigned to my db and it fixed a lot of access issues for me.
Joining a WORKGROUP then rejoining the domain fixed this issue for me.
I got this error while using Virtual Box VM's. The issue started to happen when I moved the VM files to a new drive location or computer.
Hope this helps the VM folks.
We now use a privileged account management solution that changes our passwords regularly. I ended up receiving this error after my password was changed. Closing and re-opening SSMS with the new password resolved my issue.
I started to get this error when i tried to login to SSMS using 'windows Authentication'. This started to happen after i renamed the Windows SQL server. I tried everything to resolve this error and in my particular case changing the machine names in the 'hosts' file to reflect the name SQL server name change resolved the issue. C:\Windows\System32\Drivers\etc\hosts
I had this problem because we where using a DNS name from an old server, ponting to a new server. Using the newserver\inst1 address, worked. both newserver\inst1 and oldserver\inst1 pointed to the same IP.
Yet another thing to check:We had our nightly QA restore job stop working all of a sudden after another developer remoted into the QA server and tried to start the restore job during the middle of the day, which subsequently failed with the "untrusted domain" message. Somehow the server pointed to be the job's maintenance plan was (changed?) using the ip address, instead of the local machine's name. Upon replacing with the machine name the issue was resolved.
TLDR: Changing the DNS server to the loop back address worked for me.
I am working in VirtualBox and had setup two Windows Server 2016 instances. Server A is configure as a Domain Controller and Server B as an SQL Server. After adding Server B to the domain I cold not connect to with Management Studio from Server A. I was getting the "The login is from an untrusted domain and cannot be used with Windows authentication".
My initial configuration had the server getting its IP from the VirtualBox DHCP server.
I changed this to use static IP and entered the 127.0.0.1 address in the Primary DNS and this worked for me.
Hope this helps someone passing by.
I enabled Trust Server Certificate in the Connection Properties and it worked for me
You might find out that you have more than one connection string, and you forgot to change the other one to Integrated Security to false. It happened to me. This answer might help someone.
I was focusing on the web config and the access rights, after a long hustle i remembered that I have another connection string in one of my classes for the emails, I had to change the connection string on the class to use the web config one.
i removed Integrated Security=true and Trusted_Connection=True both of them , worked for me..
In .net Core also you may get this error if Trusted_Connection=True;
Is set. Sample setting in appsettings.json
ConnectionStrings": {
"DefaultConnection": "Server=serverName; Database=DbName; uid=userId; pwd=password; MultipleActiveResultSets=true"
},
Sometime SSMS hang and close all of sudden ,then you get below error when you reconnect to SSMS
i) The Login is From an Untrusted Domain and Cannot be Used with Windows Authentication
OR
ii) The target principal name is incorrect .Cannot generate SSPI context.
In both cases RESTART YOUR MACHINE.
I also had a similar error but then I realised I just had changed the password for my system which caused this error.
To resolve it , I simply logged out of the current session and logged in again and this time
Please Use This Connection URL It's Work Fine
"Data Source=Your IP Address;Initial Catalog = DatabaseName;User ID =sa;Password =your PassWord;TrustServerCertificate=True"
Example : "Data Source=192.168.150.122;Initial Catalog=StudentDb;User ID=sa; PassWord=123;TrustServerCertificate=True"
If you have two servers on the same domain (eg. APP and DB), you can also use Windows Authentication between the app and MSSQL by setting up local users on both machines that match (same username and password). If you don't have the passwords matched up, it can throw this error.
Following was working for me. hope this helps you
<add name="getconn" connectionString="Data Source=servername;Initial Catalog=DBName;Persist Security Info=True;User ID=sa;Password=***" />

Login Failed on SQL Server 2014

I'm having issues connecting to a SQL Server 2014 database using Management Studio with SQL Authentication. This server was just created by my IT department. I restored a database, I added the login with a password, I made sure the server properties had both SQL Authentication and Windows Authentication checked. I made sure the user at the database level is mapped to the login. But when I try to connect using Management Studio I get the following error. However, it works if I use my Windows Authentication account.
Actually, I kept getting the normal 'Login Failed' message. But this morning I got the above error which is probably more helpful. When I kept trying again, I kept getting the normal Login Failed message.
Have you seen this Microsoft TechNet post about the message you received? It looks like your admin may need to adjust the SQL Server configuration.
Specifically, it looks like you might need to try one of:
Connect using TCP/IP, or use the SQL Server Configuration Manager to
enable remote connections using named pipes.
or...
Using SQL Server Configuration Manager on the client computer, move
TCP before named pipes in the protocol order list.

Connection string for SQL Server Express 2014

On Win 7 system I installed SQL Server 2014 Express. I created instance HM1. I've set it up to use mixed mode. I created a SQL Server user and I am able to login with no issue.
My goal is to connect to a database using this SQL Server user using configuration file. This connection will be used for another application. Note: if I disable user/password usage during login I have no problem but I have to use credentials.
For connection I provided machine_name\SQLSERVEREXPRESS\HM1. I also provided username and password. I also changed the service for SQLEXPRESS and HM1 to run as local account.
When I try to connect using that file I've got the error
Can't connect to database
Again if I disable user/password usage I am able to connect using the same file with no problem.
Could someone help me with it?
Thanks
For connection I provided "machine_name\SQLSERVEREXPRESS\HM1".
You just want this:
machine_name\HM1
That's probably not your only problem, but that's all I can tell you until you try that and then come back and include more detail from the error message.
You need to put the un-encoded password in the config file instead of the base64 encoded one. Alternatively, you can also put Trusted_Connection=True; in the connection string if your database server resides on the same server as the application server. This will use the OS credentials instead of the username/password that you provide.

Error while trying to connect to SQL Server - localhost

I have checked the instance name, auto close is set to true, allow remote connections on the server is checked. The server is running when I open the SQL Server configuration manager. I have even rebooted. I have created this db the same way as all others. I use Entity Framework and have checked the names in the web.config and they match. This is the default connection string from the wizard - I use for testing before I deploy. I just can't think of anything else to check to figure out why it won't connect. Working inside SQL Server everything is fine.
Here is the general error.
A network-related or instance-specific error occurred while establishing a connection to SQL >Server. The server was not found or was not accessible. Verify that the instance name is >correct and that SQL Server is configured to allow remote connections. (provider: Shared >Memory Provider, error: 40 - Could not open a connection to SQL Server)
Config:
connectionString="metadata=res://*/Model.csdl|res://*/Model.ssdl|res://*/Model.msl;provider=System.Data.SqlClient;provider connection string='data source=.\SQL_1;attachdbfilename="C:\Program Files (x86)\Microsoft SQL Server\MSSQL10_50.SQL_1\MSSQL\DATA\A_db.mdf";integrated security=True;connect timeout=10;user instance=True;multipleactiveresultsets=True;App=EntityFramework'" providerName="System.Data.EntityClient" />
Thanks in advance.
Let me preface this answer by saying that proper setup of a SQL instance is not as easy as Microsoft would like you to think with the entity framework. It's a little bit involved and requires that you put your DBA hat on for a little bit.
The error you have indicates that the web instance is attempting to connect to the SQL server using Windows Integrated Security. This will work fine if (a) the windows user that the process is running as (which can be configured in IIS) is authorized to log on to the SQL server and has a valid login in the database and (b) if the SQL server is on the same machine or in the same domain as the IIS server.
In light of this, I recommend using SQL Server authentication. If you need to know how to do this, I recommend searching for "SQL Server Authentication setup" - here is an article that I found which might help you set this up.
http://msdn.microsoft.com/en-us/library/aa337562.aspx
In general, I recommend taking the following actions:
Connect to the sql server using MS SQL Management Studio.
Permanently attach your database, then use the Initial Catalog property on your connection string rather than AttachDbFileName
Then set up your login username and password on the SQL server, and create a login in the database for it.
Make sure your login can only execute the stored procedures you want it to execute. Deny it access to running sql statements.
You will also need to add the username and password to your connection string, and set IntegratedSecurity=false.
Let us know how things go once you get your SQL server set up properly.
This error means that your provider code cannot find the SQL Server. If you have checked the server instance name (it should be <yourLocalServer>\SQL_1), then it could be the attachdbfilename= parameter, as this is a really unreliable way to specify the database to connect to (you should be using the Database Name, not the file name), because there are about a hundred reasons that the file name could change that have nothing to do with your application.

Application Login not working unless Windows Authentication works in SQL Server

I have a database set up that is to gather planned vacation time that I need to put on sharepoint asap. It's completely done, except for that the application login is failing for anyone that tries to use it that does not have their corprate login (windows authentication) listed in the security logins folder.
The connection string is fine, as it works properly on my computer and another programmer's computer, but not on my boss's computer. I can also login to sql server using the application login that is listed in the connection string.
I'm using SQL Server Managment Studio 2008, the server is 2005.
Edit#1: Further research led to finding this page: http://sql-articles.com/articles/troubleshooting/troubleshooting-login-failed-error-18456/
And my error is state 11. It's listing a windows authentication in the error log even though I specified otherwise.
Edit#2: My Connection String is this:
Driver={SQL Server};Server=ServerName\ThingIdontUnderstand;Database=ReportingDevDB;Trusted_Connection=FALSE;uid=Derp;pwd=qwerty;
Edit#3: Solved! See comments on this post for answers since I can't answer my own questions yet.
You check for the version when you connect to the instance through SSMS it will show it beside the server name under Object Explorer. Or just use the query SELECT ##VERSION
I would suggest simply creating a corporate windows group and adding those individuals to it, and then simply add this group as a login to your instance of SQL Server.

Resources