【问题标题】:.Net MVC Identity 2 custom user store HttpContext.Curent is null.Net MVC Identity 2 自定义用户存储 HttpContext.Curent 为空
【发布时间】:2014-10-12 22:31:33
【问题描述】:

我有一个 IUserStore 的自定义实现,因为我想使用我的存储库层。我所有的数据库表都存储了插入或最后更新行的用户名。我从 HttpContext.Current 获取用户名,效果很好,但通过 UserManager 调用存储库时,我在 HttpContext.Current 中得到 null。当我自己的业务层调用存储库时,一切正常。 (我使用的是异步版本的方法。)

有人知道 UserManager 中发生了什么,为什么会丢失当前的 httpcontext?

我从帐户控制器调用用户服务:

var ir = await UserService.ChangePasswordAsync(User.Identity, vm.Password);

用户服务使用 UserManager 类保存新密码:

var ir = await Manager.PasswordValidator.ValidateAsync(password);
if (!ir.Succeeded)
    return ir;

user.PasswordHash = Manager.PasswordHasher.HashPassword(password);
ir = await Manager.UpdateAsync(user);

UserManager 使用我自己的 UserStore 实现:

public Task UpdateAsync(UserEntity user)
{
    return UserRepo.UpdateAsync(user);
}

在这里,当我使用调试器停止程序时,在 HttpContext.Current 中我有 null。这是一个问题,因为用户存储库尝试获取当前用户名。

【问题讨论】:

  • 为什么不获取当前登录的用户:User.Identity.Name?
  • User 属性在 mvc 的基本控制器中定义,因此只能从控制器访问。
  • 你能提供一些代码吗?
  • 我做了一些评论。

标签: .net asp.net-mvc asynchronous asp.net-identity httpcontext


【解决方案1】:

您可以通过将对象 [HttpContextBase context] 注入类的构造函数并在 context.User 对象中获取用户相关信息来解决此问题

【讨论】:

  • 对不起,我不明白你的意思。你能不能更详细地说明一下。同时,我在我的问题中添加了一些代码。
猜你喜欢
  • 2014-01-06
  • 1970-01-01
  • 2015-05-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-04-20
  • 2016-01-28
相关资源
最近更新 更多