如何在ASP.Net MVC 5视图中获取ApplicationUser的自定义属性值?

时间:2016-08-09 09:23:01

标签: c# asp.net asp.net-mvc asp.net-mvc-5 asp.net-identity

with test(id, name, score) as ( select 1,'Alex',10 from dual union all select 2,'Jim',20 from dual union all select 3,'Ming',12 from dual union all select 4,'Alex',15 from dual union all select 5,'Alex',17 from dual union all select 6,'Ming',11 from dual ) select id, name, score from ( select row_number() over (partition by name order by score desc) as RN, id, name, score from test ) where RN = 1 中,ASP.Net MVC 5可以扩展为具有自定义属性。我已将其扩展为现在它有一个名为ApplicationUser的新属性:

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; } } Update-Database中的Package-Manager Console命令更新了数据库表,以确保Visual Studio ApplicationUser与{{}之间的一致性1}}表。我已确认名为class的新列现在存在于AspNetUsers表中。

enter image description here

现在,我想对原始DisplayName AspNetUsers中的文字使用DisplayName而不是默认UserName。但正如你所看到的那样:

_LoginPartial.cshtml

原始View正在使用<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的{​​{1}}。 User.Identity.GetUserName()UserName以及ApplicationUserUser.Identity等...但是如何让GetUserId显示?

1 个答案:

答案 0 :(得分:9)

在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:

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

在您的视图中使用它:

User.Identity.GetDisplayName()