我只是想开发一个用于身份验证和授权的示例项目,并按照链接
ASP.NET Core MVC: Authentication and Role Based Authorisation with Identity
运行代码时,出现错误
堆栈追踪:
[MissingMethodException:没有为此定义无参数构造函数 对象。] System.RuntimeTypeHandle.CreateInstance(RuntimeType类型, Boolean publicOnly,Boolean noCheck,Boolean& canBeCached, RuntimeMethodHandleInternal&安培; ctor,布尔& 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:尝试创建时出错 “SampleAuthProject.Controllers.HomeController”类型的控制器。 确保控制器具有无参数的公共构造函数。] 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&控制器,IControllerFactory&厂) +194 System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext,AsyncCallback回调,对象状态)+50
System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback回调,对象状态)+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 步,布尔& 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();
}
我的HomeController是
[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);
}
}
答案 0 :(得分:1)
看来你正试图混合框架版本。
Startup
正在使用来自 asp.net-mvc-5 的IAppBuilder
和来自最近的IApplicationBuilder > asp.net芯
另外,异常消息
没有为此对象定义无参数构造函数
通常与从MVC 5和Web.Api 2返回的模糊消息相关联。*
这基本上意味着该项目的依赖注入框架不知道Identity依赖关系,这就是它无法将依赖项注入控制器的原因。
要么确保项目实际上是 asp.net-core 项目才能使用 asp.net-core-identity
Introduction to Identity on ASP.NET Core
或者使用正确版本的Identity Framework for MVC 5.