C# 扩展ASP.NET标识

C# 扩展ASP.NET标识,c#,asp.net,asp.net-mvc,asp.net-identity,C#,Asp.net,Asp.net Mvc,Asp.net Identity,这似乎已经被问了很多次,在很多方面,其中似乎没有一个适合我的确切情况 下面是my _LoginPartial.cshtml文件中的一行: @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" }) 请参阅说明User.Identity.GetUserName()的部分

这似乎已经被问了很多次,在很多方面,其中似乎没有一个适合我的确切情况

下面是my _LoginPartial.cshtml文件中的一行:

@Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
请参阅说明User.Identity.GetUserName()的部分

我想将其更改为User.Identity.FirstName或User.Identity.GetFirstName()

我不想说“你好电子邮件地址”,而是“你好鲍勃”

我的想法是,我只是想在Identity类上显示一个新属性(或方法)。显然,这肯定不止这些

我已经添加了FirstName属性,它在AccountController中可用

public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
它不会在_LoginPartial文件中公开。我要把它暴露在那里


感谢您的帮助

我在用户登录时将名字和姓氏添加到声明中,然后编写自己的扩展方法

用户登录时,将您想要的详细信息添加到声明集(
AccountController
):

扩展方法,它只是从用户的声明集中提取细节:

public static class IdentityExtensions
{
    public static IdentityName GetGivenName(this IIdentity identity)
    {
        if (identity == null)
            return null;

        return (identity as ClaimsIdentity).FirstOrNull(ClaimTypes.GivenName);
    }

    internal static string FirstOrNull(this ClaimsIdentity identity, string claimType)
    {
        var val = identity.FindFirst(claimType);

        return val == null ? null : val.Value;
    }
}
现在,在部分中,只需调用新的扩展方法即可获得所需的详细信息:

@Html.ActionLink("Hello " + User.Identity.GetGivenName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })
编辑:更新以更紧密地匹配SignInAsync()方法的原始版本

private async Task SignInAsync(ApplicationUser user, bool isPersistent) 
{ 
    AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);  

    var identity = await user.GenerateUserIdentityAsync(UserManager);
    //add your claim here

    AuthenticationManager.SignIn(new AuthenticationProperties() 
        { 
            IsPersistent = isPersistent 
        }, identity);
}

尽管你的回答“不完全”符合我的要求,但你的评论让我找到了这个解决方案:

@using Microsoft.AspNet.Identity
@using YourModelnameHere.Models
@if (Request.IsAuthenticated)
{
using (Html.BeginForm("LogOff", "Account", FormMethod.Post, new { id = "logoutForm", @class = "navbar-right" }))
{
    @Html.AntiForgeryToken()

    <ul class="nav navbar-nav navbar-right">
        <li>
            @{
                var manager = new UserManager<ApplicationUser>(new Microsoft.AspNet.Identity.EntityFramework.UserStore<ApplicationUser>(new ApplicationDbContext()));
                var currentUser = manager.FindById(User.Identity.GetUserId()); 
            }
            @Html.ActionLink("Hello " + currentUser.FirstName + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" })

            @Html.ActionLink("Hello " + User.Identity.GetUserName() + "!", "Manage", "Account", routeValues: null, htmlAttributes: new { title = "Manage" }) // I no longer need this ActionLink!!!
        </li>
    </ul>
    }
}

回答更新为Asp.net核心! 希望这能为其他用户节省一些时间

@if (SignInManager.IsSignedIn(User)){

<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
    <ul class="nav navbar-nav navbar-right">
        <li>
            @{

                var currentUser = await UserManager.FindByEmailAsync(User.Identity.Name);
            }

            <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @currentUser.FirstName </a>
        </li>
        <li>
            <button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button>
        </li>
    </ul>
</form>
@if(SignInManager.IsSignedIn(用户)){
  • @{ var currentUser=await UserManager.findbyemailsync(User.Identity.Name); } 你好@currentUser.FirstName
  • 注销

}

感谢您的快速回复。假设我想要检索一个已经存在的值,比如EmailConfirm?有那么多不同吗?没有。您只需在
SignInAsync
方法中添加其他声明(从存储数据的任何位置提取数据),然后创建相应的扩展方法来检索它们。私有异步任务SignInAsync(ApplicationUser user,bool isPersistent){AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);AuthenticationManager.SignIn(新建AuthenticationProperties(){IsPersistent=IsPersistent},等待用户.GenerateUserIdentityAsync(UserManager));谢谢你的回复。这对我来说不起作用-这并不奇怪-根本不责怪,但我会继续寻找。晚上好。你的
符号sync
在做什么?这就是你在上面发布的内容吗?如果是这样,你不会对身份进行任何声明。你可以随时拨打db电话,其中电子邮件是User.identity.GetUserName();您可以在底部添加@using Microsoft.AspNet.Identity.EntityFramework以获得较短的代码。回答得好!哦,感谢您为我做的工作。Differentily不想向路过的人显示电子邮件地址。这是登录的一半。这很有用。这里添加了一个参考:我在网上搜索时很困惑。我刚刚忘记了使用MyProj.Models添加一个
@以访问
ApplicationUser
。我已经添加了自定义属性!!感谢您的解决方案!
public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
@if (SignInManager.IsSignedIn(User)){

<form asp-area="" asp-controller="Account" asp-action="LogOff" method="post" id="logoutForm" class="navbar-right">
    <ul class="nav navbar-nav navbar-right">
        <li>
            @{

                var currentUser = await UserManager.FindByEmailAsync(User.Identity.Name);
            }

            <a asp-area="" asp-controller="Manage" asp-action="Index" title="Manage">Hello @currentUser.FirstName </a>
        </li>
        <li>
            <button type="submit" class="btn btn-link navbar-btn navbar-link">Log off</button>
        </li>
    </ul>
</form>