Identity 2.0 ApplicationRoleManager和ApplicationUserManager从OWIN Context中撤回null

时间:2014-10-13 18:07:56

标签: asp.net-mvc asp.net-web-api asp.net-identity owin

该项目是Web API 2.0。我有一个控制器,我试图在Identity表中添加角色。我正在使用OWIN中间件。另外,我正在使用StructureMap for DI。 ApplicationUserManager和ApplicationRoleManager都在控制器中返回空值。

我的启动课程

public void Configuration(IAppBuilder app)
    {
        HttpConfiguration config = new HttpConfiguration();
        ConfigureOAuth(app);
        WebApiConfig.Register(config);
        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
        app.UseWebApi(config);
        app.CreatePerOwinContext(ApartmentContext.Create);
        app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
        app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);

        config.Services.Replace(typeof(IHttpControllerActivator), new ServiceActivator(config));
        ConfigureContainer();            
    }

DI Container

 public void ConfigureContainer()
    {
        ObjectFactory.Configure(x =>
            {
                x.For<DbContext>().Use(() => new ApartmentContext());
                x.For<IAuthRepository>().Use<AuthRepository>();
                x.For<IUserStore<ApplicationUser>>().Use<UserStore<ApplicationUser>>();
                x.For<IRoleStore<ApplicationRole,string>>().Use<RoleStore<ApplicationRole>>();
            });
    }

控制器内部的代码&lt; ---所有方法都返回NULL。检索到OWIN上下文,但不检索GET&lt;&gt;。

的值
            var test1 = Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var test2 = System.Web.HttpContext.Current.Request.GetOwinContext().GetUserManager<ApplicationUserManager>();
            var test3 = System.Web.HttpContext.Current.Request.GetOwinContext().Get<ApplicationUserManager>();
            var test4 = Request.GetOwinContext().Get<ApplicationRoleManager>();
            var test5 = System.Web.HttpContext.Current.Request.GetOwinContext().Get<ApplicationRoleManager>();

这些USING语句在控制器中(除其他外):

using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using System.Net.Http;
using System.Web.Http;

// Configure the RoleManager used in the application. RoleManager is defined in the ASP.NET Identity core assembly
public class ApplicationRoleManager : RoleManager<ApplicationRole>
{
    public ApplicationRoleManager(IRoleStore<ApplicationRole, string> roleStore)
        : base(roleStore)
    {
    }

    public static ApplicationRoleManager Create(IdentityFactoryOptions<ApplicationRoleManager> options, IOwinContext context)
    {
        return new ApplicationRoleManager(new RoleStore<ApplicationRole>(context.Get<ApartmentContext>()));
    }
}

1 个答案:

答案 0 :(得分:0)

Owin需要知道如何创建ApplicationUserManager的唯一原因是,如果用户更改了安全标记,则能够使cookie无效。 (您在Auth.Startup.cs中进行配置,给出检查cookie有效的频率)

因此,您可以一直配置容器并将ApplicationUserManagerApplicationRoleManager注入控制器的构造函数中。并且不要在Owin上下文中使用GetUserManager - 这是一个隐藏的服务定位器反模式。这是Identity尝试更换DI容器而不强迫任何人使用特定容器。

对于你得到的空值,ApplicationUserManager.Create是返回实际对象还是空?