我是asp.net核心的新手,我的网站有问题。 我搜索的次数越来越多,但徒劳无功。
我的应用程序使用asp.net core 2. *,它由两个区域组成:管理员和用户
我为每个区域创建了一个登录页面,下面是startup.cs文件代码中的身份验证。
该应用程序已成功运行,但是我的问题是,当浏览到经过身份验证的页面然后在浏览器中单击时, 该网站将转到登录页面。当我重新加载此页面时,网站将加载经过身份验证的页面(我要通过“后退”按钮返回的必需页面)。
这意味着(User.Identity.IsAuthenticted
变量在浏览器转到上一页时为false),但是当浏览器再次向该页面发出新请求时,此变量将等于true并正确呈现页面。
我了解问题所在,但无法解决。请任何人可以帮助我并节省时间吗?
public void ConfigureServices(IServiceCollection services)
{
string UserScheme = "UserScheme";
services.AddDbContext<FastDoEntities>(options =>
options.UseSqlServer(Configuration.GetConnectionString("FastDoEntities")));
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(Configuration.GetConnectionString("FastDoEntities")));
services.AddIdentity<ApplicationUser, IdentityRole>()
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultTokenProviders();
// Add application services.
services.AddTransient<IEmailSender, EmailSender>();
services.AddMvc();
services.AddAuthorization(autorizationOptions =>
{
autorizationOptions.AddPolicy("UserPolicy", policy =>
{
policy.AuthenticationSchemes.Add(UserScheme);
policy.RequireAuthenticatedUser();
policy.RequireRole("User");
}).
autorizationOptions.AddPolicy("AdminPolicy", policy =>
{
policy.AuthenticationSchemes.Add(AdminScheme);
policy.RequireAuthenticatedUser();
policy.RequireRole("Admin");
});
});
services.AddAuthentication()
.AddCookie("UserScheme", CookieBuilder =>
{
CookieBuilder.Cookie.Path = "/";
CookieBuilder.LoginPath = "/User/Login";
CookieBuilder.AccessDeniedPath = "/User/AccessDenied";
CookieBuilder.Cookie.Name = "UserCookie";
})
.AddCookie("AdminScheme", CookieBuilder =>
{
CookieBuilder.Cookie.Path = "/";
CookieBuilder.LoginPath = "/Admin/Login";
CookieBuilder.AccessDeniedPath = "/Admin/AccessDenied";
CookieBuilder.Cookie.Name = "AdminCookie";
});
}