gpt4 book ai didi

c# - ASP.net identity : How to get current IdentityUser (ApplicationUser)? UserManager.FindById 在哪里?

转载 作者:太空狗 更新时间:2023-10-29 20:58:34 25 4
gpt4 key购买 nike

我从 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”:

Intellisense open with Fi .

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

Intellisense open with this.Fi

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

最佳答案

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

你应该添加

using Microsoft.AspNet.Identity;

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

关于c# - ASP.net identity : How to get current IdentityUser (ApplicationUser)? UserManager.FindById 在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25483227/

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