简体   繁体   English

asp.net身份SetEmailConfirmedAsync

[英]asp.net identity SetEmailConfirmedAsync

I'm having issues with the SetEmailConfirmedAsync method within my UserStore class. 我的UserStore类中的SetEmailConfirmedAsync方法出现问题。

Everything is working fine, users are being created with hashed password, tokens are being generated for the confirmation email, the email is sending successfully. 一切正常,使用哈希密码创建用户,为确认电子邮件生成令牌,电子邮件发送成功。 The problem comes when I try to verify the email 当我尝试验证电子邮件时出现问题

public async Task<ActionResult> ConfirmEmail(Guid userId, string token)
{
    Task<Microsoft.AspNet.Identity.IdentityResult> result = UserManager.ConfirmEmailAsync(userId, token);

    if (result.Result.Succeeded)
    {

    }
    else
    {

    }

    return View();
}

This then calls 然后调用

public Task SetEmailConfirmedAsync(User user, bool confirmed) 
        {
            AccountService.VerifiyAccount(user.Id, confirmed);
            return Task.FromResult(0);
        }

which sets the account to verified as i'd expect. 这将帐户设置为按照我的期望进行验证。 However, the next thing that happens is that the FindByNameAsync is called followed by the UpdateAsync method which overwirtes my changes applied within SetEmailConfirmedAsync. 但是,接下来发生的事情是先调用FindByNameAsync,然后再调用UpdateAsync方法,该方法将覆盖我在SetEmailConfirmedAsync中应用的更改。

Does anyone know why asp.net Identity makes a call to UpdateAsync after SetEmailConfirmedAsync? 有谁知道为什么SetEmailConfirmedAsync之后asp.net Identity会调用UpdateAsync? I've searched around for some clues but cant find anything. 我已经搜索了一些线索,但是找不到任何东西。

Update 更新

I've done some digging and pulled this from the Identity source code 我做了一些挖掘,并将其从Identity源代码中提取出来

public virtual async Task<IdentityResult> ConfirmEmailAsync(TKey userId, string token)
        {
            ThrowIfDisposed();
            var store = GetEmailStore();
            var user = await FindByIdAsync(userId).WithCurrentCulture();
            if (user == null)
            {
                throw new InvalidOperationException(String.Format(CultureInfo.CurrentCulture, Resources.UserIdNotFound,
                    userId));
            }
            if (!await VerifyUserTokenAsync(userId, "Confirmation", token).WithCurrentCulture())
            {
                return IdentityResult.Failed(Resources.InvalidToken);
            }
            await store.SetEmailConfirmedAsync(user, true).WithCurrentCulture();
            return await UpdateAsync(user).WithCurrentCulture();
        }

I'm struggling to figure out why a call to update user would be made after SetEmailConfirmedAsync has been called 我正在努力弄清楚为什么在调用SetEmailConfirmedAsync之后将进行更新用户的调用

Turns out I was being dumb, must remember that c# objects are reference types! 原来我很傻,必须记住c#对象是引用类型!

instead of this... 而不是这个...

public Task SetEmailConfirmedAsync(User user, bool confirmed) 
{
    AccountService.VerifiyAccount(user.Id, confirmed);
    return Task.FromResult(0);
}

I just needed to do this... 我只需要这样做...

public Task SetEmailConfirmedAsync(User user, bool confirmed)
{            
    user.Verified = confirmed;
    return Task.FromResult(0);
}

Set my custom confirmed field and then identity calls the updaate method which updates my user entity 设置我的自定义确认字段,然后身份调用更新我的用户实体的updaate方法

public Task UpdateAsync(User user)
{
    AccountService.UpdateUser(user);
    return Task.FromResult(0);
}

Dunno what version of identity you are using, but the current version calls this method: Dunno您正在使用什么版本的身份,但是当前版本使用此方法:

public virtual Task SetEmailConfirmedAsync(TUser user, bool confirmed)
    {
        ThrowIfDisposed();
        if (user == null)
        {
            throw new ArgumentNullException("user");
        }
        user.EmailConfirmed = confirmed;
        return Task.FromResult(0);
    }

as you can see, it only sets the user with the e-mail confirmed, but then needs to save the changes into the database, thus the 如您所见,它只设置了确认电子邮件的用户,然后需要将更改保存到数据库中,因此

return await UpdateAsync(user).WithCurrentCulture();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM