Asp.net core Asp.net核心MVC授权属性未阻止
“授权”属性不起作用。我没有登录,它允许我访问此功能 我已经在底部附加了Startup.cs。请帮我开始这个。我已经在以前版本的MVC上成功地使用了这些方法,但在MVC核心上还没有成功 在此之后,我希望添加角色。任何关于从何处开始的指示都将不胜感激。 谢谢Asp.net core Asp.net核心MVC授权属性未阻止,asp.net-core,Asp.net Core,“授权”属性不起作用。我没有登录,它允许我访问此功能 我已经在底部附加了Startup.cs。请帮我开始这个。我已经在以前版本的MVC上成功地使用了这些方法,但在MVC核心上还没有成功 在此之后,我希望添加角色。任何关于从何处开始的指示都将不胜感激。 谢谢 公共类安全访问控制器:控制器 { 私有SecurityAccessDbContext SecurityAccessDbContext{get;set;} public SecurityAccessController([FromService
公共类安全访问控制器:控制器
{
私有SecurityAccessDbContext SecurityAccessDbContext{get;set;}
public SecurityAccessController([FromServices]SecurityAccessDbContext SecurityAccessDbContext)
{
this.SecurityAccessDbContext=SecurityAccessDbContext;
}
//获取://
[授权]
公共IActionResult索引()
{
返回视图();
}
}
这是我的初创公司
根据以下评论建议进行更新
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMemoryCache();
services.AddSession();
//Added
services.AddBootstrapPagerGenerator(options => {options.ConfigureDefault();});
//Database services
services.AddEntityFrameworkSqlServer().AddDbContext<SecurityAccessDbContext>(options => { options.UseSqlServer(Configuration["ConnectionStrings:Accumatica"]); });
services.AddEntityFrameworkSqlServer().AddDbContext<AcumaticaDbContext>(options => { options.UseSqlServer(Configuration["ConnectionStrings:Accumatica"]); });
services.AddEntityFrameworkSqlServer().AddDbContext<RMADbContext>(options => { options.UseSqlServer(Configuration["ConnectionStrings:Accumatica"]); });
services.AddEntityFrameworkSqlServer().AddDbContext<WarrantyDbContext>(options => { options.UseSqlServer(Configuration["ConnectionStrings:Accumatica"]); });
services.AddEntityFrameworkSqlServer().AddDbContext<GenericDbContext>(options => { options.UseSqlServer(Configuration["ConnectionStrings:Accumatica"]); });
services.AddEntityFrameworkSqlServer().AddDbContext<ApplicationIdentityDbContext>(options => { options.UseSqlServer(Configuration["ConnectionStrings:Accumatica"]); });
services.AddIdentity<ApplicationUser, ApplicationRole>(options =>
{
options.Cookies.ApplicationCookie.LoginPath = "/Account/Login";
options.Cookies.ApplicationCookie.AccessDeniedPath = "/Home/AccessDenied";
})
.AddEntityFrameworkStores<ApplicationIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddTransient<IEmailSender, AuthMessageSender>();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
{
loggerFactory.AddConsole(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
app.UseSession();
app.UseIdentity();
app.UseMvcWithDefaultRoute();
}
public void配置服务(IServiceCollection服务)
{
//添加框架服务。
services.AddMemoryCache();
services.AddSession();
//增加
addBootstrapAgerGenerator(选项=>{options.ConfigureDefault();});
//数据库服务
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>{options.UseSqlServer(配置[“ConnectionString:Accumatica”];});
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>{options.UseSqlServer(配置[“ConnectionString:Accumatica”];});
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>{options.UseSqlServer(配置[“ConnectionString:Accumatica”];});
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>{options.UseSqlServer(配置[“ConnectionString:Accumatica”];});
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>{options.UseSqlServer(配置[“ConnectionString:Accumatica”];});
services.AddEntityFrameworkSqlServer().AddDbContext(选项=>{options.UseSqlServer(配置[“ConnectionString:Accumatica”];});
服务.附加性(选项=>
{
options.Cookies.applicationcokie.LoginPath=“/Account/Login”;
options.Cookies.applicationcokie.AccessDeniedPath=“/Home/AccessDenied”;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();
services.AddTransient();
}
//此方法由运行时调用。使用此方法配置HTTP请求管道。
公共void配置(IApplicationBuilder应用程序、IHostingEnvironment环境、iLogger工厂)
{
loggerFactory.AddConsole(Configuration.GetSection(“Logging”);
loggerFactory.AddDebug();
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
其他的
{
app.UseExceptionHandler(“/Home/Error”);
}
app.UseStaticFiles();
app.UseSession();
app.UseIdentity();
app.UseMvcWithDefaultRoute();
}
在添加Mvc
之前添加Identity
。此外,您不需要添加授权
,就像添加标识
时所做的那样,如图所示。您还可以配置身份选项,如登录路径,而无需配置CookieAuthenticationOptions
。相反,您可以在添加标识时对其进行配置
下面是代码的一个片段
// Remove me
// services.AddAuthorization();
// Remove me too
// services.Configure<CookieAuthenticationOptions>(options =>
// ....
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.LoginPath = "/Account/Login";
options.Cookies.ApplicationCookie.AccessDeniedPath = "/Home/AccessDenied";
options.Cookies.ApplicationCookie.AutomaticChallenge = true;
options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
})
.AddEntityFrameworkStores<ApplicationIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
//删除我
//services.AddAuthorization();
//把我也带走
//配置(选项=>
// ....
服务.附加性(选项=>
{
options.Cookies.applicationcokie.LoginPath=“/Account/Login”;
options.Cookies.applicationcokie.AccessDeniedPath=“/Home/AccessDenied”;
options.Cookies.applicationcokie.automaticcchallenge=true;
options.Cookies.applicationcokie.AutomaticAuthenticate=true;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();
在添加Mvc
之前添加Identity
。此外,您不需要像添加Identity
时那样添加授权
。您还可以配置身份选项,例如登录路径,而无需配置CookieAuthenticationOptions
。相反,您可以这样做添加标识时对其进行配置
下面是代码的一个片段
// Remove me
// services.AddAuthorization();
// Remove me too
// services.Configure<CookieAuthenticationOptions>(options =>
// ....
services.AddIdentity<ApplicationUser, IdentityRole>(options =>
{
options.Cookies.ApplicationCookie.LoginPath = "/Account/Login";
options.Cookies.ApplicationCookie.AccessDeniedPath = "/Home/AccessDenied";
options.Cookies.ApplicationCookie.AutomaticChallenge = true;
options.Cookies.ApplicationCookie.AutomaticAuthenticate = true;
})
.AddEntityFrameworkStores<ApplicationIdentityDbContext>()
.AddDefaultTokenProviders();
services.AddMvc();
//删除我
//services.AddAuthorization();
//把我也带走
//配置(选项=>
// ....
服务.附加性(选项=>
{
options.Cookies.applicationcokie.LoginPath=“/Account/Login”;
options.Cookies.applicationcokie.AccessDeniedPath=“/Home/AccessDenied”;
options.Cookies.applicationcokie.automaticcchallenge=true;
options.Cookies.applicationcokie.AutomaticAuthenticate=true;
})
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
services.AddMvc();
我发现了问题
文件launchsettings.json已被删除
"iisSettings": {
"windowsAuthentication": true,
我改成
"iisSettings": {
"windowsAuthentication": false,
我发现了问题
文件launchsettings.json已被删除
"iisSettings": {
"windowsAuthentication": true,
我改成
"iisSettings": {
"windowsAuthentication": false,
上面的回答也对我有所帮助,但我可以补充一点,如果您希望[AllowAnonymous]属性起作用,您还需要将匿名身份验证更改为true:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
上面的回答也对我有所帮助,但我可以补充一点,如果您希望[AllowAnonymous]属性起作用,您还需要将匿名身份验证更改为true:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
我试过你的建议,[Authorize]仍然不起作用我试过你的建议,[Authorize]仍然不起作用