gpt4 book ai didi

ASP.NET Core 更改 AccessDenied 路由

转载 作者:行者123 更新时间:2023-12-04 09:23:44 26 4
gpt4 key购买 nike

我在路由 AccessDenied 时遇到了一些问题,也可能是登录/注销路径。该项目是一个剥离的默认项目,没有更多的魔法。 Soo 存在一个 Account带有 AccessDenied() 的 Controller 方法。

我现在正在尝试的是(这是互联网商品提供的解决方案)

services.Configure<CookieAuthenticationOptions>(options =>
{
options.LoginPath = new PathString("/");
options.AccessDeniedPath = new PathString("/InactiveSponsor");
options.LogoutPath = new PathString("/");
});

但这完全没有区别。那么有什么想法吗?关于为什么它不起作用以及如何使它起作用的任何想法。

这是我的 Startup.cs
public Startup(IHostingEnvironment env)
{
var builder = new ConfigurationBuilder()
.SetBasePath(env.ContentRootPath)
.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
.AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
.AddEnvironmentVariables();

if (env.IsDevelopment())
{
// This will push telemetry data through Application Insights pipeline faster, allowing you to view results immediately.
builder.AddApplicationInsightsSettings(developerMode: true);
}
Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddApplicationInsightsTelemetry(Configuration);

string connection = "DefaultConnection";
//services.AddDbContext<SponsorContext>(options => options.UseSqlServer(connection));
services.AddDbContext<SponsorContext>(options => options.UseSqlServer(Configuration[$"Data:{connection}"]));

services.AddIdentity<ApplicationUser, ApplicationRole>()
.AddEntityFrameworkStores<SponsorContext>()
.AddDefaultTokenProviders();


services.AddMvc();


services.AddAuthorization(options =>
{
options.AddPolicy(Policies.RequireAdmin, policy => policy.RequireRole(Roles.Administrator));
options.AddPolicy(Policies.IsSponsor, policy => policy.RequireRole(Roles.Sponsor));
options.AddPolicy(Policies.IsSponsorOrAdmin, policy => policy.RequireRole(Roles.Administrator, Roles.Sponsor));
});

/*
* AddTransient Different on each instance/use
* AddScoped Different instance on a per request basis
* AddSingleton Always the same instance
*/
//DI
services.AddScoped<ManageUserRepository>();
services.AddScoped<ISponsorManagement, SponsorRepository>();
services.AddScoped<ISponsorRead, SponsorRepository>();
}

// 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();

app.UseApplicationInsightsRequestTelemetry();

if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}

app.UseApplicationInsightsExceptionTelemetry();

app.UseStaticFiles();
app.UseIdentity();


app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}

最佳答案

尝试

 services.AddIdentity<ApplicationUser, IdentityRole>(op=>op.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/InactiveSponsor"))
.AddEntityFrameworkStores<SponsorContext>()
.AddDefaultTokenProviders();

或者
        services.Configure<IdentityOptions>(opt =>
{
opt.Cookies.ApplicationCookie.LoginPath = new PathString("/aa");
opt.Cookies.ApplicationCookie.AccessDeniedPath = new PathString("/InactiveSponsor");
opt.Cookies.ApplicationCookie.LogoutPath = new PathString("/");
});

关于ASP.NET Core 更改 AccessDenied 路由,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39250272/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com