【问题标题】:How to Get Custom Property Value of the ApplicationUser in the ASP.Net MVC 5 View?如何在 ASP.Net MVC 5 视图中获取 ApplicationUser 的自定义属性值?
【发布时间】:2016-12-15 06:26:09
【问题描述】:

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

我还在Visual Studio 中的Package-Manager Console 中使用Update-Database 命令更新了数据库表,以确保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.cshtml是用User.Identity.GetUserName()得到ApplicationUserUserNameUser.IdentityGetUserIdNameAuthenticationType 等...但是我如何让我的 DisplayName 显示?

【问题讨论】:

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


    【解决方案1】:

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

    【讨论】:

    • 这看起来很有希望...您通常将IdentiyExtensions 方法放在哪里?在IdentityModels.cs?或者您会为它创建一个具有特定名称约定的新文件?
    • 我会创建一个名为“IdentityExtensions.cs”的新文件,可能在名为“Helpers”的文件夹中
    • 感谢您的回答!有用!不过我做了一点修改......我没有返回return claim.FindFirst("DisplayName"),而是返回return claim.FindFirst("DisplayName").ToString().Substring(("DisplayName: ").Length);——这是因为claim.FindFirst("DisplayName")的类型是Claim,但return必须是string。另外,如果我不输入Substring,它将显示DisplayName: myname 而不仅仅是myname
    • @Ian 你对返回字符串是正确的。我更新了扩展方法。
    • 我没有找到FindFirstValue 方法(没有)。我错过任何参考吗?感谢您的更新-无论如何我都接受了您的回答,因为它已经充分指出了正确的答案。 ;)
    猜你喜欢
    • 2015-11-02
    • 2018-04-13
    • 2011-02-27
    • 2014-01-22
    • 2015-12-18
    • 1970-01-01
    • 2013-11-18
    • 1970-01-01
    相关资源
    最近更新 更多