简体   繁体   English

ASP.net身份:如何获取当前的IdentityUser(ApplicationUser)? UserManager.FindById在哪里?

[英]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打开Fi .

Intellisense entering "this.Fi": Intellisense进入“this.Fi”:

Intellisense打开了这个.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.

相关问题 ASP.NET MVC 5 - 身份。 如何获取当前的ApplicationUser - ASP.NET MVC 5 - Identity. How to get current ApplicationUser ASP.NET Core Identity不会注入UserManager <ApplicationUser> - ASP.NET Core Identity does not inject UserManager<ApplicationUser> 使用 ApplicationUser 时出现问题:IdentityUser, UserManager in clean architecture asp.net core - Trouble when using ApplicationUser: IdentityUser, UserManager in clean architecture asp.net core ASP.NET Core 标识用户管理器<ApplicationUser> -Error 没有为此对象定义无参数构造函数 - ASP.NET Core Identity UserManager<ApplicationUser> -Error No parameterless constructor defined for this object ASP.NET Identity 2 UserManager 异步获取所有用户 - ASP.NET Identity 2 UserManager get all users async 从ASP.NET MVC中的类获取当前的ApplicationUser - Get current ApplicationUser from class in ASP.NET MVC 如何模拟 Asp.net 身份 UserManager 的 CreateAsync 方法 - How to mock Asp.net identity UserManager's CreateAsync method Asp.Net身份用户管理器和工作单元-如何配对? - Asp.Net Identity Usermanager and Unit Of Work - how to mate them? ASP.net Identity UserManager 未验证密码 - ASP.net Identity UserManager not verifying passwords 使用ASP.NET Identity UserManager进行事务 - Transactions with ASP.NET Identity UserManager
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM