C# HttpContext.GetOwinContext().GetUserManager<;批准经理>;()返回空值

C# HttpContext.GetOwinContext().GetUserManager<;批准经理>;()返回空值,c#,asp.net-identity,identity,roles,owin,C#,Asp.net Identity,Identity,Roles,Owin,我使用ASP.NET Identity 2创建角色,但是HttpContext.GetOwinContext().GetUserManager()的结果为空 然后我无法创建角色 我怎样才能解决这个问题 public class MVCController : Controller { public MVCController() { } public AppRoleManager RoleManager // returns null ?! {

我使用ASP.NET Identity 2创建角色,但是
HttpContext.GetOwinContext().GetUserManager()
的结果为空

然后我无法创建角色

我怎样才能解决这个问题

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>();
        }
    }
} 
公共类MVC控制器:控制器
{
公共MVCController()
{
}
公共批准管理器角色管理器//返回null?!
{
得到
{
返回HttpContext.GetOwinContext().GetUserManager();
}
}
公共用户当前用户
{
得到
{
字符串currentUserId=User.Identity.GetUserId();
User currentUser=DataContextFactory.GetDataContext().Users.FirstOrDefault(x=>x.Id.ToString()==currentUserId);
返回当前用户;
}
}
公共IAAuthenticationManager AuthManager
{
得到
{
返回HttpContext.GetOwinContext().Authentication;
}
}
公共AppUserManager用户管理器
{
得到
{
返回HttpContext.GetOwinContext().GetUserManager();
}
}
} 
我的控制器:

公共类RoleAdminController:MVCController
{
[HttpPost]
公共异步任务CreateRole([必需]字符串名称)
{
if(ModelState.IsValid)
{
如果(RoleManager!=null)//RoleManager为null,为什么?!
{
IdentityResult result=await RoleManager.CreateAsync(新角色{Name=Name});
if(result.successed)
{
返回操作(“索引”);
}
其他的
{
AddErrorsFromResult(结果);
}
}
}
返回视图(名称);
}
}
审批经理:

公共类审批管理器:角色管理器,IDisposable
{
公共批准经理(RoleStore商店)
:基地(商店)
{
}
公共静态AppRoleManager创建(IdentityFactoryOptions选项,IOwinContext上下文)
{
返回新的ApprovalManager(新角色存储库(DataContextFactory.GetDataContext());
}
}

您很可能错过了向OwinContext提供创建ApplicationUserManager的方法。
为此,您需要在
public void配置(IAppBuilder应用程序)

app.CreatePerOwinContext(ApplicationUserManager.Create);
app.CreatePerOwinContext(applormanager.Create);

这将用
OwinContext
注册创建
UserManager
rolemager
的代理,然后您可以在控制器中调用这些代理。

我知道这是一篇老文章,但我想与其他人分享我的研究,对于这个问题,我创建了部分类,并在那里添加了我所有的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))
                }
            });


        }
    }

尝试
HttpContext.GetOwinContext().Get()
@trailmax:返回null。Get和GetUserManager之间的区别是什么?第一行已经添加了,但是当我将代码的第二行添加到它时,我得到了一个错误。。。。编译器错误消息:CS1928:“Owin.IAppBuilder”不包含“CreatePerOwinContext”的定义,并且最佳扩展方法重载“Owin.AppBuilderExtensions.CreatePerOwinContext(Owin.IAppBuilder,System.Func)”具有一些无效的arguments@Jahan道歉,纠正了我的回答,它应该是第二行中的一个通用参数
AppRoleManager
。方法位置:project>startup.csAppRoleManager非exist@AugustinBocken
ApplicationUserManager.Create是否返回任何内容?
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()));
    }
}
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<AppRoleManager>(AppRoleManager.Create);
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);
        }
    }