如何在 ASP.Net MVC 5 视图中获取 ApplicationUser 的自定义属性值? [英] How to Get Custom Property Value of the ApplicationUser in the ASP.Net MVC 5 View?

查看:21
本文介绍了如何在 ASP.Net MVC 5 视图中获取 ApplicationUser 的自定义属性值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

ASP.Net MVC 5 中,ApplicationUser 可以扩展为具有自定义属性.我已经扩展了它,现在它有一个名为 DisplayName 的新属性:

In the ASP.Net MVC 5, the ApplicationUser can be extended to have custom property. I have extended it such that it now has a new property called DisplayName:

// You can add profile data for the user by adding more properties to your ApplicationUser class, please visit http://go.microsoft.com/fwlink/?LinkID=317594 to learn more.
public class ApplicationUser : IdentityUser {
    public string ConfirmationToken { get; set; }
    public string DisplayName { get; set; } //here it is!

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

我也在Visual Studio中的Package-Manager Console中使用Update-Database命令更新了数据库表,以确保两者之间的一致性ApplicationUser classAspNetUsers 表.我已经确认名为 DisplayName 的新列现在存在于 AspNetUsers 表中.

I also have updated the database table using Update-Database command in the Package-Manager Console in Visual Studio to ensure the consistency between the ApplicationUser class and the AspNetUsers table. I have confirmed that the new column called DisplayName is now exist in the AspNetUsers table.

现在,我想对原始 _LoginPartial.cshtml View 中的文本使用该 DisplayName 而不是默认的 UserName.但正如你所看到的:

Now, I want to use that DisplayName instead of the default UserName for the text in the original _LoginPartial.cshtml View. But as you can see:

<ul class="nav navbar-nav navbar-right">
  <li>
    @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
  </li>
  <li><a href="javascript:document.getElementById('logoutForm').submit()">Log off</a></li>
</ul>

原来的_LoginPartialView.cshtml 是使用User.Identity.GetUserName() 来获取ApplicationUserUserName代码>.User.IdentityGetUserIdNameAuthenticationType 等......但是我如何得到我的DisplayName 用于显示?

The original _LoginPartialView.cshtml is using User.Identity.GetUserName() to get the UserName of the ApplicationUser. The User.Identity has GetUserId and also Name, AuthenticationType, etc... But how do I get my DisplayName for display?

推荐答案

在 ClaimsIdentity 中添加声明:

Add the claim in ClaimsIdentity:

public class ApplicationUser : IdentityUser
{
    ...

    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("DisplayName", DisplayName));
        return userIdentity;
    }
}

创建了从 ClaimsIdentity 读取 DisplayName 的扩展方法:

Created an extention method to read DisplayName from ClaimsIdentity:

public static class IdentityExtensions
{
    public static string GetDisplayName(this IIdentity identity)
    {
        if (identity == null)
        {
            throw new ArgumentNullException("identity");
        }
        var ci = identity as ClaimsIdentity;
        if (ci != null)
        {
            return ci.FindFirstValue("DisplayName");
        }
        return null;           
    }
}

在您看来,像这样使用它:

In your view use it like:

User.Identity.GetDisplayName()

这篇关于如何在 ASP.Net MVC 5 视图中获取 ApplicationUser 的自定义属性值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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