【问题标题】:ASP.NET, IIS and SQL Server 2008ASP.NET、IIS 和 SQL Server 2008
【发布时间】:2017-09-29 22:17:12
【问题描述】:

我的场景是:我有 3 台单独的机器用于域控制器 (Windows 2008)、SQL Server 2008 和 IIS (Windows 7)。

我正在使用 SQL Server 触发器(删除、更新、插入)维护表日志, 通过将suser_name() 设置为默认值,例如:

  • 表 1:TrNo Bigint、TrDate 日期时间、CDate 作为日期时间
  • Table1_Log:TrNo Bigint、TrDate Datetime、CDate as datetime、AuditUser Varchar(100) 默认 suser_name()

每当用户更新任何记录时,它都会维护其日志表。我的问题是它总是在配置为 IIS 的日志表中显示相同的 ComputerName。

我在 IIS 中做了什么:

Application - 
               Anonymous Authentication  - Disabled
               ASP.Net Impersonation     - Disabled
               Basic Authentication      - Disabled
               Form Authentication       - Disabled    
               Windows Authentication    - Enabled
Application Pools - 
               Default Application Pool
                              Identity - Network Services

--------------------------------------------------------------

现在,当我运行该应用程序时,它会显示一个 Windows 对话框,其中用户输入了他的 Domain\UserName,当我编辑任何记录时,它会将我的计算机名称保存到 AuditUser 列(即:suser_name())。

我想保存在 Windows 对话框中输入的实际用户名。 suser_name()

谢谢

【问题讨论】:

    标签: asp.net sql-server-2008 iis-7


    【解决方案1】:

    您的应用程序以 IIS 应用程序池中指定的身份运行。为了让它以个人身份连接到 SQL Server,您需要启用模拟。

    这涉及在您的 web.config 中启用模拟

    <system.web>
        ...
        <authentication mode="Windows"/>
        <identity impersonate="true"/>
        ...
    </system.web>
    

    然后在您的代码中,您使用 WindowsImpersonationContext 来包装您想要模拟该用户的代码部分(例如,在您的情况下,在您的数据库调用期间)。

    var identity = User.Identity as WindowsIdentity;
    
    using (var context = identity.Impersonate())
    {
        // Do stuff
    }
    

    请记住,这意味着所有这些用户都需要数据库的访问权限。

    【讨论】:

    • 感谢您在 Connection.open() 上快速回复“NT AUTHORITY\ANONYMOUS LOGON”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-05
    • 2011-01-16
    • 1970-01-01
    • 2014-02-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多