问题描述
在我的 ASP.NET Core身份中,我注意到我添加的声明正在迷路.
In my ASP.NET Core Identity I have noticed that my claim that I add are getting lost.
首先,我有一个创建索赔视图,该视图具有一个用于创建索赔的表单. 声明已添加到当前登录的用户.
First I have a Create claim view having a form to create a claim. The claim is added to the currently logged in user.
Action方法的代码为:
The code of the Action method is:
[HttpPost]
[ActionName("Create")]
public IActionResult Create_Post(string claimType, string claimValue, string claimIssuer)
{
ClaimsIdentity identity = User.Identity as ClaimsIdentity;
Claim claim = new Claim(claimType, claimValue, ClaimValueTypes.String, claimIssuer);
identity.AddClaim(claim);
return RedirectToAction("Index");
}
在此行上添加了clain-identity.AddClaim(claim);
.
The clain is added on this line - identity.AddClaim(claim);
.
现在,最后一行将重定向到其代码为:
Now the last line is redirecting to Index action method whose code is:
public ViewResult Index() => View(User?.Claims);
为用户显示所有索偿的索引视图是:
The Index View which is showing all the claim for the user is:
@model IEnumerable<System.Security.Claims.Claim>
<table class="table table-sm table-bordered">
<tr>
<th>Subject</th>
<th>Issuer</th>
<th>Type</th>
<th>Value</th>
</tr>
@foreach (var claim in Model.OrderBy(x => x.Type))
{
<tr>
<td>@claim.Subject.Name</td>
<td>@claim.Issuer</td>
<td>@claim.Type</td>
<td>@claim.Value</td>
</tr>
}
</table>
示例:我添加了一个声明,请参见下图
Example: I added a claim, see the below image
但是,索引视图未提取版权声明,请参见下图:
But the Index View did not fetched the claim, see the below image:
怎么了?
推荐答案
如果要将声明保存到User?.Claims
,则需要使用已更新的ClaimsIdentity
调用_signInManager.Context.SignInAsync
.
If you want to save the claims to User?.Claims
, you need to call _signInManager.Context.SignInAsync
with updated ClaimsIdentity
.
请按照以下步骤操作:
-
使用新的
ClaimsIdentity
public class CustomClaimsCookieSignInHelper<TIdentityUser> where TIdentityUser : IdentityUser
{
private readonly SignInManager<TIdentityUser> _signInManager;
public CustomClaimsCookieSignInHelper(SignInManager<TIdentityUser> signInManager)
{
_signInManager = signInManager;
}
public async Task SignInUserAsync(ClaimsIdentity claimsIdentity)
{
await _signInManager.Context.SignInAsync(IdentityConstants.ApplicationScheme, new ClaimsPrincipal(claimsIdentity));
}
}
注册CustomClaimsCookieSignInHelper<TIdentityUser>
services.AddTransient<CustomClaimsCookieSignInHelper<IdentityUser>>();
更新用户声明
Update User Claims
public class IdentityController : Controller
{
private readonly CustomClaimsCookieSignInHelper<IdentityUser> _signInHelper;
private readonly UserManager<IdentityUser> _userManager;
public IdentityController(CustomClaimsCookieSignInHelper<IdentityUser> signInHelper
, UserManager<IdentityUser> userManager)
{
_signInHelper = signInHelper;
_userManager = userManager;
}
public ViewResult Index() => View(User?.Claims);
[HttpGet]
[ActionName("Create")]
public IActionResult Create_Post()
{
return View();
}
[HttpPost]
[ActionName("Create")]
public async Task<IActionResult> Create_Post(string claimType, string claimValue, string claimIssuer)
{
ClaimsIdentity identity = User.Identity as ClaimsIdentity;
Claim claim = new Claim(claimType, claimValue, ClaimValueTypes.String, claimIssuer);
identity.AddClaim(claim);
await _signInHelper.SignInUserAsync(identity);
return RedirectToAction("Index");
}
}
这篇关于添加到ClaimsIdentity中的声明在ASP.NET Core Identity System中丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!