我已经为我的 Web 应用程序实现了自定义用户存储。到目前为止一切正常。 现在我想扩展商店类,以支持角色。我实现了以下接口(interface):
IUserStore<UserViewModel>, IUserLockoutStore<UserViewModel, string>, IUserPasswordStore<UserViewModel, string>, IUserTwoFactorStore<UserViewModel, string>, IUserRoleStore<UserViewModel, string>, IRoleStore<RoleModel, string>
并使用 Owin AppBuilder 注册服务如下:
var ctx = UnityMvcActivator.Context.Container;
app.CreatePerOwinContext(() => ctx.Resolve<WebUserManager>());
app.CreatePerOwinContext(() => ctx.Resolve<RoleManager>());
app.CreatePerOwinContext(() => ctx.Resolve<WebApplicationUserStore>());
如果我回到我的 Controller 并调用 User.IsInRole(Constants.Roles.COMPANY_OWNER)
不会调用商店来获取用户的角色。我想有某种缺失的链接,但我找不到它。
我还实现了一个 UserManager<UserViewModel, string>
与 SupportsUserRole
属性显式设置为 true,这完全没有区别。
最佳答案
User.IsInRole
不检查您的数据库,只检查当前用户的 cookie。它将为您提供存储在 cookie 中的角色,但这些角色可能与您在数据库中拥有的角色不同。如果用户角色自上次刷新 cookie 后发生变化,则可能会出现差异。
如果您需要检查数据库中的角色,请使用 UserManager.IsInRoleAsync
( MSDN )
关于c# - 根本不调用 ASP.MVC IUserRoleStore<>,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47782898/