ASP.NET核心标识区域

时间:2020-05-09 19:07:41

标签: asp.net asp.net-mvc asp.net-core

我有一个有趣的问题,Microsoft的文档似乎并未涵盖。我正在创建一个ASP.NET Core 3.1项目并通过该应用程序使用授权,因此您必须具有查看该页面或某些页面的权限,您只需要登录到应用程序即可访问该页面。如果您已登录,则一切正常。

但是,在未登录用户的身份区域中引入的身份区域将用户重定向到:(如果我手动添加/ Identity /,则其行为正常。

“ /登录/ returnurl =某物?某物”

代替

“ / Identity / Login / returnUrl =某物?某物

这是启动文件:

    public void ConfigureServices(IServiceCollection services)
    {

        services.AddMvc(options => options.EnableEndpointRouting = false)
            .SetCompatibilityVersion(CompatibilityVersion.Version_3_0);

        services.ConfigureApplicationCookie(options =>
        {
            options.LoginPath = $"/Identity/Account/Login";
            options.LogoutPath = $"/Identity/Account/Logout";
            options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddIdentity<IdentityUser, IdentityRole>(options => options.SignIn.RequireConfirmedAccount = true)
            .AddDefaultTokenProviders()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.Configure<DataProtectionTokenProviderOptions>(o =>
            o.TokenLifespan = TimeSpan.FromHours(3));

        services.AddTransient<IEmailSender, EmailSender>();
        services.AddScoped<IUnitOfWork, UnitOfWork>();
        services.Configure<AuthMessageSenderOptions>(Configuration);

        services.AddSession(options =>
        {
            options.IdleTimeout = TimeSpan.FromMinutes(10);
            options.Cookie.HttpOnly = true;
            options.Cookie.IsEssential = true; 
        });

        //These keys need to be setup on Azure or where you are running it to make it work.
        //services.AddAuthentication()
        //.AddFacebook(facebookOptions =>
        //{
        //    facebookOptions.AppId = Configuration["Authentication:Facebook:AppId"];
        //    facebookOptions.AppSecret = Configuration["Authentication:Facebook:AppSecret"];
        //})
        //.AddGoogle(options =>
        //{
        //    IConfigurationSection googleAuthNSection =
        //        Configuration.GetSection("Authentication:Google");

        //    options.ClientId = googleAuthNSection["ClientId"];
        //    options.ClientSecret = googleAuthNSection["ClientSecret"];
        //});
        services.AddRazorPages().AddRazorRuntimeCompilation();
        services.AddControllersWithViews().AddRazorRuntimeCompilation();
    }

    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseSession();

        app.UseRouting();

        app.UseAuthentication();
        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapRazorPages();
        });

        app.UseMvc();
    }

1 个答案:

答案 0 :(得分:0)

我刚刚解决了将这些行添加到ConfigureServices中的相同问题。

services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = $"/Identity/Account/Login";
    options.LogoutPath = $"/Identity/Account/Logout";
    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});