Вот мой программный класс
Код: Выделить всё
builder.Services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
builder.Services.AddScoped();
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
options.DefaultSignInScheme = IdentityConstants.ApplicationScheme;
}).AddJwtBearer(options =>
{
options.SaveToken = true;
options.TokenValidationParameters = new TokenValidationParameters()
{
ValidateIssuer = true,
ValidateAudience = true,
ValidIssuer = builder.Configuration["JWTOptions:Issuer"],
ValidAudience = builder.Configuration["JWTOptions:Audience"],
IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWTOptions:SecretKey"]!))
};
}).AddCookie(IdentityConstants.ApplicationScheme, options =>
{
options.LoginPath = "/Admin/Login";
});
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Admin/Login"; // This MUST be here
options.AccessDeniedPath = "/Admin/AccessDenied";
options.Cookie.Name = "GymunityAuthCookie"; // Custom name helps debugging
});
Код: Выделить всё
[Authorize(AuthenticationSchemes = "Identity.Application")]
public async Task Index()
{
var productRepo = _unitOfWork.GetRepository
();
var queryParams = new ProductQueryParms();
var specification = new ProductWithTypeAndBrandSpec(queryParams, true);
var products = await productRepo.GetAllAsync(specification);
var productsViewModel = products.Select(product => new ProductViewModel
{
Id = product.Id,
Name = product.Name,
Description = product.Description,
PictureUrl = product.PictureUrl,
Price = product.Price,
BrandId = product.BrandId,
TypeId = product.TypeId,
Brand = product.ProductBrand,
Type = product.ProductType
});
return View(productsViewModel);
}
Есть причина?
Я изменил свой класс программы на этот
Код: Выделить всё
builder.Services.AddAuthentication(options =>
{
// Sets the default scheme for authenticating requests
options.DefaultAuthenticateScheme = IdentityConstants.ApplicationScheme;
options.DefaultChallengeScheme = IdentityConstants.ApplicationScheme;
});
builder.Services.AddIdentity()
.AddEntityFrameworkStores()
.AddDefaultTokenProviders();
builder.Services.ConfigureApplicationCookie(options =>
{
options.LoginPath = "/Admin/Login"; // This MUST be here
options.AccessDeniedPath = "/Admin/AccessDenied";
options.Cookie.Name = "GymunityAuthCookie"; // Custom name helps debugging
options.Events.OnRedirectToLogin = context =>
{
context.Response.StatusCode = StatusCodes.Status401Unauthorized;
return Task.CompletedTask;
};
options.Events.OnRedirectToAccessDenied = context =>
{
context.Response.StatusCode = StatusCodes.Status403Forbidden;
return Task.CompletedTask;
};
});
builder.Services.AddScoped();
//builder.Services.AddAuthentication(options =>
//{
// options.DefaultAuthenticateScheme = JwtBearerDefaults.AuthenticationScheme;
// options.DefaultChallengeScheme = JwtBearerDefaults.AuthenticationScheme;
//}).AddJwtBearer(options =>
//{
// options.SaveToken = true;
// options.TokenValidationParameters = new TokenValidationParameters()
// {
// ValidateIssuer = true,
// ValidateAudience = true,
// ValidIssuer = builder.Configuration["JWTOptions:Issuer"],
// ValidAudience = builder.Configuration["JWTOptions:Audience"],
// IssuerSigningKey = new SymmetricSecurityKey(Encoding.UTF8.GetBytes(builder.Configuration["JWTOptions:SecretKey"]!))
// };
//});