C# 在首次使用ASP.NET Core 3.0加载之前,通过授权服务器保护SPA的正确方法是什么?

C# 在首次使用ASP.NET Core 3.0加载之前,通过授权服务器保护SPA的正确方法是什么?,c#,angular,asp.net-core,.net-core,C#,Angular,Asp.net Core,.net Core,我正在使用IDE(Visual Studio 2019)为dotnet core 3.0中的angular v8.0 SPA应用程序使用“新”项目模板 我试图做的是在应用程序首次加载之前保护SPA本身。这意味着:当我打开SPA时,例如,我希望立即被重定向到授权服务器,而不是单击某个按钮进行身份验证 见项目结构: 我已经尝试过的: //Added this to redirect to Identity Server auth prior to loading SPA app.Use(a

我正在使用IDE(Visual Studio 2019)为dotnet core 3.0中的angular v8.0 SPA应用程序使用“新”项目模板

我试图做的是在应用程序首次加载之前保护SPA本身。这意味着:当我打开SPA时,例如,我希望立即被重定向到授权服务器,而不是单击某个按钮进行身份验证

见项目结构:

我已经尝试过的:

//Added this to redirect to Identity Server auth prior to loading SPA    
app.Use(async (context, next) =>
{
    if (!context.User.Identity.IsAuthenticated)
    {
        await context.ChallengeAsync("Identity.Application");
    }
    else
    {
        await next();
    }
});
上面这行是我在app.UseSpa之前添加的

My Startup.cs:

public class Startup
{
    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.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("DefaultConnection")));

        services.AddDefaultIdentity<ApplicationUser>()
            .AddEntityFrameworkStores<ApplicationDbContext>();

        services.AddIdentityServer()
            .AddApiAuthorization<ApplicationUser, ApplicationDbContext>();

        services.AddAuthentication()
            .AddIdentityServerJwt();
        services.AddControllersWithViews();
        services.AddRazorPages();
        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist";
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        if (!env.IsDevelopment())
        {
            app.UseSpaStaticFiles();
        }

        app.UseRouting();

        app.UseAuthentication();
        app.UseIdentityServer();
        app.UseAuthorization();
        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action=Index}/{id?}");
            endpoints.MapRazorPages();
        });

        app.Use(async (context, next) =>
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                await context.ChallengeAsync("Identity.Application");
            }
            else
            {
                await next();
            }
        });

        app.UseSpa(spa =>
        {
            // To learn more about options for serving an Angular SPA from ASP.NET Core,
            // see https://go.microsoft.com/fwlink/?linkid=864501

            spa.Options.SourcePath = "ClientApp";

            if (env.IsDevelopment())
            {
                spa.UseAngularCliServer(npmScript: "start");
            }
        });
    }
}
公共类启动
{
公共启动(IConfiguration配置)
{
配置=配置;
}
公共IConfiguration配置{get;}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
services.AddDbContext,但我无法路由到我的SPA路由和页面。当我单击XYZ上的锚链接时,理想情况下,这是主组件上的路由,或者如果我强制从url路由计数器组件,则会显示下面相同的登录页面。请帮助我了解我做错了什么,以及授权服务器保护SPA的正确方法是什么第一批矿石

输出


您使用cookie身份验证,如果您未经身份验证,您的应用程序将使用该代码将您重定向到openid登录页面

    app.Use(async (context, next) =>
    {
        if (!context.User.Identity.IsAuthenticated)
        {
            await context.ChallengeAsync("Identity.Application");
        }
        else
        {
            await next();
        }
    });

您使用cookie身份验证,如果您未经身份验证,您的应用程序将使用该代码将您重定向到openid登录页面

    app.Use(async (context, next) =>
    {
        if (!context.User.Identity.IsAuthenticated)
        {
            await context.ChallengeAsync("Identity.Application");
        }
        else
        {
            await next();
        }
    });

如果我答对了你的问题,你有一个SPA,用户要查看索引页,他们必须获得授权。现在你已经实现了这一点,但在单击“登录”按钮后,页面仍会加载验证页。1.你是否从服务器接收令牌?2.你是否将令牌保存到区域设置?3.你的应用程序是否识别令牌?当我“登录”按钮单击后,会显示“验证我自己”和“我的电子邮件”,但当我将自己路由到SPA组件时,它不允许我在登录页面上按原样进行身份验证,但我仍然成功地进行了身份验证。如果我问对了问题,您有一个SPA,用户要查看索引页面,必须对其进行授权。现在您已经实现了。”但在单击登录按钮后,页面仍会加载身份验证页面。1.您是否从服务器收到令牌?2.您是否将令牌保存到区域设置?3.您的应用程序是否识别令牌?当登录按钮单击时,会显示“验证我的身份”和“我的电子邮件”,但当我将我的身份路由到SPA组件时,它不允许我和我的电子邮件在登录页面上显示我的身份,但我仍然成功通过身份验证。如果我没有看到,但他正在使用AddIdentityServerJwt(),请更正我;您在哪里看到Cookie身份验证?如果我没有看到,但他正在使用AddIdentityServerJwt(),请更正我;您在哪里看到Cookie身份验证?