【问题标题】:ASP.net identity: How to get current IdentityUser (ApplicationUser)? Where is UserManager.FindById?ASP.net 身份:如何获取当前的 IdentityUser (ApplicationUser)? UserManager.FindById 在哪里?
【发布时间】:2014-08-25 09:48:41
【问题描述】:

我从 VS2013 中 ASP.net 的默认模板开始。我想获取当前的用户对象。这应该可以在不直接访问数据库的情况下实现。

在文档中,这看起来很简单:http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx

应该是这样的

var currentUser = manager.FindById(User.Identity.GetUserId()); 

但是 FindById 不见了!几个小时以来,我一直在尝试改用 FindByIdAsync。但我想我遇到了死锁。

public class UserManager : UserManager<IdentityUser>
{
    public UserManager()
        : base(new UserStore<IdentityUser>(new ApplicationDbContext()))
    {
    }

    public async System.Threading.Tasks.Task<IdentityUser> GetCurrentUser()
    {
        var user = await FindByIdAsync(HttpContext.Current.User.Identity.Name);
        return user;
    }
}

调用属性:

private IdentityUser_CurrentUser;
protected IdentityUser CurrentUser
{
    get
    {
        if (_CurrentUser == null)
        {                   
            var manager = new UserManager();
            var result = manager.GetCurrentUser();
            //-- STOPS HERE!!
            _CurrentUser = result.Result;
        }
        return _CurrentUser;
    }
}

任何帮助将不胜感激!要么告诉我 FindById 去了哪里,要么如何让我的代码工作。还是有另一种加载 IdentityUser 的方法?

添加

在用户管理器中,没有找到FindById,但是找到了this.FindById。我将添加屏幕截图。这不是一个合适的解决方案,因为我不明白为什么会这样,或者有人可以解释这种行为吗?我附加了 2 个打开智能感知的屏幕。我还想提一下,这不是智能感知的问题——如果我不添加this.,代码将无法编译

智能感知输入“Fi”:

.

智能感知输入“this.Fi”:

这样,至少我不再卡住了。

【问题讨论】:

  • 不确定这是否相关,但class UserManager : UserManager&lt;IdentityUser&gt; 总是让我感到困惑。为什么不class MyUserManager : UserManager&lt;IdentityUser&gt;
  • 什么类的调用属性?您是否直接从控制器尝试过代码,并将管理器实例化为var manager = new UserManager&lt;IdentityUser&gt;(new UserStore&lt;IdentityUser&gt;(context));,然后使用 FindByIdAsync。见鬼,UserManager 怎么能知道诸如 HttpContext 之类的想法???
  • class MyUserManager : UserManager 取自教程asp.net/web-forms/tutorials/aspnet-45/…。到目前为止,它一直运行良好。
  • 我认为我的块与此有关:blog.stephencleary.com/2012/07/dont-block-on-async-code.html 但是将我的整个母版页标记为异步只是为了读取用户对象对我来说似乎不合适。
  • 我想你找到了问题所在。我只是补充一点,最新的 ASP MVC(官方)模板在 AccountController 中使用 async 方法,而该方法使用 await

标签: c# asp.net webforms asp.net-identity


【解决方案1】:

FindById 是来自Microsoft.AspNet.Identity.UserManagerExtensions 类的扩展方法。它是Microsoft.AspNet.Identity.Core nuget 包的一部分。

你应该添加

using Microsoft.AspNet.Identity;

到您的代码开始使用非异步方法。

【讨论】:

  • 另外,使用 Visual Studio 2013 Update 3 及更高版本,只需使用个人帐户创建一个新的 MVC 项目,您就可以看到 FindById 的大量使用
  • 命名空间已包含在内。这些是我的命名空间:using Microsoft.AspNet.Identity; using Microsoft.AspNet.Identity.EntityFramework; using Microsoft.Owin.Security; using System; using System.Collections.Generic; using System.ComponentModel.DataAnnotations.Schema; using System.Data.Entity; using System.Web;
  • 正如我在问题中提到的,这是一个 ASP.net Web Forms 项目,没有 MVC。但我想我找到了解决方案。我将编辑我的问题。
  • 感谢 Rick,它帮助我了解了如何正确使用它。 ApplicationDbContext applicationDbContext = new ApplicationDbContext(); UserManager userManager = new UserManager(new UserStore(applicationDbContext)); var user = userManager.FindById(Context.User.Identity.GetUserId());
  • 之前的评论有一些关于子自定义对象没有为 ApplicationUser 加载的问题。这是一个更好的代码:private static ApplicationUserManager _userManager = HttpContext.Current.GetOwinContext().GetUserManager&lt;ApplicationUserManager&gt;(); var user = _userManager.FindByName&lt;ApplicationUser, string&gt;(userName);
猜你喜欢
  • 2014-01-22
  • 2018-10-30
  • 2014-05-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多