无法删除 asp 身份中的用户声明
can't remove user claim in asp Identity
我尝试更新我服务中的用户声明,首先我应该删除这样的声明:
var userClaimCollection = this.AppUserManager.GetClaims(user.Id);
var toRemove = new List<Claim>();
foreach (var claim in userClaimCollection)
{
if (claim.Type == group_role)
{
toRemove.Add(claim);
}
}
foreach(var t in toRemove)
{
Claim c = new Claim(group_role, t.Value);
this.AppUserManager.RemoveClaim(user.Id, c);
}
但是在 RemoveClaim(user.Id,c)
我得到 Collection was modified; enumeration operation may not execute
错误。
我到底做错了什么?
最后我发现我的问题是 Identity 2.2.0,更新到 Identity 2.2.1 后我的问题解决了。
我找到了答案 here
我有解决办法。简单地使用 2.2.1 对我不起作用。我为此绞尽脑汁,因为看起来删除声明应该很容易。但是内置管理器 RemoveClaim 实用程序会失败。我遇到过两次添加声明的情况。而且我终其一生都无法摆脱重复的索赔条目。
直到我迭代 indentity.claims 并使用 TryRemoveClaim()。
此处示例:
public async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
foreach(Claim c in identity.Claims)
{
if (c.Type == "FullName" || c.Type == "AcctStatus")
{
identity.TryRemoveClaim(c);
}
}
identity.AddClaim(new Claim("FullName", user.BF_FullName));
identity.AddClaim(new Claim("AcctStatus", user.BF_AcctStatus));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
要删除声明,请按照以下步骤操作,
Step 1: fetch the claims based on the User Id
var claims = await manager.GetClaimsAsync(user.Id);
Step 2: Filter the needed claim from fetched claims.
var lastAccessedClaim = claims.FirstOrDefault(t => t.Type == claimType);
Step 3: Then, last use the removeclaim method to delete the claim for
the user as shown below.
var resDelete = (lastAccessedClaim == null)? null: await manager.RemoveClaimAsync(user.Id, lastAccessedClaim);
输入以下代码以获得准确的结果。
var claims = await manager.GetClaimsAsync(user.Id);
var lastAccessedClaim = claims.FirstOrDefault(t => t.Type == claimType);
var resDelete = (lastAccessedClaim == null)? null: await manager.RemoveClaimAsync(user.Id, lastAccessedClaim);
我尝试更新我服务中的用户声明,首先我应该删除这样的声明:
var userClaimCollection = this.AppUserManager.GetClaims(user.Id);
var toRemove = new List<Claim>();
foreach (var claim in userClaimCollection)
{
if (claim.Type == group_role)
{
toRemove.Add(claim);
}
}
foreach(var t in toRemove)
{
Claim c = new Claim(group_role, t.Value);
this.AppUserManager.RemoveClaim(user.Id, c);
}
但是在 RemoveClaim(user.Id,c)
我得到 Collection was modified; enumeration operation may not execute
错误。
我到底做错了什么?
最后我发现我的问题是 Identity 2.2.0,更新到 Identity 2.2.1 后我的问题解决了。
我找到了答案 here
我有解决办法。简单地使用 2.2.1 对我不起作用。我为此绞尽脑汁,因为看起来删除声明应该很容易。但是内置管理器 RemoveClaim 实用程序会失败。我遇到过两次添加声明的情况。而且我终其一生都无法摆脱重复的索赔条目。
直到我迭代 indentity.claims 并使用 TryRemoveClaim()。
此处示例:
public async Task SignInAsync(ApplicationUser user, bool isPersistent)
{
AuthenticationManager.SignOut(DefaultAuthenticationTypes.ExternalCookie);
var identity = await UserManager.CreateIdentityAsync(user, DefaultAuthenticationTypes.ApplicationCookie);
foreach(Claim c in identity.Claims)
{
if (c.Type == "FullName" || c.Type == "AcctStatus")
{
identity.TryRemoveClaim(c);
}
}
identity.AddClaim(new Claim("FullName", user.BF_FullName));
identity.AddClaim(new Claim("AcctStatus", user.BF_AcctStatus));
AuthenticationManager.SignIn(new AuthenticationProperties() { IsPersistent = isPersistent }, identity);
}
要删除声明,请按照以下步骤操作,
Step 1: fetch the claims based on the User Id
var claims = await manager.GetClaimsAsync(user.Id);
Step 2: Filter the needed claim from fetched claims.
var lastAccessedClaim = claims.FirstOrDefault(t => t.Type == claimType);
Step 3: Then, last use the removeclaim method to delete the claim for the user as shown below.
var resDelete = (lastAccessedClaim == null)? null: await manager.RemoveClaimAsync(user.Id, lastAccessedClaim);
输入以下代码以获得准确的结果。
var claims = await manager.GetClaimsAsync(user.Id);
var lastAccessedClaim = claims.FirstOrDefault(t => t.Type == claimType);
var resDelete = (lastAccessedClaim == null)? null: await manager.RemoveClaimAsync(user.Id, lastAccessedClaim);