ASP.net 身份:如何获取当前的 IdentityUser (ApplicationUser)?UserManager.FindById 在哪里? [英] ASP.net identity: How to get current IdentityUser (ApplicationUser)? Where is UserManager.FindById?

查看:21
本文介绍了ASP.net 身份:如何获取当前的 IdentityUser (ApplicationUser)?UserManager.FindById 在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I started with the default template for ASP.net in VS2013. I want to get the current user object. This should be possible without directly accessing the database.

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

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

应该是这样的

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

但是 FindById 丢失了!几个小时以来,我一直在尝试使用 FindByIdAsync.但我想我得到了一个死锁.

But FindById is missing! Since several hours, I have been trying to use FindByIdAsync instead. 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;
    }
}

调用属性:

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?

Any help would be appreciated! Either to show me where FindById is gone or how to make my code work. Or is there another way to load the IdentityUser?

添加

在用户管理器中,没有找到FindById,但是找到了this.FindById.我会添加截图.这不是一个正确的解决方案,因为我不明白为什么会发生这种情况,或者有人可以解释这种行为吗?我附加了 2 个打开智能感知的屏幕.我还想提一下,这不是智能感知的问题 - 如果我不添加 this.

In the user manager, FindById is not found, but this.FindById is found. 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.

智能感知输入Fi":

.

Intellisense 输入this.Fi":

Intellisense entering "this.Fi":

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

This way, at least I am not stuck any more.

推荐答案

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

FindById is an extension method coming from Microsoft.AspNet.Identity.UserManagerExtensions class. It is a part of Microsoft.AspNet.Identity.Core nuget package.

你应该添加

using Microsoft.AspNet.Identity;

添加到您的代码以开始使用非异步方法.

to your code to start using non-async methods.

这篇关于ASP.net 身份:如何获取当前的 IdentityUser (ApplicationUser)?UserManager.FindById 在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