IUserStore<&TUSER GT; .CreateAsync:如何表明自定义实现失败? [英] IUserStore<TUser>.CreateAsync : how to indicate failure in custom implementation?

查看:413
本文介绍了IUserStore<&TUSER GT; .CreateAsync:如何表明自定义实现失败?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写IUserStore自定义实现。 create方法的签名是:

I am writing a custom implementation for IUserStore. The signature of the create method is:

public async virtual Task CreateAsync(TUser user)

和有意义考虑Microsoft.AspNet.Identity核心接口IUserStore是(其是相同的)。

And that makes sense considering the core interface IUserStore in Microsoft.AspNet.Identity is (which is the same).

然而,在Microsoft.AspNet.Identity定义的UserManager类的接口是:

However the interface of the UserManager class defined in Microsoft.AspNet.Identity is :

public virtual Task<IdentityResult> CreateAsync(TUser user);

我的问题是我不知道我应该怎么这个IdentityResult传递到的UserManager因为在店里的返回类型简直是任务。我有一个自定义的逻辑来确定用户是否可以或不可以被创建的,所以我真的需要CreateAsync的结果告诉给的UserManager。

My problem is I don't see how I should pass this IdentityResult to the UserManager since the return type in the store is simply "Task". I have a custom logic to determine if a user can or cannot be created, so I really need to tell the outcome of CreateAsync to the UserManager.

任何想法?

推荐答案

纵观<一个href=\"https://www.symbolsource.org/MyGet/Metadata/aspnetwebstacknightly/Project/Microsoft.AspNet.Identity.Core/2.0.0-rtm-140226/Release/Default/Microsoft.AspNet.Identity.Core/Microsoft.AspNet.Identity.Core/UserManager.cs?ImageName=Microsoft.AspNet.Identity.Core\"相对=nofollow>来源$ C ​​$ C为 UserManager.CreateAsync (这是身份2.0),你可以看到在调用之前 IUserStore.CreateAsync ,它对 IIdentityValidator℃的调用; TUSER&GT; .ValidateAsync ,它负责实际返回相关的 IdentityResult 对象:

Looking at the source code for UserManager.CreateAsync (this is for Identity 2.0) you can see that prior to calling IUserStore.CreateAsync, it makes a call to IIdentityValidator<TUser>.ValidateAsync, which is responsible to actually return the relevant IdentityResult object:

public virtual async Task<IdentityResult> CreateAsync(TUser user)
{
        ThrowIfDisposed();
        await UpdateSecurityStampInternal(user).ConfigureAwait(false);
        var result = await UserValidator.ValidateAsync(user).ConfigureAwait(false);
        if (!result.Succeeded)
        {
            return result;
        }
        if (UserLockoutEnabledByDefault && SupportsUserLockout)
        {
            await GetUserLockoutStore().SetLockoutEnabledAsync(user, true).ConfigureAwait(false);
        }
        await Store.CreateAsync(user).ConfigureAwait(false);
        return IdentityResult.Success;
}

IUserStore.CreateAsync 的主要目的是使呼叫从而节省数据底层数据源。看来您真的想实施 IIdentityValidator&LT; TUSER方式&gt; ,并把它放在你的的UserManager 实例

The main purpose of IUserStore.CreateAsync is to make the call to the underlying data source which saves the data. It seems that you make actually want to implement IIdentityValidator<TUser> and set it on your UserManager instance.

这篇关于IUserStore&LT;&TUSER GT; .CreateAsync:如何表明自定义实现失败?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