ced*_*und 6 claims-based-identity claims asp.net-core-identity asp.net-core-2.0
正如它在标题中所说的那样我已经为注册用户分配了声明,我现在正试图在用户登录sql server中的UserClaims表中的应用程序时检索声明值,我觉得这有点难以做到,因为这是我第一次使用索赔.
寻找我们的方向来实现这一目标,提前谢谢你.
public async Task<IActionResult> Register(RegisterViewModel model, string returnUrl = null)
{
ViewData["ReturnUrl"] = returnUrl;
if (ModelState.IsValid)
{
var user = new ApplicationUser { UserName = model.UserName, Email = model.Email, UserRoleId = model.RoleId };
var result = await _userManager.CreateAsync(user, model.Password);
if (result.Succeeded)
{
_logger.LogInformation("User created a new account with password.");
var code = await _userManager.GenerateEmailConfirmationTokenAsync(user);
var callbackUrl = Url.EmailConfirmationLink(user.Id, code, Request.Scheme);
await _emailSender.SendEmailConfirmationAsync(model.Email, callbackUrl);
await _signInManager.SignInAsync(user, isPersistent: false);
_logger.LogInformation("User created a new account with password.");
await addUserClaims(model.CusomterId, model.UserName);
return RedirectToLocal(returnUrl);
}
AddErrors(result);
}
List<UserRole> roles = _userRoleRepo.GetAll();
model.CreateRoleList(roles);
List<Customer> customers = await _customerRepository.GetAll();
model.SetupCustomerOptionList(customers);
// If we got this far, something failed, redisplay form
return View(model);
}
private async Task addUserClaims(string CustomerID ,string username)
{
// Customer customer = _customerRepository.GetById(customerid);
List<Customer> customers = await _customerRepository.GetAll();
Customer customer = _customerRepository.GetById(CustomerID);
var user = await _userManager.FindByNameAsync(username);
;
await _userManager.AddClaimAsync(user, new Claim(ClaimTypes.Name, CustomerID));
}
Run Code Online (Sandbox Code Playgroud)
Irf*_*raf 18
组
var claims = new List<Claim>
{
new Claim("Currency", "PKR")
};
Run Code Online (Sandbox Code Playgroud)
得到
@User.Claims.FirstOrDefault(c => c.Type == "Currency").Value
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10198 次 |
| 最近记录: |