.Net Core Application connects to SQL server with wrong credentials

0

I am building a .Net Core (2.2, started as 2.1) application and I have been using a locally hosted database but as I continue I wanted to move to a database hosted on the server it will be located on in the future. The problem is I am not able to scaffold a new database where it should go because it seems it tries to login with the current user account on my workstation, rather than the specified credentials in the connection string. This is the connection string in the appsettings.json file:

"ConnectionStrings": {
    "DocHandlerContext": "Server=SQLserverName\\SQLEXPRESS;Database=DocHandler;Trusted_Connection=True;MultipleActiveResultSets=true;User ID=xxxx;Password=xxxx"
},

And here is the startup.js file calling the DB context:

public class Startup
{
    public static string ConnectionString { get; private set; }

    public Startup(IConfiguration configuration)
    {
       Configuration = configuration;   

       ConnectionString = Configuration.GetConnectionString("DocHandlerContext");
    } 
public void ConfigureServices(IServiceCollection services)
    {
        services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.AddDbContext<DocHandlerContext>(options =>
                options.UseSqlServer(ConnectionString));
    }

And then in the DBcontext file where I have the models, scaffolding data and relationships defined I also call this connection string like this:

public static string GetDefaultConnectionString()
        {
            return Startup.ConnectionString;
        }

        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        {
            optionsBuilder.UseSqlServer(GetDefaultConnectionString());
        }

Yet when I try and do a database-update command with the new settings I get an error message, but I think the relevant issue is this:

CREATE DATABASE permission denied in database 'master'.

Looking at the SQL server logs it indicates it tried to log in with the user account I am currently logged in with, so it doesn't seem to be using the credentials I have specified. I have no idea where this is coming from. Is it not finding the connection string and is defaulting to trying windows authentication? I also tried to run the application in debug mode and I get the same error there:

fail: Microsoft.EntityFrameworkCore.Database.Connection[20004]
      An error occurred using the connection to database 'DocHandler' on server 'SQLSERVERNAME\SQLEXPRESS'.
System.Data.SqlClient.SqlException (0x80131904): Cannot open database "DocHandler" requested by the login. The login failed.
Login failed for user 'DOMAIN\Dennis'.

I have tried for a day now to try and solve this problem, one idea was to use update-database with a specified connection string but it seems that's not available in EF core.

c#
sql-server
.net-core
asp.net-core-2.2
asked on Stack Overflow Jun 12, 2019 by Dennis

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0