Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angular/32.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angular Azure B2C+;角度+;Azure中托管的ASP.NET Core 2.2重新身份验证超时问题_Angular_Azure_Asp.net Core_Single Page Application_Azure Ad B2c - Fatal编程技术网

Angular Azure B2C+;角度+;Azure中托管的ASP.NET Core 2.2重新身份验证超时问题

Angular Azure B2C+;角度+;Azure中托管的ASP.NET Core 2.2重新身份验证超时问题,angular,azure,asp.net-core,single-page-application,azure-ad-b2c,Angular,Azure,Asp.net Core,Single Page Application,Azure Ad B2c,我们在前端使用一堆Angular,在后端使用ASP.NET Core+Azure B2C进行身份验证。应用程序托管在Azure上的应用程序服务中 我注意到,有时我会启动网站,但它一直处于空闲状态(比如说一夜之间),当我尝试单击另一个链接以收回数据时,B2C看起来会尝试重定向到收取索赔/重新验证。如果我做了一个硬刷新,我注意到它会跳转到B2C URL,然后正确重定向回我的应用程序 我们只是在控制器上设置了[Authorize]属性,以确保它们得到授权 有没有什么好的处理方法?下面是一些关于我们如何

我们在前端使用一堆Angular,在后端使用ASP.NET Core+Azure B2C进行身份验证。应用程序托管在Azure上的应用程序服务中

我注意到,有时我会启动网站,但它一直处于空闲状态(比如说一夜之间),当我尝试单击另一个链接以收回数据时,B2C看起来会尝试重定向到收取索赔/重新验证。如果我做了一个硬刷新,我注意到它会跳转到B2C URL,然后正确重定向回我的应用程序

我们只是在控制器上设置了
[Authorize]
属性,以确保它们得到授权

有没有什么好的处理方法?下面是一些关于我们如何做到这一点的关键代码区域

这更像是CORS的问题吗?此外,在该设置中是否有延长身份验证“超时”的方法

错误:

代码:

公共类启动
{
公共IConfiguration配置{get;}
公共启动(IConfiguration配置)
{
配置=配置;
}
//此方法由运行时调用。请使用此方法将服务添加到容器中。
public void配置服务(IServiceCollection服务)
{
配置(选项=>
{
options.checkApprovementRequired=\u=>true;
options.MinimumSameSitePolicy=SameSiteMode.None;
});
services.AddAuthentication(sharedOptions=>
{
sharedOptions.DefaultScheme=CookieAuthenticationDefaults.AuthenticationScheme;
sharedOptions.DefaultChallengeScheme=OpenIdConnectDefaults.AuthenticationScheme;
})
.AddAzureAdB2C(选项=>Configuration.Bind(“AzureAdB2C”,选项))
.AddCookie(x=>
{
x、 Cookie.Name=UIConstants.WebSessionCookieName;
});
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
.AddJsonOptions(选项=>
{
options.SerializerSettings.ReferenceLoopHandling=ReferenceLoopHandling.Ignore;
});
services.AddSpaStaticFiles(c=>
{
c、 RootPath=“ClientApp/dist”;
});
}
//此方法由运行时调用。请使用此方法配置HTTP请求管道。
公共无效配置(IApplicationBuilder应用程序,IHostingEnvironment环境)
{
if(env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
其他的
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseCookiePolicy();
app.UseSpaStaticFiles();
app.UseAuthentication();
应用程序使用(异步(上下文,下一步)=>
{
如果(!context.User.Identity.IsAuthenticated)
{
wait context.ChallengeAsync(
OpenIdConnectDefaults.AuthenticationScheme,
新的身份验证属性{RedirectUri=“/auth/signin”});
返回;
}
等待next.Invoke();
});
app.UseMvcWithDefaultRoute();
app.UseSpa(spa=>
{
//要了解有关从ASP.NET Core提供角度SPA的更多选项,请参阅https://go.microsoft.com/fwlink/?linkid=864501
spa.Options.SourcePath=“ClientApp”;
});
}
}

问题似乎在于您正在使用cookie身份验证。前端AJAX调用被重定向到B2C。您可以尝试在前端检测这种情况,然后重定向用户,也可以切换到使用JWT承载令牌身份验证+MSAL.js。谢谢!你有没有想过如何检测这个前端的角度和重定向?我不想切换到JwT或者其他类似的东西。那么我如何可靠地扩展cookie呢?我不确定这是否可行,但如果您可以在重定向之后禁用AJAX,那么您就可以在代码中处理302响应,并检测到它正试图将浏览器重定向到Azure AD。
public class Startup
{
    public IConfiguration Configuration { get; }

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

    // 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 =>
        {
            options.CheckConsentNeeded = _ => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddAuthentication(sharedOptions =>
        {
            sharedOptions.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            sharedOptions.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
        })
            .AddAzureAdB2C(options => Configuration.Bind("AzureAdB2C", options))
            .AddCookie(x =>
            {
                x.Cookie.Name = UIConstants.WebSessionCookieName;
            });

        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_2)
            .AddJsonOptions(options =>
            {
                options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
            });

        services.AddSpaStaticFiles(c =>
        {
            c.RootPath = "ClientApp/dist";
        });
    }

    // 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();
        }
        else
        {
            app.UseHsts();
        }

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

        app.Use(async (context, next) =>
        {
            if (!context.User.Identity.IsAuthenticated)
            {
                await context.ChallengeAsync(
                    OpenIdConnectDefaults.AuthenticationScheme,
                    new AuthenticationProperties { RedirectUri = "/auth/signin" });

                return;
            }

            await next.Invoke();
        });

        app.UseMvcWithDefaultRoute();
        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";


            });
    }
}