User.Identity.Name返回的是UserName而不是Name [英] User.Identity.Name is returning UserName instead of Name

查看:203
本文介绍了User.Identity.Name返回的是UserName而不是Name的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在_LoginPartial中的导航栏中显示用户名而不是用户名.目前,我正在使用User.Identity.GetUserName()作为用户名,但是现在我想显示当前用户名.

_LoginPartial由Startup.Auth.cs调用,因此我无法在后端运行查询并获取用户名,因此我只能使用在视图中可以由razor运行的内置函数.

我尝试了所有方法,但是它们都给了我用户名,而不是用户名.

<small>@System.Threading.Thread.CurrentPrincipal.Identity.Name</small>
<small>@User.Identity.Name</small>
<small>@threadPrincipal.Identity.Name</small>
<small>@System.Web.HttpContext.Current.User.Identity.Name</small>

如何获取名称而不是userName

这是用户表

ID
Email
EmailConfirm
Password
Security
PhoneNumber
PhoneNumberConfirm
TwoFactorEnable
LockoutEndDateUtc
LockoutEnable
AccessFailedCount
UserName
Name
Status
Image

解决方案

由于名称是ApplicationUser(扩展的IdentityUser)的自定义字段,因此您需要将该字段添加为声明.

如果使用模板设置Identity,则将在 IdentityModels.cs 中找到ApplicationUser类.在这里,我添加了一个字段名称",并将其添加为声明:

public class ApplicationUser : IdentityUser
{
    public string Name { get; set; }

    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("CustomName", Name));

        return userIdentity;
    }
}

此代码将添加一个具有名称值的声明"CustomName".

在您看来,您现在可以阅读声明.这是_LoginPartial中的示例:

<ul class="nav navbar-nav navbar-right">
    <li>
        @{ var user = (System.Security.Claims.ClaimsIdentity)User.Identity; }
        @Html.ActionLink("Hello " + user.FindFirstValue("CustomName") + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
    </li>

您可以添加其他自定义字段以及声明.

I want to display Name of a user instead of UserName in navbar that is in _LoginPartial Currently I'm using User.Identity.GetUserName() for userName but now I want to display Name of the current user.

_LoginPartial is called by the Startup.Auth.cs so I can't run the query on the backend and get the Name of my user so I can only use built-in functions which can run by razor in view.

I have tried all of them but they all are giving me username instead of name of the user.

<small>@System.Threading.Thread.CurrentPrincipal.Identity.Name</small>
<small>@User.Identity.Name</small>
<small>@threadPrincipal.Identity.Name</small>
<small>@System.Web.HttpContext.Current.User.Identity.Name</small>

How can I get the name instead of userName

This is the User Table

ID
Email
EmailConfirm
Password
Security
PhoneNumber
PhoneNumberConfirm
TwoFactorEnable
LockoutEndDateUtc
LockoutEnable
AccessFailedCount
UserName
Name
Status
Image

解决方案

Since Name is a custom field of ApplicationUser (the extended IdentityUser) you'll need to add the field as claim.

If you've used the template to setup Identity then you'll find in IdentityModels.cs the class ApplicationUser. Here I've added a field 'Name' and added this as claim:

public class ApplicationUser : IdentityUser
{
    public string Name { get; set; }

    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("CustomName", Name));

        return userIdentity;
    }
}

This code will add a claim 'CustomName' which has the value of the name.

In your view you can now read the claims. Here's an example in _LoginPartial:

<ul class="nav navbar-nav navbar-right">
    <li>
        @{ var user = (System.Security.Claims.ClaimsIdentity)User.Identity; }
        @Html.ActionLink("Hello " + user.FindFirstValue("CustomName") + "!", "Index", "Manage", routeValues: null, htmlAttributes: new { title = "Manage" })
    </li>

You can add other custom fields as well as claims.

这篇关于User.Identity.Name返回的是UserName而不是Name的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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