私は身元を覗いています。私は確かに苦労しています。私は検索して検索しましたが、何も表示されません。MVC6 COREのクレームビューでの身分請求
私はクレームをアイデンティティに追加し、自分のページのビューにこれを掲載することができます。私の登録ユーザーで
public async Task<ClaimsIdentity> GenerateUserIdentityAsync(UserManager<ApplicationUser> manager)
{
var authenticationType = "Basic";
var userIdentity = new ClaimsIdentity(await manager.GetClaimsAsync(this), authenticationType);
// Add custom user claims here
userIdentity.AddClaim(new Claim("FirstName", this.FirstName));
return userIdentity;
}
public string FirstName { get; set; }
public string LastName { get; set; }
public string Address { get; set; }
私は、ユーザーにこれらを保存:私は私のデータベースをチェックして、姓と名が格納されていることを
public async Task<IActionResult> Register(RegisterViewModel model)
{
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.Email, Email = model.Email, FirstName = model.FirstName, LastName = model.LastName};
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
// For more information on how to enable account confirmation and password reset please visit http://go.microsoft.com/fwlink/?LinkID=532713
// Send an email with this link
//var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
//var callbackUrl = Url.Action("ConfirmEmail", "Account", new { userId = user.Id, code = code }, protocol: HttpContext.Request.Scheme);
//await _emailSender.SendEmailAsync(model.Email, "Confirm your account",
// "Please confirm your account by clicking this link: <a href=\"" + callbackUrl + "\">link</a>");
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation(3, "User created a new account with password.");
return RedirectToAction(nameof(HomeController.Index), "Home");
}
AddErrors(result);
}
// If we got this far, something failed, redisplay form
return View(model);
}
注意!
私はその後、私のアイデンティティを拡張(それは実際にはnullを返したかどうかを確認するために、私は、元のリターンを削除します)
public static class IdentityExtension
{
public static string GetFirstName(this IIdentity identity)
{
var claim = ((ClaimsIdentity)identity).FindFirst("FirstName");
// Test for null to avoid issues during local testing
// return (claim != null) ? claim.Value : string.Empty;
return claim.ToString() ;
}
}
私は今、私は私のかみそりビューからデータにアクセスすることができるはずです考え出しこれは直接、私も拡張せずにこれを実行しようとしに_Navigation.cshtml
@if (Context.User.Identity.IsAuthenticated)
{
<div class="dropdown profile-element">
<a data-toggle="dropdown" class="dropdown-toggle" href="#">
<span class="clear">
<span class="block m-t-xs">
<strong class="font-bold">Hello @User.Identity.GetFirstName()</strong>
</span>
です:
@{
if (Context.User.Identity.IsAuthenticated)
{
var FirstName = ((ClaimsIdentity)User.Identity).FindFirst("FirstName");
}
}
私は明らかに何かが欠けています。問題なく通常のユーザー名にアクセスできます。私は検索して、ここにも多くを見つけましたが、それの多くは異なるバージョンですので、私はつまらないです..
あなたが私を助けることができれば嬉しいです!
ありがとうございました。
あなたは、あなたはそれが働いてしまった喜んで歓迎します。 – zulq
[私はリンクが素晴らしいと思っていますが、決して答えの中で唯一の情報であってはいけません。](http://meta.stackexchange.com/a/8259/171858)。 –