gpt4 book ai didi

asp.net-mvc - 经过身份验证的用户和服务层

转载 作者:行者123 更新时间:2023-12-05 01:21:38 25 4
gpt4 key购买 nike

我有一个 MVC 4 应用程序,它使用不同类库中的服务层。

对该服务层的某些调用需要知道哪些用途正在请求数据。

数据记录因用户角色而异。

为了防止耦合问题,我应该在请求中传递用户名 (HttpContext.User.Identity.Name) 还是应该使用相同的 HttpContext.User.Identity 在服务层直接访问它.姓名.

我不确定是否应该从服务层隐藏 HttpContext。

最佳答案

只需将当前经过身份验证的用户作为参数传递给您的服务层。切勿在服务层中使用 HttpContext.User.Identity.Name

例如:

[Authorize]
public ActionResult SomeAction()
{
string user = User.Identity.Name;
this.someService.SomeOperation(user);
...
}

您的服务层永远不应绑定(bind)到 HttpContext

关于asp.net-mvc - 经过身份验证的用户和服务层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14682893/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com