[英]Extending ASP.NET Identity
從許多方面來看,這似乎已被多次詢問,但這些似乎都不適合我的確切情況。
這是我的_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 Bob”
我的想法是,我只是想在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; }
}
我通過在用戶登錄時向聲明添加名字和姓氏,然后編寫我自己的擴展方法來完成此操作。
當用戶登錄時,將所需的詳細信息添加到聲明集( 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);
}
為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>
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.