【问题标题】:Business Logic Layer - Getting the Current User业务逻辑层 - 获取当前用户
【发布时间】:2015-02-22 13:11:46
【问题描述】:

我有一个业务逻辑层和 2 个使用它的应用程序,一个 MVC UI 和一个 Web API 项目。

我的类的属性之一是 CreatedBy 和 UpdatedBy,它们应该保存用户 ID。

    public Nullable<System.DateTime> CreatedTS { get; set; }
    public string CreatedBy { get; set; }
    public Nullable<System.DateTime> UpdatedTS { get; set; }
    public string UpdatedBy { get; set; }

鉴于 BLL 有多个使用者,捕获 userId 的最佳方法是什么?

A) - 在 BLL 中使用 Environment.UserName? 设置它?

B) - 要求客户端设置并使用模型数据注释使此为必填项

C) - 要求客户端将此传递给 BLL 中的任何 Create 或 Update 方法。

【问题讨论】:

    标签: c# asp.net architecture


    【解决方案1】:

    我一般会使用Thread.CurrentPrincipal.Identity.Name

    为此,您必须确保将Thread.CurrentPrincipal 设置为代表当前用户的主体。这是在 UI 层完成的,如果您:

    【讨论】:

    • 那么在访问 BLL 之前在 UI/Client 中设置这个?
    • @JackRussell,是的,将其设置为代表 UI 层中经过身份验证的用户的主体。
    【解决方案2】:

    如果您在 MVC 和 WebApi 中都使用 FormsAuthentication,则可以访问属性 User.Identity.Name

    int userId = db.Users.Single(r=>r.Name == User.Identity.Name);
    

    在 WebApi 中它将是 HttpContext.Current.User.Identity.Name

    这种方法非常安全。如果您在客户端存储userId,用户将能够对其进行修改。

    【讨论】:

    • 认证方式为无&lt;authentication mode="None"/&gt;。我们有一个在 IIS 之上运行的外部服务,它提供我们的身份验证服务。我只需要帮助设计设置用户标识的位置。 BLL 还是客户?
    • 如果当前用户未经授权,您如何确定当前用户?
    • 我们有一个运行在 IIS 之上的外部服务,它提供我们的身份验证服务。我只需要帮助设计设置用户标识的位置。 BLL 还是客户?
    • 而 BLL 应该读取 userID 属性并在内部设置它,以便客户端不知道?
    • 您的身份验证服务应确定userId 并将其进一步传递给BLL。
    猜你喜欢
    • 2010-12-18
    • 1970-01-01
    • 2011-12-03
    • 2011-11-26
    • 2017-04-29
    • 2016-08-12
    • 2013-05-18
    • 2014-09-04
    • 2013-02-18
    相关资源
    最近更新 更多