HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() 返回 null
Posted
技术标签:
【中文标题】HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() 返回 null【英文标题】:HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() return null 【发布时间】:2014-12-08 11:11:16 【问题描述】:我使用 ASP.NET Identity 2 创建角色,但 HttpContext.GetOwinContext().GetUserManager<AppRoleManager>()
的结果为 null。
然后我无法创建角色。
我该如何解决这个问题?
public class MVCController : Controller
public MVCController()
public AppRoleManager RoleManager // returns null ?!
get
return HttpContext.GetOwinContext().GetUserManager<AppRoleManager>();
public User CurrentUser
get
string currentUserId = User.Identity.GetUserId();
User currentUser = DataContextFactory.GetDataContext().Users.FirstOrDefault(x => x.Id.ToString() == currentUserId);
return currentUser;
public IAuthenticationManager AuthManager
get
return HttpContext.GetOwinContext().Authentication;
public AppUserManager UserManager
get
return HttpContext.GetOwinContext().GetUserManager<AppUserManager>();
我的控制器:
public class RoleAdminController : MVCController
[HttpPost]
public async Task<ActionResult> CreateRole([Required]string name)
if (ModelState.IsValid)
if (RoleManager != null) // RoleManager is null, why?!
IdentityResult result = await RoleManager.CreateAsync(new Role Name = name );
if (result.Succeeded)
return RedirectToAction("Index");
else
AddErrorsFromResult(result);
return View(name);
AppRoleManager:
public class AppRoleManager : RoleManager<Role, int>, IDisposable
public AppRoleManager(RoleStore<Role, int, UserRole> store)
: base(store)
public static AppRoleManager Create(IdentityFactoryOptions<AppRoleManager> options, IOwinContext context)
return new AppRoleManager(new RoleStore<Role, int, UserRole>(DataContextFactory.GetDataContext()));
【问题讨论】:
试试HttpContext.GetOwinContext().Get<AppRoleManager>();
@trailmax : 返回 null。
Get 和 GetUserManager 有什么区别?
【参考方案1】:
您很可能错过了为 OwinContext 提供创建 ApplicationUserManager 的方法。
为此,您需要在 public void Configuration(IAppBuilder app)
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);
这将使用OwinContext
注册创建UserManager
和RoleManager
的委托,然后您才能在控制器中回调它们。
【讨论】:
已经添加了第一行,但是当我向其中添加代码的第二行时,出现错误.... 编译器错误消息:CS1928:'Owin.IAppBuilder' 不包含'CreatePerOwinContext' 和最佳扩展方法重载 'Owin.AppBuilderExtensions.CreatePerOwinContextAppRoleManager
作为第二行中的通用参数。
方法位置:project>startup.cs
AppRoleManager 不存在
@AugustinBocken ApplicationUserManager.Create
有什么回报吗?【参考方案2】:
我知道这是旧帖子,但我想与其他人分享我的研究,对于这个问题,我创建了部分类并在那里添加了我所有的 owin 参考:
public partial class Startup
public void ConfigureAuth(IAppBuilder app)
// Configure the db context, user manager and signin manager to use a single instance per request
app.CreatePerOwinContext(IdentityModels.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationSignInManager>(ApplicationSignInManager.Create);
app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);
app.UseCookieAuthentication(new CookieAuthenticationOptions
AuthenticationType = DefaultAuthenticationTypes.ApplicationCookie,
LoginPath = new PathString("/Account/Login"),
Provider = new CookieAuthenticationProvider
// Enables the application to validate the security stamp when the user logs in.
// This is a security feature which is used when you change a password or add an external login to your account.
OnValidateIdentity = SecurityStampValidator.OnValidateIdentity<ApplicationUserManager, ApplicationUser>(
validateInterval: TimeSpan.FromMinutes(30),
regenerateIdentity: (manager, user) => user.GenerateUserIdentityAsync(manager))
);
然后我在其他部分启动中使用此方法:
public partial class Startup
public void Configuration(IAppBuilder app)
ConfigureAuth(app);
【讨论】:
以上是关于HttpContext.GetOwinContext().GetUserManager<AppRoleManager>() 返回 null的主要内容,如果未能解决你的问题,请参考以下文章