Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.net identity: How to get current IdentityUser (ApplicationUser)? Where is UserManager.FindById?

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.

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

So it should be

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

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;
    }
}

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. Or is there another way to load the IdentityUser?

ADDED

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.

Intellisense entering "Fi":

Intellisense open with Fi.

Intellisense entering "this.Fi":

Intellisense open with this.Fi

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

like image 506
Tillito Avatar asked Aug 25 '14 09:08

Tillito


People also ask

How to get current user id asp net Core Identity?

You can create a method to get the current user : private Task<ApplicationUser> GetCurrentUserAsync() => _userManager. GetUserAsync(HttpContext. User);

How can we get current user claims in asp net identity in view?

GenerateUserIdentityAsync(manager), getUserIdCallback: (id) => (id. GetUserId<int>()) ) } }); Then, you can access the claim (in a view or in a controller):

How do I get current user in .NET core Web API?

AddTransient<IPrincipal>(provider => provider. GetService<IHttpContextAccessor>(). HttpContext. User);

How to get current logged in user c#?

GetCurrent(). Name; Returns: NetworkName\Username. Gets the user's Windows logon name.


1 Answers

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

You should add

using Microsoft.AspNet.Identity;

to your code to start using non-async methods.

like image 105
trailmax Avatar answered Oct 18 '22 20:10

trailmax