Forum Discussion

wallym's avatar
wallym
Copper Contributor
May 05, 2026

Getting a ConnectionString empty error in a webapi project with .net 10

I'm running in .NET 10 with the most recent update for everything. I'm in EF. I'm ASP.NET Core. Below is my program.cs file. Inside of the program.cs file, when I call to get my connection string, I get back what I expect. When I hit the LoginController, I see the settings in the IConfiuguration that is handed in. However, when I set a breakpoint in the FSMUserStore constructor, I check the context that is handed in, and it's connection string is not set correctly (at all). I've run through Visual Studio debugger so many times, and tried so many things, I'm just lost on this. I think the problem is in how I am setting up for dependency injection in the program.cs file, but I've tried so many things that I don't know what else to try.  Anyone have a suggestion?

using ElmahCore.Mvc; 
using Microsoft.AspNetCore.Authentication.JwtBearer;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.IdentityModel.Tokens;
using Portal.Libraries;
using PortalDataModels.Models;
using System.Text;
using Twilio.TwiML.Voice;

var builder = WebApplication.CreateBuilder(args);
IConfiguration configuration = new ConfigurationBuilder()
.AddJsonFile("appsettings.json")
.AddEnvironmentVariables()
.Build();

// connString get's the correct value
var connString = configuration.GetConnectionString("DefaultConnection");

// Add services to the container.
// For Entity Framework
builder.Services.AddDbContext<PortalDataModels.Models.POA_CSMContext>(options => options.UseSqlServer(configuration.GetConnectionString(connString))); builder.Services.AddScoped<IUserStore<FSMUser>, FSMUserStore>();
builder.Services.AddIdentity<FSMUser, IdentityRole>()
.AddEntityFrameworkStores<POA_CSMContext>()
.AddUserManager<UserManager<FSMUser>>()
.AddSignInManager<SignInManager<FSMUser>>()
.AddDefaultTokenProviders();

builder.Services.AddElmah<ElmahCore.Sql.SqlErrorLog>(options => {
options.ConnectionString = connString;
options.SqlServerDatabaseTableName = "Elmah_Error"; //Defaults to ELMAH_Error if not set
options.OnPermissionCheck = context => false; });

// Adding Authentication
builder.Services.AddAuthentication(options => {
options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
options.DefaultScheme = JwtBearerDefaults.AuthenticationScheme; })

// Adding Jwt Bearer
.AddJwtBearer(options => { options.SaveToken = true;
options.RequireHttpsMetadata = false;
options.MapInboundClaims = false;
options.TokenValidationParameters = new TokenValidationParameters() { ValidateIssuer = true, ValidateAudience = true, ValidAudience = configuration["Jwt:ValidAudience"], ValidIssuer = configuration["Jwt:ValidIssuer"], IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(configuration["Jwt:Key"])) }; });

builder.Services.AddScoped<UserManager<FSMUser>>();

builder.Services.AddControllers(); // Learn more about configuring OpenAPI at https://aka.ms/aspnet/openapi
builder.Services.AddOpenApi();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment()) {
app.MapOpenApi();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();

1 Reply

  • wallym's avatar
    wallym
    Copper Contributor

    A guy on stack overflow spotted my problem.  I was using the value of the connection string as a key.  My bad definitely.  sometimes you just need other eyes to look at something.