I am fiddling around with identity and I am definitely struggling - I have searched and searched, but nothing shows.
I want to add claims to identity and then be able to post these in the view on my page.
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; }
In my register user i save these with the user:
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);
}
Note, that i checked my database and FirstName and LastName are being stored!
I then extended my identity (Note i removed the original return, to check if it indeed 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() ;
}
}
I now figured I should be able to access the data from my razor view, which is in _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>
I also attempted to do this without the extension, directly:
@{
if (Context.User.Identity.IsAuthenticated)
{
var FirstName = ((ClaimsIdentity)User.Identity).FindFirst("FirstName");
}
}
I am obviously missing something.. I can access the regular username without a problem. I have searched and found a lot to even get here, but a lot of it is different versions, so im stuck..
I'd appreciate if you can help me out!
Thanks.
请遵循此处: 定制索赔转换
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.