Razor 视图中的 MVC6 CORE 身份声明
MVC6 CORE identity claims in razor view
我正在摆弄身份,我确实在挣扎 - 我搜索了又搜索,但没有任何显示。
我想添加身份声明,然后能够 post 在我页面的视图中添加这些声明。
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);
}
请注意,我检查了我的数据库并存储了名字和姓氏!
然后我扩展了我的身份 (注意我删除了原来的 return,以检查它是否确实 returned 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() ;
}
}
我现在认为我应该能够从我的 razor 视图访问数据,它位于 _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");
}
}
我显然遗漏了一些东西。我可以毫无问题地访问常规用户名。我已经搜索并找到了很多甚至可以到达这里,但是很多都是不同的版本,所以我卡住了..
如果你能帮助我,我将不胜感激!
谢谢。
我正在摆弄身份,我确实在挣扎 - 我搜索了又搜索,但没有任何显示。
我想添加身份声明,然后能够 post 在我页面的视图中添加这些声明。
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);
}
请注意,我检查了我的数据库并存储了名字和姓氏!
然后我扩展了我的身份 (注意我删除了原来的 return,以检查它是否确实 returned 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() ;
}
}
我现在认为我应该能够从我的 razor 视图访问数据,它位于 _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");
}
}
我显然遗漏了一些东西。我可以毫无问题地访问常规用户名。我已经搜索并找到了很多甚至可以到达这里,但是很多都是不同的版本,所以我卡住了..
如果你能帮助我,我将不胜感激!
谢谢。