Hello! I'm testing UWP with SqlClient on Win10 and can't resolve this weird error. I'll provide details on the software I'm using first:
Windows 10 build 16299 (preview for the Fall Creator's update)
Visual Studio 2017 version 15.4.0 Preview 3.0
.NET core 2.0 SDK/runtime
Nuget Packages for project:
Microsoft.EntityFrameworkCore.SqlServer v2.0
Microsoft.NETCore.UniversalWindowsPlatform v6.0.0-preview-25706-01
The project targets and has as the minimum Windows build 16278 an (the previous update before 16299 installed this morning).
I can connect to and query the database using a .NET core console application, and also within Visual Studio's Server Explorer.
The exception is as follows:
System.Data.SqlClient.SqlException: 'Failed to generate SSPI context. ErrorCode=NoCredentials.
The code to connect to the database is as follows (it is in the code-behind for a control on the main page.)
...
using System.Data.SqlClient;
...
this.InitializeComponent();
using(SqlConnection helpdeskTest = new SqlConnection())
{
helpdeskTest.ConnectionString = "Data Source = xxx.xxx.xxx; Initial Catalog = whd; Integrated Security = True";
helpdeskTest.Open(); // Exception occurs here.
SqlCommand getAssets = new SqlCommand("SELECT * FROM ASSET")
{
Connection = helpdeskTest
};
using(SqlDataReader result = getAssets.ExecuteReader())
{
while(result.Read())
{
asset_list.Items.Add(result["ASSET_NUMBER"]);
}
}
}
Should this work? The connection string is identical to the one in server explorer. If this is the wrong place for this question / bug, please let me know!
We're going to be testing this with a hardcoded username and password to see if the issue is related to integrated windows security. I'll update with the results.
Changing the connection string to this:
helpdeskTest.ConnectionString = "Data Source = xxx.xxx.xxx; Initial Catalog = whd; User id = " + username + "; password = " + pass + ";";
works perfectly fine. It's able to retrieve assets from the helpdesk system. However, just changing the last part of the connection string to Integrated Security = True
causes the SSPI error. I think this would imply that UWP is unable to generate credentials for the database connection using Windows security. Is there anything else I can do to find what might be causing this issue?
Just want to bump this. Since documentation isn't out for this feature yet, I may be using this incorrectly, but I created a new UWP project today (using Microsoft.NETCore.UniversalWindowsPlatform v6.0.1) on the latest build of the Fall Creator's update (16299.15) in Visual Studio 2017 v15.4 Preview 6, and I'm getting the same bug. If I use this:
private string conString = "Data Source = xxx.xxx; Initial Catalog = xxx; User id = xxx; password = xxx;";
It works fine. But if I use this (integrated security):
private string conString = "Data Source = xxx.xxx; Initial Catalog = whd; Integrated Security = True;";
It gives the error above, that it cannot generate the SSPI context because there are "no credentials." I tested this running the same code, but just commenting out the line I wasn't testing above. I know that my account is allowed to access the server, because integrated security works for the same SQL query using a .NET console application on the same computer. If this is reproducible on other projects with UWP, I'd guess that this is a pretty big bug since integrated Windows security is the suggested way to go.
I just tested this today, and get the same error when using integrated security in a new UWP test project.
Do you have Enterprise Authentication enabled in your app manifest? https://github.com/dotnet/corefx/issues/22890#issuecomment-333305084
Enabling Enterprise Authentication solved it, thanks! (At the time of writing, the docs don't include the new SQL features.)
Most helpful comment
Do you have Enterprise Authentication enabled in your app manifest? https://github.com/dotnet/corefx/issues/22890#issuecomment-333305084