.NET Core 2.1:未经授权访问时如何在登录页面重定向

时间:2018-12-26 00:33:37

标签: c# asp.net-core asp.net-core-2.1

当我尝试访问授权页面时,我在.Net Core 2.1中工作但未登录,它重定向到 “ https://localhost:44361/Identity/Account/Login?ReturnUrl=%2FHome” 但是我想重定向到我的登录页面“ https://localhost:44361/Account/Index”。

请问有人可以指导我如何做吗?

这是StartUp.cs

公共类启动     {

    public Startup(IConfiguration configuration)
    {
        Configuration = configuration;
    }

    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("DefaultConnection")));
        //services.AddDefaultIdentity<IdentityUser>().AddEntityFrameworkStores<ApplicationDbContext>();
        services.AddIdentity<ApplicationUser, ApplicationRole>(options => options.Stores.MaxLengthForKeys = 128)
             .AddEntityFrameworkStores<ApplicationDbContext>()
             .AddDefaultUI()
             .AddDefaultTokenProviders();

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

        services.ConfigureApplicationCookie(options => options.LoginPath = "/Account/Index");

    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Account}/{action=Index}/{id?}");
        });
    }
}

0 个答案:

没有答案