在很多方面,这似乎已被多次询问,但这似乎都不适合我的确切情况。
这是我的_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()。
我不希望它说“Hello email address”,而是“Hello Bob”
我的想法是,我只是想在Identity类上显示一个新属性(或方法)。显然它必须不止于此。
我在AccountController中添加了FirstName属性并且可用。
public class ApplicationUser : IdentityUser
{
public string FirstName { get; set; }
public string LastName { get; set; }
它不会在_LoginPartial文件中公开。我希望它暴露在那里!
感谢您的帮助
答案 0 :(得分:10)
即使你的答案不是“我想要的”,你的评论也让我得到了这个解决方案:
@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; }
}
答案 1 :(得分:7)
我通过在用户登录时向声明添加名字和姓氏,然后编写我自己的扩展程序来完成此操作。
当用户登录时,将您想要的详细信息添加到声明集(AccountController
)中:
private async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
// Add the users primary identity details to the set of claims.
var pocoForName = GetNameFromSomeWhere();
identity.AddClaim(new Claim(ClaimTypes.GivenName, pocoForName));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
扩展方法,它只是从用户声明集中提取详细信息:
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;
}
}
现在在partial中,只需调用新的扩展方法即可获得所需的详细信息:
@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);
}
答案 2 :(得分:0)
为Asp .net Core更新了答案! 希望这可以为其他用户节省一些时间。
@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>
}