Код: Выделить всё
public class AuthorizeSessionAttribute : Attribute, IAsyncActionFilter
{
public async Task OnActionExecutionAsync(ActionExecutingContext context, ActionExecutionDelegate next)
{
if (AuthorizeCore(context))
{
await next();
}
else
{
HandleUnauthorizedRequest(context);
}
}
protected bool AuthorizeCore(ActionExecutingContext context)
{
// Some not interesting code
}
protected void HandleUnauthorizedRequest(ActionExecutingContext context)
{
// Some not interesting code
if (context.HttpContext.Request.IsAjaxRequest()) {
context.Result = new StatusCodeResult((int)HttpStatusCode.MethodNotAllowed);
}
context.Result = new RedirectToRouteResult(new RouteValueDictionary(new { action = "Index", controller = "Home" }));
}
}
Код: Выделить всё
public class Program
{
const string AppCors = "_AppCors";
public static void Main(string[] args)
{
WebApplicationBuilder builder = WebApplication.CreateBuilder(args);
#if DEBUG
builder.Services.AddDbContext(options =>
options.UseSqlServer(builder.Configuration.GetConnectionString("DefaultConnection")), ServiceLifetime.Transient
);
#endif
builder.Services.Configure(options => { options.AutomaticAuthentication = false; });
builder.Services.AddSwaggerGen();
builder.Services.AddHttpClient();
builder.Services.AddScoped();
builder.Services.AddSession(options => { options.IdleTimeout = TimeSpan.FromMinutes(30); });
builder.Services.AddSingleton();
builder.Services.AddMvc(options => { options.EnableEndpointRouting = false; })
.AddNewtonsoftJson(options => {
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
options.SerializerSettings.NullValueHandling = NullValueHandling.Ignore;
options.SerializerSettings.ContractResolver = new DefaultContractResolver();
})
.AddRazorRuntimeCompilation()
.AddRazorOptions(options => {
options.ViewLocationFormats.Add("/Views/Shared/DisplayTemplates/{0}.cshtml");
options.ViewLocationFormats.Add("/Views/Shared/EditorTemplates/{0}.cshtml");
})
.AddSessionStateTempDataProvider();
builder.Services.AddFluentValidationAutoValidation();
builder.Services.AddValidatorsFromAssemblyContaining
();
builder.Services.AddRazorPages();
WebApplication app = builder.Build();
if (app.Environment.IsDevelopment()) {
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
DefaultContractResolver contractResolver = new DefaultContractResolver {
NamingStrategy = new CamelCaseNamingStrategy()
};
app.UseCors(AppCors);
app.UseStaticFiles();
app.UseSession();
app.UseStatusCodePagesWithReExecute("/Error", "?status={0}");
app.UseMvc(routes => {
routes.MapRoute("default", "{controller=Login}/{action=Index}/{id?}");
});
app.Run();
}
}
В чем может быть проблема ? Больше у меня нет информации. Если у меня есть разрешение, я получаю в ответ код 200.
Подробнее здесь: https://stackoverflow.com/questions/790 ... ecomes-404