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

查看:669
本文介绍了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.

在文档中,这看起来非常简单:<一href=\"http://blogs.msdn.com/b/webdev/archive/2013/10/16/customizing-profile-information-in-asp-net-identity-in-vs-2013-templates.aspx\" rel=\"nofollow\">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;
    }
}

任何帮助将是AP preciated!要不就告诉我在哪里FindById消失或如何让我的code的工作。还是有另一种方式来加载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?

ADDED

在用户管理器, FindById 没有找到,但 this.FindById 中找到。我会添加屏幕截图。这不是一个妥善的解决办法,因为我不明白,为什么发生这种情况,也可以有人解释这种现象?我附上2屏幕,智能感知开放。我也想提一提,它不是智能感知的一个问题 - 如果我不加code不能编译这个

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.

智能感知进入网络连接

.

智能感知输入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;

到code,开始使用非异步方法。

to your code to start using non-async methods.

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

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