延长ASP.净的身份
本文关键字:身份 ASP 延长 | 更新日期: 2023-09-27 18:18:55
似乎这个问题已经被问过很多次了,在很多方面,似乎没有一个适合我的确切情况。
这是我的_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文件中暴露。我要它暴露在那里!
谢谢你的帮助
尽管你的回答不是"完全"我想要的,但你的评论使我想到了这个解决方案:
@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; }
}
我在用户登录时将First和Last Name添加到声明中,然后编写我自己的扩展方法。
当用户登录时,将您想要的详细信息添加到声明集(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;
}
}
现在在部分中,只需调用新的扩展方法来获取所需的详细信息:
@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);
}
回答更新为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>
}