User.Identity.Name全名mvc5

Ash*_*ran 23 asp.net-mvc asp.net-identity

我通过在ApplicationUser派生自的类中添加几个字段来扩展ASP NET标识架构IdentityUser.我添加的领域之一是FullName.

现在,当我写的时候User.Identity.Name,它给了我用户名,我正在寻找类似的东西User.Identity.FullName应该返回我添加的FullName.

不确定,如何实现这一点,我们将非常感谢任何指导.

谢谢.

Hao*_*ung 26

您可以在创建用户时将其添加到用户的声明中,然后将其作为来自User.Identity的声明进行检索:

await userManager.AddClaimAsync(user.Id, new Claim("FullName", user.FullName));
Run Code Online (Sandbox Code Playgroud)

撤回它:

((ClaimsIdentity)User.Identity).FindFirst("FullName")
Run Code Online (Sandbox Code Playgroud)

或者您可以直接获取用户并从user.FullName访问它:

var user = await userManager.FindById(User.Identity.GetUserId())
return user.FullName
Run Code Online (Sandbox Code Playgroud)

  • 谢谢你的解决方案.我想知道,如果我将这些数据存储为声明,它是否会被添加到身份验证cookie?我想在我的视图中始终提供一些用户数据来集成intercom.io.谢谢 (2认同)

Mar*_*cel 22

在ApplicationUser类中,您会注意到一条评论(如果您使用标准MVC5模板),其中显示"在此处添加自定义用户声明".

鉴于此,这是添加FullName的样子:

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

    public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
    {
        // Note the authenticationType must match the one defined in CookieAuthenticationOptions.AuthenticationType
        var userIdentity = await manager.CreateIdentityAsync(this, DefaultAuthenticationTypes.ApplicationCookie);
        // Add custom user claims here
        userIdentity.AddClaim(new Claim("FullName", this.FullName));
        return userIdentity;
    }
}
Run Code Online (Sandbox Code Playgroud)

使用此功能,当有人登录时,FullName声明将被放入cookie中.你可以帮助它像这样访问它:

    public static string GetFullName(this System.Security.Principal.IPrincipal usr)
    {
        var fullNameClaim = ((ClaimsIdentity)usr.Identity).FindFirst("FullName");
        if (fullNameClaim != null)
            return fullNameClaim.Value;

        return "";
    }
Run Code Online (Sandbox Code Playgroud)

并使用这样的帮助:

@using HelperNamespace
...
@Html.ActionLink("Hello " + User.GetFullName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
Run Code Online (Sandbox Code Playgroud)

请注意,自定义用户声明存储在cookie中,这比从数据库获取用户信息更好...为常用数据保存数据库命中.

  • @Decoder94 - 在辅助工具或实用工具类中,然后将命名空间添加到视图中.在我的例子中,我在Models文件夹中有一个名为Utilities.cs的类.我在那里放了`GetFullName()`方法.然后我将`@using myProject.Models`添加到视图中. (3认同)
  • 我在哪里放:public static string GetFullName(this System.Security.Principal.IPrincipal usr) (2认同)

H20*_*der 9

我发现这很好用

的AccountController:

private async Task SignInAsync(ApplicationUser user, bool isPersistent)
    {
        AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
        var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
        identity.AddClaim(new Claim("FullName", user.FullName));
        identity.AddClaim(new Claim("Email", user.Email));
        identity.AddClaim(new Claim("DateCreated", user.DateCreated.ToString("MM/dd/yyyy")));
        AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
    }
Run Code Online (Sandbox Code Playgroud)

身份的扩展方法:

 public static class GenericPrincipalExtensions
{
    public static string FullName(this IPrincipal user)
    {
        if (user.Identity.IsAuthenticated)
        {
            ClaimsIdentity claimsIdentity = user.Identity as ClaimsIdentity;
            foreach (var claim in claimsIdentity.Claims)
            {
                if (claim.Type == "FullName")
                    return claim.Value;
            }
            return "";
        }
        else
            return "";
    }
}
Run Code Online (Sandbox Code Playgroud)

在您的视图中:

 @Html.ActionLink("Hello " + User.FullName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
Run Code Online (Sandbox Code Playgroud)

你可以看一下这里的主题: 链接