Skip to main content
 首页 » 编程设计

c#之Identity UpdateAsync 丢失 IdentityResult.Failed

2024年02月13日13findumars

我已经为我在 ASP.NET MVC 5.1 应用程序中使用的 ASP.NET Identity 编写了自定义 UserStore。一切都按预期工作,我对 ASP.NET 的这个新功能(身份)感到非常满意......

问题是,自从我尝试从 UpdateAsync 方法返回错误以来,我认为已经快 2 天了,而且不知何故,我似乎无法返回任何内容。

LE:我正在使用 ASP.NET Identity Core 2.0.0-beta1

这是我的几行代码:

public Task UpdateAsync(IdentityUser user) 
{ 
    AdminUserEntity userEntity = new AdminUserEntity(); 
    userEntity.IsNew = false; 
    userEntity.Id = user.UserModel.Id; 
    userEntity.UserCompleteName = user.UserModel.UserCompleteName; 
    userEntity.IsDisabled = user.UserModel.IsDisabled; 
 
    if (!String.IsNullOrEmpty(user.UserModel.PasswordHash)) 
        userEntity.PasswordHash = user.UserModel.PasswordHash; 
 
    if (user.Claims != null && user.Claims.Count > 0) 
    { 
        foreach (Claim claim in user.Claims) 
        { 
            AdminUserClaimEntity claimEntity = userEntity.AdminUserClaims.AddNew(); 
            claimEntity.AdminUserUniqueId = user.UserModel.UniqueId; 
            claimEntity.ClaimType = claim.Type; 
            claimEntity.ClaimValue = claim.Value; 
        } 
    } 
 
    try 
    { 
        byte[] timestamp = Convert.FromBase64String(user.UserModel.Timestamp); 
        AdminUserEntityManagement.UpdateCompleteAdminUserEntity(userEntity, timestamp); 
    } 
    catch (Exception exception) 
    { 
        List<string> errors = new List<string>() {exception.Message}; 
        return Task.FromResult<IdentityResult>(IdentityResult.Failed(errors.ToArray())); 
    } 
 
    return Task.FromResult<object>(null); 
} 

尽管出现异常并且正在执行 catch block ,但以下行始终返回成功:

IdentityResult result = await UserManager.UpdateAsync(identityUser); 

有人可以告诉我我错过了什么吗?

请您参考如下方法:

我假设这个 UpdateAsync 在你的 ApplicationUserManager 类中,签名不应该是

public override Task<IdentityResult> UpdateAsync  

如果您尝试更改 UserManager 的 UpdateAsync 方法的工作方式。