No ApplicationRoleManager class in my MVC 5 template - asp.net-mvc

When I try to add app.CreatePerOwinContext(ApplicationRoleManager.Create) to configauth I am told applicationRoleManager does not exist. Does anybody know why?
using Microsoft.Owin.Security.OAuth;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.EntityFramework;
using Microsoft.AspNet.Identity.Owin;
using Microsoft.Owin;
using Microsoft.Owin.Security.Cookies;
using Microsoft.Owin.Security.DataProtection;
using Microsoft.Owin.Security.Google;
using Owin;
using System;
using LaCeibaNetv4.Models;
namespace LaCeibaNetv4
{
public partial class Startup
{
// For more information on configuring authentication, please visit http://go.microsoft.com/fwlink/?LinkId=301864
public void ConfigureAuth(IAppBuilder app)
{
// Configure the db context and user manager to use a single instance per request
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);

I was having the same issue and came across this code which I used to add the class to my project:
public class ApplicationRoleManager : RoleManager<IdentityRole>
{
public ApplicationRoleManager(IRoleStore<IdentityRole,string> roleStore)
: base(roleStore) { }
public static ApplicationRoleManager Create(
IdentityFactoryOptions<ApplicationRoleManager> options,
IOwinContext context)
{
var manager = new ApplicationRoleManager(
new RoleStore<IdentityRole>(context.Get<ApplicationDbContext>()));
return manager;
}
}
Also, remember to have it configured at startup by adding this piece of code in Startup.Auth.cs:
app.CreatePerOwinContext<ApplicationRoleManager>(Application‌​RoleManager.Create);
The full article is available here: http://www.codeproject.com/Articles/762428/ASP-NET-MVC-and-Identity-Understanding-the-Basics

Related

Parameterless constructor error with autofac in web api 2

I am using Autofac as IOC and here is my structure:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdTudent.Repo
{
public interface IRepository
{
IAdvertisementRepository Advertisements { get; set; }
IProfileRepository Profiles { get; set; }
}
}
My repositories class is:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AdTudent.Repo
{
public class Repositories : IRepository
{
public Repositories(IAdvertisementRepository advertisementRepository,
IProfileRepository profileRepository)
{
Advertisements = advertisementRepository;
Profiles = profileRepository;
}
public IAdvertisementRepository Advertisements { get; set; }
public IProfileRepository Profiles { get; set; }
}
}
And my startup class is:
public partial class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
builder.RegisterType<Repositories>().As<IRepository>();
builder.Register<IGraphClient>(context =>
{
var graphClient = new GraphClient(new Uri("http://localhost:7474/db/data"));
graphClient.Connect();
return graphClient;
}).SingleInstance();
// STANDARD WEB API SETUP:
// Get your HttpConfiguration. In OWIN, you'll create one
// rather than using GlobalConfiguration.
var config = new HttpConfiguration();
// Register your Web API controllers.
//builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// Run other optional steps, like registering filters,
// per-controller-type services, etc., then set the dependency resolver
// to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// OWIN WEB API SETUP:
// Register the Autofac middleware FIRST, then the Autofac Web API middleware,
// and finally the standard Web API middleware.
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
ConfigureAuth(app);
}
}
and here is my account controller:
public class AccountController : ApiController
{
private IRepository _repository;
private const string LocalLoginProvider = "Local";
private ApplicationUserManager _userManager;
private IGraphClient _graphClient;
public ISecureDataFormat<AuthenticationTicket> AccessTokenFormat { get; private set; }
public AccountController(IRepository repository, IGraphClient graphClient,
ISecureDataFormat<AuthenticationTicket> accessTokenFormat)
{
AccessTokenFormat = accessTokenFormat;
_repository = repository;
_graphClient = graphClient;
}
}
but I always this problem
"Message": "An error has occurred.",
"ExceptionMessage": "An error occurred when trying to create a controller of type 'AccountController'. Make sure that the controller has a parameterless public constructor.",
"ExceptionType": "System.InvalidOperationException",
"StackTrace": " at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)\r\n at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()
I searched on the internet but I couldn't find anything that can help can anyone guide me?
Per the WebApi OWIN documentation:
A common error in OWIN integration is use of the GlobalConfiguration.Configuration. In OWIN you create the configuration from scratch. You should not reference GlobalConfiguration.Configuration anywhere when using the OWIN integration.
It looks like you have a couple of issues:
The HttpConfiguration needs to be newed up when using OWIN.
You are missing the UseAutofacWebApi virtual method call.
You also need the Autofac WebApi OWIN package as per the docs.
public class Startup
{
public void Configuration(IAppBuilder app)
{
var builder = new ContainerBuilder();
// STANDARD WEB API SETUP:
// Get your HttpConfiguration. In OWIN, you'll create one
// rather than using GlobalConfiguration.
var config = new HttpConfiguration();
// Register your Web API controllers.
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
// Run other optional steps, like registering filters,
// per-controller-type services, etc., then set the dependency resolver
// to be Autofac.
var container = builder.Build();
config.DependencyResolver = new AutofacWebApiDependencyResolver(container);
app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);
// OWIN WEB API SETUP:
// Register the Autofac middleware FIRST, then the Autofac Web API middleware,
// and finally the standard Web API middleware.
app.UseAutofacMiddleware(container);
app.UseAutofacWebApi(config);
app.UseWebApi(config);
}
}
Check your Global.asax.cs. You should remove this line if you have it:
GlobalConfiguration.Configure(WebApiConfig.Register);
You can move this to Startup.cs Configuration like this:
WebApiConfig.Register(config);
You haven't told Autofac how to create an ISecureDataFormat<AuthenticationTicket> instance.
Add this in your startup class before your call builder.Build().
builder.RegisterType<ISecureDataFormat<AuthenticationTicket>>().As<TicketDataFormat>();

