在实现自己的IUserStore时,类上的“可选”接口实际上是可选的吗?

时间:2021-02-28 22:30:21

I'm working with Microsoft's Asp.Net Identity framework version 2, and am implementing my own IUserStore. My new class MyUserStore implements the IUserStore<MyUserClass,int> interface and the IUserPasswordStore<MyUserClass,int>, which is what is required to use it with the UserManager<MyUserClass,int> class. Or at least that's what I gathered from reading tutorials like this:

我正在使用Microsoft的Asp.Net Identity框架版本2,并且正在实现我自己的IUserStore。我的新类MyUserStore实现了IUserStore 接口和IUserPasswordStore ,这是将它与UserManager 类一起使用所需的。或者至少这是我从阅读教程中收集的内容,如下所示: ,int> ,int> ,int>

"The one required interface in the identity system is IUserStore" - Scott Allen

“身份系统中所需的一个接口是IUserStore” - Scott Allen

But this doesn't seem to be the case when I run the code.

但是,当我运行代码时,情况似乎并非如此。

I initialize my manager:

我初始化我的经理:

var uMan= new UserManager<MyUserClass, int>(new MyUserStore()); 
var sMan = new SignInManager<MyUserClass, int>(uMan,authCtxFromOwin);

And when sMan.PasswordSignIn(...) on the SignInManager is executed, no matter what, the SignInManager always runs functionality in the UserManager that depends on the optional interfaces. Here's the source for the PasswordSignInAsync method from the SignInManager class:

当执行SignInManager上的sMan.PasswordSignIn(...)时,无论如何,SignInManager始终在UserManager中运行依赖于可选接口的功能。以下是SignInManager类中PasswordSignInAsync方法的来源:

public virtual async Task<SignInStatus> PasswordSignInAsync(string userName, string password, bool isPersistent, bool shouldLockout)
        {
           ...
            if (await UserManager.IsLockedOutAsync(user.Id).WithCurrentCulture())
            {
                return SignInStatus.LockedOut;
            }
            if (await UserManager.CheckPasswordAsync(user, password).WithCurrentCulture())
            {
                return await SignInOrTwoFactor(user, isPersistent).WithCurrentCulture();
            }
            ...
            return SignInStatus.Failure;
        }

It always calls UserManager.IsLockedOutAsync() before it tries to check the password, so if the store doesn't implement the IUserLockoutStore interface, an exception gets thrown every time no matter what.

它总是在尝试检查密码之前调用UserManager.IsLockedOutAsync(),因此如果存储没有实现IUserLockoutStore接口,则无论何时都会抛出异常。

Does this mean that to use the default functionality of the UserManager and SignInManager classes, you need to implement every I*Store interface?

这是否意味着要使用UserManager和SignInManager类的默认功能,您需要实现每个I * Store接口吗?

It looks the the workaround is to inherit from SignInManager and override the PasswordSignInAsync method. Is that the standard practice?

看起来解决方法是从SignInManager继承并覆盖PasswordSignInAsync方法。这是标准做法吗?

Thanks!

1 个解决方案

#1


6  

What I found that Identity framework is not consistent with "optionality" of required I*Store. In some public methods it checks if the required Store is provided, in some other places it just calls for the method. I have not figured out which ones are absolutely required and which ones can be not called. So I'd go with the exception trail and implement whatever the stores are required for your application.

我发现Identity框架与所需I * Store的“可选性”不一致。在一些公共方法中,它检查是否提供了所需的Store,在其他一些地方它只是调用该方法。我还没弄清楚哪些是绝对必需的,哪些不能被调用。因此,我将使用异常跟踪并实现应用程序所需的任何存储。

#1


6  

What I found that Identity framework is not consistent with "optionality" of required I*Store. In some public methods it checks if the required Store is provided, in some other places it just calls for the method. I have not figured out which ones are absolutely required and which ones can be not called. So I'd go with the exception trail and implement whatever the stores are required for your application.

我发现Identity框架与所需I * Store的“可选性”不一致。在一些公共方法中,它检查是否提供了所需的Store,在其他一些地方它只是调用该方法。我还没弄清楚哪些是绝对必需的,哪些不能被调用。因此,我将使用异常跟踪并实现应用程序所需的任何存储。