[Authorize(Policy="policy")] errore 404

di il
0 risposte

[Authorize(Policy="policy")] errore 404

Ragazzi sto davvero impazzendo. Se sul controller inserisco  l'annotazione Authorize con police o ruolo, nonostante faccia login mi restituisce sempre errore 404. Se non inserisco ruolo o police fa come se Authorize on ci fosse. Aiutatemiiii!! Vi inserisco controller e program.cs 

using AutoMapper;
using IlGiroGiusto.BLL.interfaces;
using IlGiroGiusto.BLL.models;
using IlGiroGiusto.BLL.Services;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
namespace IlGiroGiusto.PL.WebAPI.Controllers
{
       [Route("api/[controller]")]
       [ApiController]
       [Authorize(Policy="Admin")]
   public class PaymentController : ControllerBase
       {
           private readonly IPaymentService _paymentService;
           private readonly IMapper _mapper;
           public PaymentController(IPaymentService paymentService, IMapper mapper)
           {
               _paymentService = paymentService;
               _mapper = mapper;
           }
       [HttpDelete]
           [Route("DeletePayment")]
           public async Task<IActionResult> DeletePayment(int paymentId)
           {
               await _paymentService.DeletePaymentAsync(paymentId);
               return Ok();
           }
           [HttpGet("GetAllPayments")]
           public async Task<IActionResult> GetAllPayments()
           {
               return Ok(await _paymentService.GetAllPaymentsAsync());
           }
           [HttpGet("GetPaymentById")]
           public async Task<IActionResult> GetPublisherByName(int paymentId)
           {
               return Ok(await _paymentService.GetPaymentByIdAsync(paymentId));
           }
       [HttpGet("GetPaymentsByDate")]
       public async Task<IActionResult> GetPaymentsByDate(DateTime date)
       {
           return Ok(await _paymentService.GetPaymentsByDateAsync(date));
       }
       
       [HttpPost("SetPayment")]
       public async Task<IActionResult> SetPayment(int paymentId)
       {
           await _paymentService.SetPayment(paymentId);
           return Ok();
       }
   }
}
using IlGiroGiusto.BLL.interfaces;
using IlGiroGiusto.BLL.Services;
using IlGiroGiusto.PL.WebAPI.Configuration;
using IlGIroGiusto.DAL;
using IlGIroGiusto.DAL.Entities;
using IlGIroGiusto.DAL.Interfaces;
using IlGIroGiusto.DAL.Repositories;
using Microsoft.AspNetCore.Authentication.Cookies;
using Microsoft.AspNetCore.Identity;
using Microsoft.EntityFrameworkCore;
using Microsoft.OpenApi.Models;
using MovieStorage.DAL.MSSQL.Repositories;
using Swashbuckle;

var builder = WebApplication.CreateBuilder(args);
var connectionString = builder.Configuration.GetConnectionString("DefaultConnection");

builder.Services.AddDbContext<IlGiroGiustoDbContext>(options =>
   {
       options.UseSqlServer(builder.Configuration.GetConnectionString("IlGiroGiustoConnection"));
   });

builder.Services.AddAuthorization(options =>
   {
       options.AddPolicy("Admin",
               policy =>
               {
                   policy.RequireAuthenticatedUser();
                   policy.RequireRole("Admin");
               });
      
   });
builder.Services.AddIdentityCore<User>(options =>
   {
       options.Password.RequiredLength = 6;
       options.Password.RequireDigit = false;
       options.Password.RequireLowercase = false;
       options.Password.RequireUppercase = false;
       options.Password.RequireNonAlphanumeric = false;
   });


builder.Services.AddIdentity<User, IdentityRole>()
.AddEntityFrameworkStores<IlGiroGiustoDbContext>()
.AddSignInManager<SignInManager<User>>()
.AddUserManager<UserManager<User>>();

builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen(c =>
{
       c.SwaggerDoc("v1", new OpenApiInfo { Title = "IlGiroGiusto", Version = "v1" });
       //c.AddSecurityDefinition("Bearer", new OpenApiSecurityScheme
       //{
       //        In = ParameterLocation.Header,
       //        Description = "Enter 'Bearer {token}'",
       //        Name = "Authorization",
       //        Type = SecuritySchemeType.ApiKey
       //});
       //    c.AddSecurityRequirement(new OpenApiSecurityRequirement
       //    {
       //        {
       //            new OpenApiSecurityScheme
       //            {
       //                Reference = new OpenApiReference
       //                {
       //                    Type = ReferenceType.SecurityScheme,
       //                    Id = "Bearer"
       //                }
       //            },
       //            new string[] { }
       //        }
       //});
});
builder.Services.AddAuthentication(options =>
{
   options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
   options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
   options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
   options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
})
.AddCookie(options =>
{
   options.LoginPath = "/Account/Login";
   options.AccessDeniedPath = "/Account/AccessDenied";
   options.ExpireTimeSpan = TimeSpan.FromMinutes(20);
   options.SlidingExpiration = true;
});
builder.Services.AddScoped(typeof(IGenericRepository<>), typeof(GenericRepository<>));
builder.Services.AddScoped<IBookRepository, BookRepository>();
builder.Services.AddScoped<IBookService, BookService>();

builder.Services.AddScoped<IAuthorRepository, AuthorRepository>();
builder.Services.AddScoped<IAuthorService, AuthorService>();
builder.Services.AddScoped<ICartItemRepository, CartItemRepository>();
build
er.Services.AddScoped<ICartItemService, CartItemService>();


builder.Services.AddScoped<ICategoryRepository, CategoryRepository>();
builder.Services.AddScoped<ICategoryService, CategoryService>();
builder.Services.AddScoped<IPublisherRepository, PublisherRepository>();
builder.Services.AddScoped<IPublisherService, PublisherService>();
builder.Services.AddScoped<ICartItemRepository, CartItemRepository>();
builder.Services.AddScoped<IBookStockRepository, BookStockRepository>();
builder.Services.AddScoped<IBookStockService, BookStockService>();

builder.Services.AddScoped<IUserRepository, UserRepository>();
builder.Services.AddScoped<IUserService, UserService>();
builder.Services.AddScoped<IPaymentRepository, PaymentRepository>();
builder.Services.AddScoped<IPaymentService, PaymentService>();
builder.Services.AddScoped<IOrderRepository, OrderRepository>();
builder.Services.AddScoped<IOrderService, OrderService>();
builder.Services.AddScoped<IStockStatisticsService, StockStatisticsService>();

builder.Services.AddScoped<ICartRepository, CartRepository>();
builder.Services.AddScoped<ICartService, CartService>();

builder.Services.AddAutoMapper(typeof(MappingProfile));
var app = builder.Build();

   
//app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
   c.SwaggerEndpoint("/swagger/v1/swagger.json", "IlGiroGiusto");
});
app.MapControllers();
app.Run();
Devi accedere o registrarti per scrivere nel forum
0 risposte