使用 Entity Framework 的 ASP.NET Core 应用程序未使用集成安全性登录到 SQL 数据库

Posted

技术标签:

【中文标题】使用 Entity Framework 的 ASP.NET Core 应用程序未使用集成安全性登录到 SQL 数据库【英文标题】:ASP.NET Core Application using Entity Framework does not login to SQL database using integrated security 【发布时间】:2021-10-10 06:18:48 【问题描述】:

我创建了一个针对 .NET 5.0 的简单 ASP.NET Core MVC 项目,它从表单中获取数据并应该将其保存到 SQL Server 上的表中。我在现有数据库上使用实体框架核心代码优先。我已经根据microsoft 的这篇文章配置了应用程序。这是连接字符串。

data source=mynetworksqlserver;initial catalog=mycatalog;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework

当我保存更改时出现错误:

SqlException:用户“'”登录失败

在承载 SQL 的服务器上有一个 SQL Server 事件

“用户“”登录失败。原因尝试使用 SQL 身份验证登录失败。服务器配置为仅用于 Windows 身份验证。”

我不明白为什么应用程序没有使用集成安全性: 启动设置:

   "iisSettings": 
    "windowsAuthentication": true,
    "anonymousAuthentication": false,
    "iisExpress": 
      "applicationUrl": "http://localhost:57731",
      "sslPort": 44382
    ,
  "profiles": 
    "IIS Express": 
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": 
        "ASPNETCORE_ENVIRONMENT": "Development"
     ,
    "RC2": 
      "commandName": "Project",
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": 
        "ASPNETCORE_ENVIRONMENT": "Development" 
  

启动:

public void ConfigureServices(IServiceCollection services)
    
        services.AddControllersWithViews();
          services.AddDbContext<RevLogonModel>(options=>options.UseSqlServer(Configuration.GetConnectionString("RevLogon")));
        services.AddAuthentication(IISDefaults.AuthenticationScheme);
    

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        
      .....
            app.UseAuthentication();
            app.UseAuthorization();

这是堆栈跟踪的摘录,我确认它尝试使用用户名和密码而不是 Windows 安全性登录:

Microsoft.Data.SqlClient.SqlInternalConnectionTds.CompleteLogin(bool enlistOK) Microsoft.Data.SqlClient.SqlInternalConnectionTds.AttemptOneLogin(ServerInfo serverInfo,字符串 newPassword,SecureString newSecurePassword,bool ignoreSniOpenTimeout,TimeoutTimer 超时,bool withFailover) Microsoft.Data.SqlClient.SqlInternalConnectionTds.LoginNoFailover(ServerInfo serverInfo, string newPassword, SecureString newSecurePassword, bool redirectedUserInstance, SqlConnectionString connectionOptions, SqlCredential credential, TimeoutTimer timeout)

我从来没有遇到过使用 .NET 框架的 ASP.NET MVC 的这个问题。任何帮助或指导表示赞赏。

【问题讨论】:

可能是因为IIS 没有添加到授权的SQL Server 用户中? IOW,检查您的应用程序是在哪个用户下启动的Environment.UserName 感谢 ide Svyatosslav。当我通过VS运行它时,用户名确实是我的登录名。当我将它部署到 IIS 服务器时,我得到了池的名称。所以应用程序可以识别 windows 用户,但 EF 正在尝试使用空白用户名和密码登录。 【参考方案1】:

我解决了上面没有出现的代码中的问题。问题出在我的上下文课程中。我使用了一些包含错误连接字符串的锅炉制造商代码。上下文类中的代码覆盖了启动类中的代码,因此控制器忽略了启动配置中的连接字符串,并在上下文类中使用了不正确的连接字符串。这是导致问题的原始代码:

   public partial class mycontext: DbContext
    
        public mycontext(DbContextOptions<mycontext> options)
            : base(options)
        
        

        public virtual DbSet<reviewer> reviewers  get; set; 
        public virtual DbSet<RevcommentFile> RevcommentFiles  get; set; 
        protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
        
            if (!optionsBuilder.IsConfigured)
            
                optionsBuilder
                  .UseSqlServer(@"Data Source=mynetworksqlserver; Initial Catalog=mycatalog;");
            
        
     

将上下文类中的连接字符串替换为正确的连接字符串(见问题)解决了问题。

【讨论】:

以上是关于使用 Entity Framework 的 ASP.NET Core 应用程序未使用集成安全性登录到 SQL 数据库的主要内容,如果未能解决你的问题,请参考以下文章

更新 ASP.NET Core Entity Framework 中的实体类

如何在 ASP.NET 5 中将 Entity Framework 6 与 MySQL 一起使用?

如何使用 Entity Framework ASP.Net MVC 5 删除多条记录? [关闭]

仍待改进,EF团队不建议在ASP.NET 4中使用Entity Framework 7

如何在 ASP.NET MVC 5、Entity Framework 6 中使用流利的 API 映射表?

使用 SSMS 删除了本地数据库表,无法使用 ASP.NET Entity Framework 重新创建它