در جلسه 10 با گذاشتن Authorize روی اکشن index کنترلر Home در Area مربوط به UserPanel حتی با وجود Login کردن ، هنگام رفتن به UserPanel به صفحه Login ری دایرکت می کند . علت چیست؟
سلام دوست من وقتتون بخیر
دوست عزیزم بی زحمت از program.cs و startup پروژه خودتون عکس ارسال کنید
با تشکر
کد زنی در محیط VS2022 با NetCore6 فایل program.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.EntityFrameworkCore;
using System.Runtime.CompilerServices;
using TopLearn.Core.Services;
using TopLearn.DataLayer.Context;
using TopLearn.Core.Services.Interfaces;
using Microsoft.AspNetCore.Authentication.Cookies;
using TopLearn.Core.Convertors;
using System;
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddRazorPages();
//builder.Services.AddMvc();
builder.Services.AddMvc(options => options.EnableEndpointRouting = false);
#region Authebtication
builder.Services.AddAuthentication(options =>
{
options.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultChallengeScheme = CookieAuthenticationDefaults.AuthenticationScheme;
options.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
}).AddCookie(options =>
{
options.LoginPath = "/Login";
options.LogoutPath = "/Logout";
options.ExpireTimeSpan = TimeSpan.FromMinutes(43200);
});
#endregion
#region Database Context
builder.Services.AddDbContext<TopLearnContext>(options =>
{
options.UseSqlServer(builder.Configuration.GetConnectionString("TopLearnConnection"));
});
#region IoC
builder.Services.AddTransient<IUserService, UserService>();
builder.Services.AddTransient<IViewRenderService, RenderViewToString>();
#endregion
#endregion
var app = builder.Build();
if (!app.Environment.IsDevelopment())
{
app.UseExceptionHandler("/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.MapRazorPages();
app.UseStaticFiles();
app.UseAuthentication();
app.UseMvcWithDefaultRoute();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
app.MapControllerRoute(
name: "areas",
pattern: "{area:exists}/{controller=Home}/{action=Index}");
سلام وقت بخیر
لطفا ترتیب middleware هارو به شکل زیر باشه
app.UseAuthentication();
app.UseAuthorization();