I want to make multi tenant site in identity 2.0 but not using user table. Can anyone give any demo project

public void ConfigureAuth(IAppBuilder app)
{
Startup.DataProtectionProvider = app.GetDataProtectionProvider();
app.CreatePerOwinContext(ApplicationDbContext.Create);
app.CreatePerOwinContext<ApplicationUserManager>(ApplicationUserManager.Create);
app.CreatePerOwinContext<ApplicationRoleManager>(ApplicationRoleManager.Create);
app.CreatePerOwinContext<ApplicationCustomManager>(ApplicationCustomManager.Create);
}
I Need custom manager code for identity 2.0 in asp.net mvc.
Here i need tenant not for user but i have a table named site. I want site CURD handle by owin context.
I need any example of custom application manager code for any other entity except user and role.
Hope this gets you started
public class ApplicationCustomManager : IDisposable
{
public ApplicationCustomManager(ApplicationDbContext _applicationDbContext)
{
//do any operations as per your requirements with _applicationDbContext
}
public static ApplicationCustomManager Create(IdentityFactoryOptions<ApplicationCustomManager> options, IOwinContext context)
{
var _applicationCustomManager = new ApplicationCustomManager(context.Get<ApplicationDbContext>());
return _applicationCustomManager;
}
public void Dispose()
{
}

Ninject + WebApi + Custom Membership Provider + Property Injection

I have a repository that i use to access user info IAccountCore and I have a custom membership provider that I've implemented as well. I have property injection setup in the membership provider but it seems that they're never injected. I've followed all the examples on the net to no avail.
I have the ninject bootstrapper + web activator firing the kernel.Inject(Membership.Provider) in the post startup method but the properties are always null. Here are some code snippets:
Bootstrapper in WebApi
[assembly: WebActivator.PostApplicationStartMethod(typeof(API.App_Start.NinjectWebCommon), "RegisterMembership")]
Method being called from above:
public static void RegisterMembership()
{
//bootstrapper.Kernel.Inject(Membership.Provider);
bootstrapper.Kernel.Load(new MembershipNinjectModule());
}
Ninject Module (in a different assembly/project):
public class MembershipNinjectModule : NinjectModule
{
/// <summary>
/// Loads the module into the kernel.
/// </summary>
public override void Load()
{
var kernel = Kernel;
kernel.Inject(Membership.Provider);
}
}
Property in custom membership provider:
[Inject]
public IAccountCore AccountCore {get;set;}
Bindings in another ninject module:
public class NinjectConfigurator : NinjectModule
{
public override void Load()
{
var kernel = Kernel
kernel.Bind<IAccountCore>().To<AccountCore>().InTransientScope();
}
}
Code that loads above module in the Ninject boot strapper:
private static void RegisterServices(IKernel kernel)
{
kernel.Load(new NinjectConfigurator());
}
Try to add on the constructor of your custom membership provider this line :
public MyCustomMembershipProvider()
{
NinjectWebCommon.Kernel.Inject(this);
}
On my project, I used an custom membership provider with Ninject, and I just have this line more than you.
Hope it helps

Import aspect is not working using MEF

using mefcontrib.mvc3 on my mvc web application. used following class for initialize mef before application start event. That class is auto generated(downloaded) from nuget. some tiny changes applied for catalog(for plugins folder) and controller factory segments.
[assembly: WebActivator.PreApplicationStartMethod(typeof(OMR.CMS.App_Start.MefContribMVC3), "Start")]
namespace OMR.CMS.App_Start
{
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Web.Mvc;
using MefContrib.Hosting.Conventions;
using MefContrib.Web.Mvc;
public static class MefContribMVC3
{
public static void Start()
{
// Register the CompositionContainerLifetimeHttpModule HttpModule.
// This makes sure everything is cleaned up correctly after each request.
CompositionContainerLifetimeHttpModule.Register();
// Create MEF catalog based on the contents of ~/bin.
//
// Note that any class in the referenced assemblies implementing in "IController"
// is automatically exported to MEF. There is no need for explicit [Export] attributes
// on ASP.NET MVC controllers. When implementing multiple constructors ensure that
// there is one constructor marked with the [ImportingConstructor] attribute.
var catalog = new AggregateCatalog(
//new DirectoryCatalog("bin"),
new DirectoryCatalog("Plugins"),
new ConventionCatalog(new MvcApplicationRegistry())); // Note: add your own (convention)catalogs here if needed.
// Tell MVC3 to use MEF as its dependency resolver.
var dependencyResolver = new CompositionDependencyResolver(catalog);
DependencyResolver.SetResolver(dependencyResolver);
// Tell MVC3 to resolve dependencies in controllers
ControllerBuilder.Current.SetControllerFactory(new MyMEFControllerFactory(dependencyResolver));
// Tell MVC3 to resolve dependencies in filters
FilterProviders.Providers.Remove(FilterProviders.Providers.Single(f => f is FilterAttributeFilterProvider));
FilterProviders.Providers.Add(new CompositionFilterAttributeFilterProvider(dependencyResolver));
// Tell MVC3 to resolve dependencies in model validators
ModelValidatorProviders.Providers.Remove(ModelValidatorProviders.Providers.OfType<DataAnnotationsModelValidatorProvider>().Single());
ModelValidatorProviders.Providers.Add(
new CompositionDataAnnotationsModelValidatorProvider(dependencyResolver));
// Tell MVC3 to resolve model binders through MEF. Note that a model binder should be decorated
// with [ModelBinderExport].
ModelBinderProviders.BinderProviders.Add(
new CompositionModelBinderProvider(dependencyResolver));
}
}
}
and using followin code on my application class:
using OMR.Core.Web.Filters;
using OMR.Core.Web.Routing;
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace OMR.CMS
{
public class MvcApplication : System.Web.HttpApplication
{
[ImportMany(typeof(IFilterRegistration))]
public IEnumerable<Lazy<IFilterRegistration>> Filters { get; set; }
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
BundleConfig.RegisterBundles(BundleTable.Bundles);
RegisterFilters();
RegisterRoutes();
}
private void RegisterFilters()
{
var filters = GlobalFilters.Filters;
var filterRegistrations = DependencyResolver.Current.GetServices<IFilterRegistration>();
foreach (var registar in filterRegistrations)
registar.Register(filters);
}
public void RegisterRoutes()
{
var routes = RouteTable.Routes;
var routeRegistrations = DependencyResolver.Current.GetServices<IRouteRegistration>();
// Ignores
foreach (var registrar in routeRegistrations)
registrar.Ignore(routes);
// Routes
foreach (var registrar in routeRegistrations)
registrar.Route(routes);
}
}
}
The problem is following property is currently null on application start method.
ImportMany(typeof(IFilterRegistration))]
public IEnumerable<Lazy<IFilterRegistration>> Filters { get; set; }
But following lines is working:
var filterRegistrations = DependencyResolver.Current.GetServices<IFilterRegistration>();
Why my import declaration isn't work?

