在 .NET Core 2.1 中将 IIS 基本身份验证传递给 SQL 服务器

Passing IIS Basic Authentication to SQL Server in .NET Core 2.1

这个问题的一些背景知识:

我有一个 ASP.NET Intranet 应用程序可以访问 MS SQL 服务器数据库中的敏感数据。由于数据的性质,数据库 table 本身仅供 select 用户使用。我们在 IIS 和模拟中使用基本身份验证,以便在访问数据时使用集成安全性。

一切正常,但我们现在正在将 Intranet 站点转换为 .NET Core。我知道 Core 不直接支持我们正在使用的那种模拟,但是是否有任何可用的选项或解决方法可以使此工作正常进行?

您有两个选择:

  1. 尝试 WindowsIdentity.RunImpersonated,它将使用当前用户身份登录到 sql 服务器。

        public IActionResult About()
    {
        IList<Blog> blogs = new List<Blog>();
        var user = (WindowsIdentity)User.Identity;
        WindowsIdentity.RunImpersonated(user.AccessToken, () =>
        {
            var impersonatedUser = WindowsIdentity.GetCurrent();
            blogs = _context.Blogs.ToList();
        });
        return Ok(blogs);
    
        //_context
        //ViewData["Message"] = "Your application description page.";
    
        //return View();
    }
    
  2. 可以运行iis中的用户账号下的.net core项目有权限访问sql服务器,然后查看用户身份是否有在访问将调用数据库操作方法的控制器之前动态访问 sql 服务器的权限。