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

伊恩

在中ASP.Net MVC 5ApplicationUser可以扩展为具有自定义属性。我对其进行了扩展,以使其现在具有一个名为的新属性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 ConsoleVisual Studio以确保ApplicationUser classAspNetUsers之间的一致性我已经确认,表中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.cshtmlUser.Identity.GetUserName()用来获取UserNameApplicationUserUser.IdentityGetUserIdNameAuthenticationType等...但我怎么得到我DisplayName的显示器?

tmg

在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()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何在 asp .net MVC 5 中将对象结果模型转换为自定义视图模型

使用ASP.NET MVC 5,如何在视图中获取Enum值的本地化DisplayAttribute字符串?

如何在VS 2015中自定义ASP.NET MVC 5支架?

如何在ASP.NET MVC 5中注册自定义主体类型?

如何在ASP.NET MVC 5中实现自定义身份验证

如何在ASP.Net MVC 5的AspNetUsers表中获取ApplicationUser的数量?

如何获取用户是否通过ASP.NET MVC视图中的自定义操作筛选器进行身份验证?

剃刀视图中的ASP.Net MVC自定义授权策略提供程序

如何在Asp.Net MVC中的视图中访问扩展属性?

使用自定义位置时如何在asp.net core mvc中指定视图位置?

如何在asp.net核心项目的MVC视图中读取Request.Form值?

如何在ASP.NET MVC中配置AutoMapper属性

如何在运行时重新加载自定义属性?ASP.NET 核心 MVC

如何在ASP.NET Core MVC中自定义Html.DropDownListFor辅助设置名称属性

如何在ASP.NET MVC中获取当前用户

asp.net MVC如何在集合中显示值

如何在ASP.NET MVC中减去2值

如何在 ASP.NET MVC 5 中使用 applozic

如何在ASP.NET 5 MVC中访问缓存?

身份如何在ASP .NET MVC 5中工作

如何在ASP.NET MVC中获取/设置自定义Azure Active Directory B2C用户属性?

如何在Asp.Net MVC中创建自定义HTML帮助器

如何在ASP.NET MVC中使用自定义表单集成条带支付?

如何在ASP.NET MVC中创建到自定义路由的ActionLink?

如何在ASP.NET Core 3.1 MVC中进行自定义路由

如何在Asp.Net-MVC中添加自定义HTTP标头

如何在ASP.NET MVC中的控制器中使用自定义方法

ASP.NET MVC-如何在EvoHtmlToPdf中使用自定义区域性

如何在 ASP.NET MVC 5 中的自定义 html-helper 内使用 Html.Partial()?