ASP.NET Core Identity UserManager<ApplicationUser> - 错误没有为此对象定义无参数构造函数
ASP.NET Core Identity UserManager<ApplicationUser> -Error No parameterless constructor defined for this object
我只是想开发一个用于身份验证和授权的示例项目,并遵循link
ASP.NET Core MVC: Authentication and Role Based Authorisation with Identity
当运行代码时,我得到一个错误
堆栈跟踪:
[MissingMethodException: No parameterless constructor defined for this
object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type,
Boolean publicOnly, Boolean noCheck, Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean
skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly,
Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)
+247 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type) +11 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext
requestContext, Type controllerType) +55
[InvalidOperationException: An error occurred when trying to create a
controller of type 'SampleAuthProject.Controllers.HomeController'.
Make sure that the controller has a parameterless public constructor.]
System.Web.Mvc.DefaultControllerActivator.Create(RequestContext
requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext
requestContext, Type controllerType) +76
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext
requestContext, String controllerName) +88
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase
httpContext, IController& controller, IControllerFactory& factory)
+194 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext,
AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext
context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
+103 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep
step, Boolean& completedSynchronously) +159
我认为这个错误是由于 DI。
所以在我的startup.cs中包含
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
public Startup()
{
var builder = new ConfigurationBuilder();
Configurations = builder.Build();
}
public IConfigurationRoot Configurations { get; }
public void ConfigureServices(IServiceCollection services)
{
try
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configurations.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<ApplicationDbContext>();
}
catch (Exception ex)
{
throw ex;
}
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseIdentity();
}
我的家庭控制器是
[Microsoft.AspNetCore.Authorization.Authorize]
public class HomeController : Controller
{
private readonly UserManager<ApplicationUser> userManager;
public HomeController(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
[Microsoft.AspNetCore.Authorization.Authorize(Roles = "User")]
public ActionResult Index()
{
//userManager=new UserManager<ApplicationUser>();
string userName = userManager.GetUserName(System.Security.Claims.ClaimsPrincipal.Current);
return View("Index",userName);
}
}
您似乎在尝试混合框架版本。
Startup
同时使用了 IAppBuilder
,它来自 asp。net-mvc-5 和 IApplicationBuilder
,这是最近的 asp.net-core.
另外,异常消息
No parameterless constructor defined for this object
通常与从 MVC 5 和 Web.Api 2.*
返回的模糊消息相关联
这基本上意味着该项目的依赖项注入框架不知道身份依赖项,这就是它无法将依赖项注入控制器的原因。
要么确保该项目实际上是一个 asp.net-core 项目,以便能够使用 asp.net-core-身份
Introduction to Identity on ASP.NET Core
或者使用正确版本的 MVC 5 身份框架。
我只是想开发一个用于身份验证和授权的示例项目,并遵循link
ASP.NET Core MVC: Authentication and Role Based Authorisation with Identity
当运行代码时,我得到一个错误
堆栈跟踪:
[MissingMethodException: No parameterless constructor defined for this object.] System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached, RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck) +0
System.RuntimeType.CreateInstanceSlow(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +119
System.RuntimeType.CreateInstanceDefaultCtor(Boolean publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark) +247 System.Activator.CreateInstance(Type type, Boolean nonPublic) +83 System.Activator.CreateInstance(Type type) +11 System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +55[InvalidOperationException: An error occurred when trying to create a controller of type 'SampleAuthProject.Controllers.HomeController'. Make sure that the controller has a parameterless public constructor.] System.Web.Mvc.DefaultControllerActivator.Create(RequestContext requestContext, Type controllerType) +178
System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext requestContext, Type controllerType) +76
System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName) +88
System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory) +194 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state) +50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state) +48
System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData) +16
System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +103 System.Web.HttpApplication.ExecuteStepImpl(IExecutionStep step) +48 System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +159
我认为这个错误是由于 DI。
所以在我的startup.cs中包含
public void Configuration(IAppBuilder app)
{
ConfigureAuth(app);
}
public Startup()
{
var builder = new ConfigurationBuilder();
Configurations = builder.Build();
}
public IConfigurationRoot Configurations { get; }
public void ConfigureServices(IServiceCollection services)
{
try
{
services.AddDbContext<ApplicationDbContext>(options => options.UseSqlServer(Configurations.GetConnectionString("DefaultConnection")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddTransient<UserManager<ApplicationUser>>();
services.AddTransient<ApplicationDbContext>();
}
catch (Exception ex)
{
throw ex;
}
}
public void Configure(IApplicationBuilder app,
IHostingEnvironment env, ILoggerFactory loggerFactory)
{
app.UseIdentity();
}
我的家庭控制器是
[Microsoft.AspNetCore.Authorization.Authorize]
public class HomeController : Controller
{
private readonly UserManager<ApplicationUser> userManager;
public HomeController(UserManager<ApplicationUser> userManager)
{
this.userManager = userManager;
}
[Microsoft.AspNetCore.Authorization.Authorize(Roles = "User")]
public ActionResult Index()
{
//userManager=new UserManager<ApplicationUser>();
string userName = userManager.GetUserName(System.Security.Claims.ClaimsPrincipal.Current);
return View("Index",userName);
}
}
您似乎在尝试混合框架版本。
Startup
同时使用了 IAppBuilder
,它来自 asp。net-mvc-5 和 IApplicationBuilder
,这是最近的 asp.net-core.
另外,异常消息
No parameterless constructor defined for this object
通常与从 MVC 5 和 Web.Api 2.*
返回的模糊消息相关联这基本上意味着该项目的依赖项注入框架不知道身份依赖项,这就是它无法将依赖项注入控制器的原因。
要么确保该项目实际上是一个 asp.net-core 项目,以便能够使用 asp.net-core-身份
Introduction to Identity on ASP.NET Core
或者使用正确版本的 MVC 5 身份框架。