添加到ClaimsIdentity中的声明在ASP.NET Core Identity System中丢失

时间:2018-10-04 18:50:26

标签: asp.net asp.net-core-mvc asp.net-identity

在我的 ASP.NET Core身份中,我注意到我添加的声明正在迷路

首先,我具有创建索赔视图,该视图具有用于创建索赔的表单。 声明已添加到当前登录的用户

Action方法的代码为:

[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");
}

该行添加了该行-identity.AddClaim(claim);

现在,最后一行将重定向到其代码为:

的Index操作方法。
public ViewResult Index() => View(User?.Claims);

显示用户所有声明的索引视图是:

@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>

示例:我添加了一个声明,请参见下图 enter image description here

但是,索引视图未提取该声明,请参见下图: enter image description here

怎么了?

2 个答案:

答案 0 :(得分:0)

添加到经过身份验证的用户的声明不会在其他请求中持续存在。必须先添加声明,然后才能登录。

您可以查看here,了解如何向用户添加声明。

here为例,说明如何在登录后更改用户声明。

答案 1 :(得分:0)

如果要将声明保存到User?.Claims,则需要使用更新后的_signInManager.Context.SignInAsync来致电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>>();
    
  • 更新用户声明

    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");
    }
     }