扩展 ASP.NET 身份

2023-11-26

似乎这个问题已经以多种方式被问过很多次了,但似乎都不符合我的具体情况。

这是我的 _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 属性并且它是可用的在帐户控制器中。

public class ApplicationUser : IdentityUser
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }

它不会在 _LoginPartial 文件中公开。我想让它暴露在那里!

感谢您的帮助


尽管您的答案并不“完全”是我想要的,但您的评论引导我找到了这个解决方案:

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

在我的 IdentityModel.cs 文件中,我添加了两个属性 FirstName 和 LastName

public class ApplicationUser : IdentityUser
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

扩展 ASP.NET 身份 的相关文章

随机推荐