ASP.NET Core 7中调用UserManager.GetUserAsync()出现含数据库连接字符串的System.ArgumentException异常咨询
Hey there, let's dig into this weird issue you're facing first—no need to rush back to .NET 6 just yet! Seeing your database connection string pop up in an ArgumentException is definitely odd and not expected behavior, so let's break down possible causes and fixes:
1. Double-check your Identity & DbContext configuration
First, make sure your Identity setup in Program.cs is correctly wired to your DbContext. A common misconfiguration here can lead to unexpected errors. For example, you should have code like this:
builder.Services.AddDefaultIdentity<ApplicationUser>(options => options.SignIn.RequireConfirmedAccount = true) .AddEntityFrameworkStores<ApplicationDbContext>();
If your UserManager isn't properly bound to the right DbContext, or if your DbContext's connection string setup has issues, it might leak sensitive info in exceptions (though the framework should prevent this by default).
2. Get the full exception stack trace
You mentioned an ArgumentException, but the stack trace will tell us exactly where the error is originating. Is it happening during an EF Core query? Or is there a problem with the ClaimsPrincipal you're passing in? For example:
- If your custom
ApplicationUseruses a non-string primary key, but theNameIdentifierclaim in yourClaimsPrincipalis a string, that could cause a type conversion error. - Maybe a custom claim transformation or user store implementation is messing up the user lookup flow.
3. Investigate the connection string leak
This is a potential red flag—ASP.NET Core shouldn't expose connection strings in exceptions by default. Check if:
- You have detailed exception pages enabled in development (which is normal) but some custom middleware or EF Core logging settings are accidentally including the connection string.
- You have any third-party libraries or custom code that's capturing exceptions and leaking sensitive data.
4. Quick troubleshooting steps
Try these simple fixes first to rule out common issues:
- Clean your solution, delete the
binandobjfolders, then rebuild from scratch. .NET 7's incremental compilation can sometimes leave behind wonky cached files that cause random errors. - If you've implemented a custom
UserStoreor extendedUserManager, double-check your code for places where you might be accidentally passing the connection string as an argument (which would get included in exception messages). - Create a minimal reproduction project: Spin up a fresh ASP.NET Core 7 Web App with default Identity, then call
UserManager.GetUserAsync(User)in a controller action. If the error doesn't happen here, the issue is likely tied to custom code or dependencies in your existing project.
Should you roll back to .NET 6?
If none of the above fixes work, rolling back to .NET 6 is a valid temporary workaround—especially if this is blocking your work. But before you do, make sure you're on the latest .NET 7 patch version. Microsoft often patches edge-case bugs in minor updates, so this might already be fixed.
备注:内容来源于stack exchange,提问作者Epic Gamer

