ASP.NET核心身份区域

问题描述 投票:0回答:1

我有一个有趣的问题,微软的文档似乎没有涉及到这个问题。我正在创建一个ASP.NET Core 3.1项目,并通过应用程序使用授权,所以你必须有权限才能查看页面,或者有些页面你只需要登录到应用程序中就可以访问页面。如果你登录了,那都是可以正常工作的。

然而,如果用户没有登录,他们引入的身份识别区域会将用户重定向到。(如果我手动添加个人信息,它就会正常运行.

"Loginreturnurl=something?something"

而不是

"IdentityLoginreturnUrl=something?

这是启动文件。

    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();
    }
asp.net asp.net-mvc asp.net-core
1个回答
0
投票

我刚刚解决了同样的问题,在ConfigureServices中添加了这几行。

services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = $"/Identity/Account/Login";
    options.LogoutPath = $"/Identity/Account/Logout";
    options.AccessDeniedPath = $"/Identity/Account/AccessDenied";
});
© www.soinside.com 2019 - 2024. All rights reserved.