Dev pro
Dev pro

Reputation: 630

Get IdentityUser FullName of the current User in ASP.Net Webforms Web api

Hi guys i have this in my model:

   public class ApplicationUser : IdentityUser
    {
        public string FullNamme { get; set; }


        public ClaimsIdentity GenerateUserIdentity(ApplicationUserManager manager)
        {
            // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
            var userIdentity = manager.CreateIdentity(this, DefaultAuthenticationTypes.ApplicationCookie);
            // Add custom user claims here
            return userIdentity;
        }

        public Task<ClaimsIdentity> GenerateUserIdentityAsync(ApplicationUserManager manager)
        {
            return Task.FromResult(GenerateUserIdentity(manager));
        }
    }

    public class ApplicationDbContext : IdentityDbContext<ApplicationUser>
    {
        public ApplicationDbContext()
            : base("DefaultConnection", throwIfV1Schema: false)
        {
        }

        public static ApplicationDbContext Create()
        {
            return new ApplicationDbContext();
        }
    }

In my controller i Use : string userId=HttpContext.Current.User.Identity.GetUserId(); to get the id of the current user, but how can i get the property FullName for the current user please need your help .

Upvotes: 0

Views: 4689

Answers (2)

Sam FarajpourGhamari
Sam FarajpourGhamari

Reputation: 14741

You could get user manager object every where by use of Owin context manager. And by help of user manager you could get user object by ID:

//make sure you added this line in the using section
using Microsoft.AspNet.Identity.Owin

string fullname = HttpContext.Current.GetOwinContext()
    .GetUserManager<ApplicationUserManager>()
    .FindById(HttpContext.Current.User.Identity.GetUserId()).FullName;

Upvotes: 4

DavidG
DavidG

Reputation: 118947

With Identity you should use the UserManager class for this sort of operation. The default MVC template will also create it's own inherited version called ApplicationUserManager, so assuming you haven't removed/changed that:

var userId = HttpContext.Current.User.Identity.GetUserId();
var userManager = ApplicationUserManager.Create();
var user = await userManager.FindByIdAsync(userId);
string fullName = user.FullName;

Upvotes: 0

Related Questions