调试ExternalLoginCallBack时触发IUserLockoutStore未实现异常
Let's break down exactly why this error is happening and walk through the fixes step by step:
Why This Error Occurs
The exception pops up because your SignInManager is trying to use user lockout logic (you enabled it in ApplicationUserManager with UserLockoutEnabledByDefault = true), but the UserStore linked to your UserManager doesn't implement the IUserLockoutStore interface.
Looking at your code, two key issues are driving this:
- Your
RegistrationController's parameterless constructor manually creates a basicUserManager<ApplicationUser>instead of using the pre-configuredApplicationUserManagerfrom the OWIN container. This basic manager doesn't have a lockout-aware store attached. - The
UserManager2property in your controller has an infinite recursion bug (return UserManager2 ?? ...) which means it can't correctly fetch the properApplicationUserManagerfrom OWIN.
Step-by-Step Fixes
1. Fix the Infinite Recursion in UserManager2
First, repair the broken UserManager2 property by referencing the private backing field instead of the property itself:
public ApplicationUserManager UserManager2 { get { return _userManager ?? HttpContext.GetOwinContext().GetUserManager<ApplicationUserManager>(); } private set { _userManager = value; } }
2. Update RegistrationController Constructors to Use ApplicationUserManager
Replace your controller's constructors to pull the pre-configured ApplicationUserManager and ApplicationSignInManager from the OWIN container instead of manually creating them:
public RegistrationController() : this( HttpContext.Current.GetOwinContext().GetUserManager<ApplicationUserManager>(), HttpContext.Current.GetOwinContext().Get<ApplicationSignInManager>() ) { } public RegistrationController(ApplicationUserManager userManager, ApplicationSignInManager signInManager) { UserManager2 = userManager; SignInManager = signInManager; }
This ensures you're using the ApplicationUserManager you set up with lockout support, which uses a UserStore<ApplicationUser> that does implement IUserLockoutStore (since your ApplicationUser inherits from IdentityUser, which includes all required lockout properties).
3. Optional: Disable Lockout If You Don't Need It
If you don't actually want user lockout functionality, you can just turn it off in ApplicationUserManager.Create:
// Replace the existing lockout enable line with: manager.UserLockoutEnabledByDefault = false;
This will prevent the SignInManager from trying to access lockout-related store methods entirely.
Verify the Fix
After making these changes, re-run your debug session. The SignInManager.ExternalSignInAsync call should execute without throwing the "Store does not implement IUserLockoutStore" exception.
内容的提问来源于stack exchange,提问作者Adem ZEINA

