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

查看:199
本文介绍了如何在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 StudioPackage-Manager Console中的Update-Database命令更新了数据库表,以确保ApplicationUser classAspNetUsers表之间的一致性.我已经确认AspNetUsers表中现在存在名为DisplayName的新列.

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.

现在,我想使用该DisplayName代替默认的UserName作为原始_LoginPartial.cshtml View中的文本.但是如您所见:

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.Identity具有GetUserId以及NameAuthenticationType等...但是如何获取我的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天全站免登陆