SqlException:在新创建的 ASP.NET Core 2.2 应用中用户"xx\yy"登录失败



我使用Visual Studio 2017(15.9.7(向导创建了一个新的ASP.NET Core 2.2 MVC应用程序。我指定了身份验证的单个帐户选项。

之后,我添加到默认模板的唯一一件事是Startup.ConfigureServices方法中的services.AddAuthentication().AddGoogle

运行该应用程序(Kestrel和iisexpress下的同一问题(,当Google授权完成时,我会得到此例外:

处理请求时发生了一个未经治疗的例外。sqlexception:用户" xx yy"登录失败。

我的appsettings.json就像是由向导创建的:

{
  "ConnectionStrings": {
    "DefaultConnection": "Server=(localdb)\mssqllocaldb;Database=aspnet-WebApplicationNetCore2-7F2E7A20-8AE2-4AB3-833A-256AC218C69E;Trusted_Connection=True;MultipleActiveResultSets=true"
  },
  "Logging": {
    "LogLevel": {
      "Default": "Warning"
    }
  },
  "AllowedHosts": "*"
}

在Google上找到了数十种解决方案,但它们都不对我有用。

我尝试设置trusted_connection = false,在nuget控制台中运行更新数据库,但发生了相同的错误。

找到了在Initialize方法中运行context.Database.EnsureCreated的建议(ASP.NET Core:无法打开登录要求的LOCALDB(,但我什至在启动类中根本没有此方法。

我还发现" aspnet-webapplicationnetcore2-7f2e7a20-8ae2-4ab3-833a-256aac218c69e*。这是否意味着向导根本没有创建数据库?甚至没有尝试吗?还是尝试过但是失败了?不确定如何确定。

我正在为项目使用身份。涉及AppIdentity的行/块包括:

  • 在Configureservices((中:
services.AddDbContext<AppIdentityDbContext>
     (options => options.UseSqlServer(
          Configuration["Data:Identity:ConnectionString"]    
     )); // could use raw connection string here. This just references appsetting.json
services.AddIdentity<IdentityUser, IdentityRole>(
     opts =>
     {
          opts.Password.RequiredLength = 4;
          opts.Password.RequireDigit = false;
          opts.Password.RequireUppercase = false;
          opts.Password.RequireNonAlphanumeric = false;
     })
     .AddEntityFrameworkStores<AppIdentityDbContext>();
// unsure, but these might be necessary
services.AddMemoryCache();
services.AddSession();
  • 在configure((中:
app.UseSession(); // only use if session and memory cache are indeed necessary
app.UseIdentity(); // I get a warning to use UseAuthentication(), because this will be replaced in the future, but it works for now

我不确定这是您需要的,但我希望它有帮助。祝你好运!

最新更新