[英]ASP.net identity: How to get current IdentityUser (ApplicationUser)? Where is UserManager.FindById?
I started with the default template for ASP.net in VS2013. 我从VS2013中的ASP.net默认模板开始。 I want to get the current user object.
我想获取当前的用户对象。 This should be possible without directly accessing the database.
这可以在不直接访问数据库的情况下实现。
In the documentation, this looks very easy: http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx 在文档中,这看起来非常简单: http : //blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013 -templates.aspx
So it should be 所以它应该是
var currentUser = manager.FindById(User.Identity.GetUserId());
But FindById is missing! 但是缺少FindById! Since several hours, I have been trying to use FindByIdAsync instead.
几个小时后,我一直在尝试使用FindByIdAsync。 But I think I get a dead lock.
但我想我得到了死锁。
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;
}
}
The calling propery: 调用的属性:
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;
}
}
Any help would be appreciated! 任何帮助,将不胜感激! Either to show me where FindById is gone or how to make my code work.
要么向我展示FindById已经消失或如何使我的代码工作。 Or is there another way to load the IdentityUser?
或者是否有另一种加载IdentityUser的方法?
ADDED 添加
In the user manager, FindById
is not found, but this.FindById
is found. 在用户管理器中,
FindById
,但找到this.FindById
。 I will add the screenshots. 我将添加截图。 This is not a proper solution because I do not understand, why this is happening, or can someone explain this behaviour?
这不是一个正确的解决方案,因为我不明白,为什么会发生这种情况,或者有人可以解释这种行为? I attach 2 screens with intellisense open.
我附上两个智能感知屏幕。 I also want to mention, that it is not a problem of intellisense - the code does not compile if I do not add
this.
我还想提一下,这不是intellisense的问题 - 如果我不添加
this.
,代码就不会编译this.
Intellisense entering "Fi": 智能感知输入“Fi”:
.
。
Intellisense entering "this.Fi": Intellisense进入“this.Fi”:
This way, at least I am not stuck any more. 这样,至少我不再被困了。
FindById
is an extension method coming from Microsoft.AspNet.Identity.UserManagerExtensions
class. FindById
是一个来自Microsoft.AspNet.Identity.UserManagerExtensions
类的扩展方法。 It is a part of Microsoft.AspNet.Identity.Core
nuget package. 它是
Microsoft.AspNet.Identity.Core
nuget包的一部分。
You should add 你应该添加
using Microsoft.AspNet.Identity;
to your code to start using non-async methods. 您的代码开始使用非异步方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.