【问题标题】:nhibernate session with custom data and event listeners具有自定义数据和事件侦听器的休眠会话
【发布时间】:2013-01-08 05:15:28
【问题描述】:

对于我正在处理的 WCF 项目,我需要为我们保留的实体实施某种审核日志。基本上,审计跟踪将包含 4 个必填字段

  • 创建日期时间
  • CreatedUserID
  • 更新日期时间
  • 更新用户 ID

我正在尝试通过我的 DataAccess 层中的 nHibernate 事件侦听器来实现这一点,因为我认为这不应该在域层中。到目前为止,我已经让 DateTime 的东西按预期工作,但还没有弄清楚如何在事件侦听器中检索用户 ID。理想情况下,我希望将用户 ID 作为某种自定义数据附加到 nHibernate 会话对象。

非常感谢任何建议。

【问题讨论】:

    标签: wcf nhibernate auditing audit-trail


    【解决方案1】:

    .Net 框架已经内置了对上下文用户身份信息的支持:Thread.CurrentPrincipal http://msdn.microsoft.com/en-us/library/system.threading.thread.currentprincipal.aspx

    使用可用的 IPrincipal 实现之一或创建您自己的实现 - 这很容易。然后在一些“begin-request”方法中尽早设置属性。

    在 ASP.NET 代码中还需要注意 HttpContext.User。

    【讨论】:

      【解决方案2】:

      这是我的做法,但我没有使用 WCF 执行此操作的经验。请注意,这需要引用 System.Web。

          /// <summary>
          ///   Returns the user name of the current user. Gets user name from HttpContext if running as a web app, else WindowsIdentity.
          /// </summary>
          private static string GetUserName()
          {
              var identity = HttpContext.Current == null ? WindowsIdentity.GetCurrent() : HttpContext.Current.User.Identity;
              if (identity == null)
              {
                  throw new Exception("Identity could not be determined.");
              }
              // Remove domain name if present
              var s = identity.Name;
              var stop = s.IndexOf("\\", StringComparison.InvariantCultureIgnoreCase);
              return (stop > -1) ? s.Substring(stop + 1, s.Length - stop - 1).ToUpper() : s;
          }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2011-07-06
        • 2015-04-05
        • 2018-08-28
        • 1970-01-01
        • 2017-08-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多