我正在使用Razor页面的ASP.Net Core 2.1站点上工作,这是我第一次使用Razor页面。但是我想做的是更改主页或登录页面。因此,如果用户未登录,则该站点应重定向到Areas文件夹中的/ Account / Login页面,但是如果用户已登录,则应转到一个名为DataManagement的页面,如下页面文件夹中所示。
我已经缝制了Identity,并在Configure Services中尝试了以下类似操作:
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
然后在configure方法中:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
无济于事。
编辑 我的StartUp.cs
公共类启动 { 公共启动(IConfiguration配置) { 配置=配置; }
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.Configure<CookiePolicyOptions>(options =>
{
// This lambda determines whether user consent for non-essential cookies is needed for a given request.
options.CheckConsentNeeded = context => true;
options.MinimumSameSitePolicy = SameSiteMode.None;
});
services.AddDbContext<ApplicationDbContext>(options =>
options.UseSqlServer(
Configuration.GetConnectionString("Connection")));
services.AddIdentity<ApplicationUser, ApplicationRole>(
options => options.Stores.MaxLengthForKeys = 128)
.AddEntityFrameworkStores<ApplicationDbContext>()
.AddDefaultUI()
.AddDefaultTokenProviders()
.AddEntityFrameworkStores<ApplicationDbContext>();
services.AddMvc().AddRazorPagesOptions(opts =>
{
opts.Conventions.AddPageRoute("/DataManagement", "/");
opts.Conventions.AddPageRoute("/DataManagement", "home");
opts.Conventions.AddPageRoute("/DataManagement", "index");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context, RoleManager<ApplicationRole> roleManager, UserManager<ApplicationUser> userManager, IServiceProvider serviceProvider)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseAuthentication();
app.UseMvc();
}
答案 0 :(得分:2)
最简单的方法是在[Authorize]
文件上使用DataManagement.cshtml.cs
属性。
[Authorize]
public class DataManagementModel : PageModel
{
public void OnGet()
{
}
}
只需像往常一样在Startup.cs
中配置默认主页:
services.AddMvc().AddRazorPagesOptions(opts =>
{
opts.Conventions.AddPageRoute("/DataManagement", "/");
opts.Conventions.AddPageRoute("/DataManagement", "home");
opts.Conventions.AddPageRoute("/DataManagement", "index");
opts.Conventions.AddAreaPageRoute("Account", "/Login", "/Account/Login");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
services.ConfigureApplicationCookie(options =>
{
options.LoginPath = $"/Account/Login";
});
并在“配置”中:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
然后删除Index.cshtml
在这种情况下,我认为您需要定义自定义登录名,取自here