Rectangle 27 0

"No owin.Environment item was found in the context" exception in ASP.NET MvcWeb Api application?


public class DataModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var context = new EntityContext();
        builder.RegisterInstance(context).As<IEntityContext>().SingleInstance();
        builder.Register(x => new UserStore<ApplicationUser>(context))
          .As<IUserStore<ApplicationUser>>().InstancePerLifetimeScope();
        builder.Register(x =>
        {
            var userStore = x.Resolve<IUserStore<ApplicationUser>>();
            var userManager = new UserManager<ApplicationUser>(userStore);
            return userManager;
        });
    }
}
public class WebModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterFilterProvider();
        builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
        builder.RegisterControllers(Assembly.GetExecutingAssembly())
          .InjectActionInvoker();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // OWIN
        builder.Register(c => HttpContext.Current.GetOwinContext())
          .As<IOwinContext>().InstancePerLifetimeScope();

        // Module chaining
        builder.RegisterModule<DataModule>();
    }
}

Maybe that's the difference - all of the authentication on our site is done through MVC - there's no Web API-specific authentication going on

My EntityContext is an IdentityDbContext<ApplicationUser> that I've also wrapped in an interface so I can set up a fake user store and stuff like that.

Next, the Data Module (there's other modules too but they aren't relevant):

The latter - authentication happens via MVC and only authenticated users can use the API

The only thing that I can see that's different about what you're doing is that you don't seem to be passing the IdentityContext into your UserManager when it's registered.

We're doing something very similar in a project, which I have got working using Autofac. I have my UserManager injected into UserService (just a domain layer class at the present time rather than an actual service), but can successfully access the UserManager from both Api and MVC controller methods. Here's how I've set things up, which may help. I haven't created separate modules for API and MVC stuff, just thrown it all in together (your resolvers are set up correctly by the way).

so, you mean all api actions are public? Or that you authenticate via MVC and only authenticated users can use the api?

thanks levelnis. I'll take a look. I'm assuming you started off with an MVC application. Did you have to do anything special in the Startup class re: Owin when you were integrating web-api authentication?

Note
Rectangle 27 0

"No owin.Environment item was found in the context" exception in ASP.NET MvcWeb Api application?


public class DataModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var context = new EntityContext();
        builder.RegisterInstance(context).As<IEntityContext>().SingleInstance();
        builder.Register(x => new UserStore<ApplicationUser>(context))
          .As<IUserStore<ApplicationUser>>().InstancePerLifetimeScope();
        builder.Register(x =>
        {
            var userStore = x.Resolve<IUserStore<ApplicationUser>>();
            var userManager = new UserManager<ApplicationUser>(userStore);
            return userManager;
        });
    }
}
public class WebModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterFilterProvider();
        builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
        builder.RegisterControllers(Assembly.GetExecutingAssembly())
          .InjectActionInvoker();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // OWIN
        builder.Register(c => HttpContext.Current.GetOwinContext())
          .As<IOwinContext>().InstancePerLifetimeScope();

        // Module chaining
        builder.RegisterModule<DataModule>();
    }
}

Maybe that's the difference - all of the authentication on our site is done through MVC - there's no Web API-specific authentication going on

My EntityContext is an IdentityDbContext<ApplicationUser> that I've also wrapped in an interface so I can set up a fake user store and stuff like that.

Next, the Data Module (there's other modules too but they aren't relevant):

The latter - authentication happens via MVC and only authenticated users can use the API

The only thing that I can see that's different about what you're doing is that you don't seem to be passing the IdentityContext into your UserManager when it's registered.

We're doing something very similar in a project, which I have got working using Autofac. I have my UserManager injected into UserService (just a domain layer class at the present time rather than an actual service), but can successfully access the UserManager from both Api and MVC controller methods. Here's how I've set things up, which may help. I haven't created separate modules for API and MVC stuff, just thrown it all in together (your resolvers are set up correctly by the way).

so, you mean all api actions are public? Or that you authenticate via MVC and only authenticated users can use the api?

thanks levelnis. I'll take a look. I'm assuming you started off with an MVC application. Did you have to do anything special in the Startup class re: Owin when you were integrating web-api authentication?

Note
Rectangle 27 0

"No owin.Environment item was found in the context" exception in ASP.NET MvcWeb Api application?


public class DataModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        var context = new EntityContext();
        builder.RegisterInstance(context).As<IEntityContext>().SingleInstance();
        builder.Register(x => new UserStore<ApplicationUser>(context))
          .As<IUserStore<ApplicationUser>>().InstancePerLifetimeScope();
        builder.Register(x =>
        {
            var userStore = x.Resolve<IUserStore<ApplicationUser>>();
            var userManager = new UserManager<ApplicationUser>(userStore);
            return userManager;
        });
    }
}
public class WebModule : Module
{
    protected override void Load(ContainerBuilder builder)
    {
        builder.RegisterFilterProvider();
        builder.RegisterType<ExtensibleActionInvoker>().As<IActionInvoker>();
        builder.RegisterControllers(Assembly.GetExecutingAssembly())
          .InjectActionInvoker();
        builder.RegisterApiControllers(Assembly.GetExecutingAssembly());

        // OWIN
        builder.Register(c => HttpContext.Current.GetOwinContext())
          .As<IOwinContext>().InstancePerLifetimeScope();

        // Module chaining
        builder.RegisterModule<DataModule>();
    }
}

Maybe that's the difference - all of the authentication on our site is done through MVC - there's no Web API-specific authentication going on

My EntityContext is an IdentityDbContext<ApplicationUser> that I've also wrapped in an interface so I can set up a fake user store and stuff like that.

Next, the Data Module (there's other modules too but they aren't relevant):

The latter - authentication happens via MVC and only authenticated users can use the API

The only thing that I can see that's different about what you're doing is that you don't seem to be passing the IdentityContext into your UserManager when it's registered.

We're doing something very similar in a project, which I have got working using Autofac. I have my UserManager injected into UserService (just a domain layer class at the present time rather than an actual service), but can successfully access the UserManager from both Api and MVC controller methods. Here's how I've set things up, which may help. I haven't created separate modules for API and MVC stuff, just thrown it all in together (your resolvers are set up correctly by the way).

so, you mean all api actions are public? Or that you authenticate via MVC and only authenticated users can use the api?

thanks levelnis. I'll take a look. I'm assuming you started off with an MVC application. Did you have to do anything special in the Startup class re: Owin when you were integrating web-api authentication?

Note