ASP.NET MVC WebApi: No parameterless constructor defined for this object

I have an ASP.NET MVC 4 Application that I want to implement Unit of Work Pattern.
In my Web Project I have:
IocConfig.cs
using System.Web.Http;
using NinjectMVC.Data;
using NinjectMVC.Data.Contracts;
using Ninject;
namespace NinjectMVC
{
public class IocConfig
{
public static void RegisterIoc(HttpConfiguration config)
{
var kernel = new StandardKernel(); // Ninject IoC
// These registrations are "per instance request".
// See http://blog.bobcravens.com/2010/03/ninject-life-cycle-management-or-scoping/
kernel.Bind<RepositoryFactories>().To<RepositoryFactories>()
.InSingletonScope();
kernel.Bind<IRepositoryProvider>().To<RepositoryProvider>();
kernel.Bind<INinjectMVCUow>().To<NinjectMVCUow>();
// Tell WebApi how to use our Ninject IoC
config.DependencyResolver = new NinjectDependencyResolver(kernel);
}
}
}
Global.asax
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Web.Mvc;
using System.Web.Optimization;
using System.Web.Routing;
namespace NinjectMVC
{
// Note: For instructions on enabling IIS6 or IIS7 classic mode,
// visit http://go.microsoft.com/?LinkId=9394801
public class MvcApplication : System.Web.HttpApplication
{
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
// Tell WebApi to use our custom Ioc (Ninject)
IocConfig.RegisterIoc(GlobalConfiguration.Configuration);
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
AuthConfig.RegisterAuth();
}
}
}
PersonsController.cs
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using NinjectMVC.Data.Contracts;
using NinjectMVC.Model;
namespace NinjectMVC.Controllers
{
public class PersonsController : ApiControllerBase
{
public PersonsController(INinjectMVCUow uow)
{
Uow = uow;
}
#region OData Future: IQueryable<T>
//[Queryable]
// public IQueryable<Person> Get()
#endregion
// GET /api/persons
public IEnumerable<Person> Get()
{
return Uow.Persons.GetAll()
.OrderBy(p => p.FirstName);
}
// GET /api/persons/5
public Person Get(int id)
{
var person = Uow.Persons.GetById(id);
if (person != null) return person;
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
// OData: GET /api/persons/?firstname=\'Hans\''
// With OData query syntax we would not need such methods
// /api/persons/getbyfirstname?value=Joe1
[ActionName("getbyfirstname")]
public Person GetByFirstName(string value)
{
var person = Uow.Persons.GetAll()
.FirstOrDefault(p => p.FirstName.StartsWith(value));
if (person != null) return person;
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
}
// Update an existing person
// PUT /api/persons/
public HttpResponseMessage Put(Person person)
{
Uow.Persons.Update(person);
Uow.Commit();
return new HttpResponseMessage(HttpStatusCode.NoContent);
}
}
}
When I try to surf: http://www.domain.com/Persons/Get Im getting:
No parameterless constructor defined for this object.
Is there any stuff I have missed? I would appreciate any help.
Here is the zip file of the project for better references:
http://filebin.ca/E6aoOkaUpbQ/NinjectMVC.zip
Wep.API uses a different IDependencyResolver than the MVC framework.
When you use theHttpConfiguration.DependencyResolver it only works for ApiControllers. But your ApiControllerBase derives from Controller...
So your ApiControllerBase should inherit from ApiController.
Change it in your ApiBaseController.cs:
public abstract class ApiControllerBase : ApiController
{
}
If you want to inject dependencies to regular Controller derived classes you need to use ( in your IocConfig):
System.Web.Mvc.DependencyResolver.SetResolver(new NinjectMvcDependencyResolver(container));
Note that in this case you cannot use your NinjectDependencyResolver because it's for ApiControllers.
So you need a different NinjectMvcDependencyResolver which should implement System.Web.Mvc.IDependencyResolver.
public class NinjectMvcDependencyResolver: NinjectDependencyScope,
System.Web.Mvc.IDependencyResolver
{
private IKernel kernel;
public NinjectDependencyResolver(IKernel kernel)
: base(kernel)
{
this.kernel = kernel;
}
}
I know this is an old thread, but this might be useful for someone. :)
In my case, it was missing the DependencyResolver on NinjectWebCommon.cs.
I followed this article and everything worked fine.
RegisterServices(kernel);
GlobalConfiguration.Configuration.DependencyResolver = new NinjectDependencyResolver(kernel);
return kernel;

Resources