繁体   English   中英

在ASP.Net Webforms Web API中获取当前用户的IdentityUser FullName

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

嗨,我在我的模型中有这个:

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

在我的控制器中,我使用: string userId=HttpContext.Current.User.Identity.GetUserId(); 获取当前用户的ID,但是如何获取当前用户的FullName属性,请帮助。

通过使用Owin上下文管理器,您可以在任何地方获取用户管理器对象。 并且在用户管理器的帮助下,您可以通过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;

使用身份,您应该使用UserManager类进行此类操作。 默认的MVC模板还将创建它自己的继承版本,称为ApplicationUserManager ,因此假设您尚未删除/更改该模板:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM