System.InvalidOperationException: ‘Scheme already exists: Cookies’

huangapple go评论78阅读模式
英文:

System.InvalidOperationException: 'Scheme already exists: Cookies'

问题

I have Razor Pages in ASP.NET Core web application in .NET 6. When I run the app.Run() at program.cs, I got this error message:

System.InvalidOperationException: 'Scheme already exists: Cookies'

I think this is the code that causes this issue. How should I reconfigure this?

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
    .AddCookie(options =>
    {
        options.ExpireTimeSpan = TimeSpan.FromSeconds(1);
    })
    .AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.GetSection("AzureAd").Bind(options);

        options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
        options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
    })
    .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
    .AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
    .AddInMemoryTokenCaches();

builder.Services.AddRazorPages()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AuthorizeFolder("/");
    })
    .AddMicrosoftIdentityUI();

And this is the code for logout:

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);

Response.Cookies.Delete(".AspNetCore.Cookies");
英文:

I have Razor Pages in ASP.NET Core web application in NET 6. When I run the app.Run() at program.cs, I got this error message:

System.InvalidOperationException: 'Scheme already exists: Cookies'

I think this is the code that cause this issue. How should I reconfigure this?

builder.Services.AddAuthentication(CookieAuthenticationDefaults.AuthenticationScheme)
.AddCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromSeconds(1);
})
.AddMicrosoftIdentityWebApp(options =>
{
    builder.Configuration.GetSection("AzureAd").Bind(options);

    options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
    options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
})
.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();


builder.Services.AddRazorPages()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AuthorizeFolder("/");
    })
    .AddMicrosoftIdentityUI();

and this is the code for logout:

await HttpContext.SignOutAsync(CookieAuthenticationDefaults.AuthenticationScheme);
await HttpContext.SignOutAsync(OpenIdConnectDefaults.AuthenticationScheme);
    
Response.Cookies.Delete(".AspNetCore.Cookies");

答案1

得分: 1

从错误消息中,您需要进行如下操作,使用两种不同的身份验证方案,如下所示:

builder.Services.AddAuthentication(options => {
    options.DefaultScheme = "Cookies";
    options.DefaultChallengeScheme = "MyAzureAdScheme";
})
.AddCookie(options =>
{
    options.ExpireTimeSpan = TimeSpan.FromSeconds(1);
  
}).AddMicrosoftIdentityWebApp(options =>
{
    builder.Configuration.GetSection("AzureAd").Bind(options);
    options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
    options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
}, null, "MyAzureAdScheme", null)   

.EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
.AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
.AddInMemoryTokenCaches();
英文:

From the error message, what you need do is to have two different authentication schemes like below:

builder.Services.AddAuthentication(options => {
        options.DefaultScheme = "Cookies";
        options.DefaultChallengeScheme = "MyAzureAdScheme";
    })
    .AddCookie(options =>
    {
        options.ExpireTimeSpan = TimeSpan.FromSeconds(1);
  
    }).AddMicrosoftIdentityWebApp(options =>
    {
        builder.Configuration.GetSection("AzureAd").Bind(options);     
        options.NonceCookie.SecurePolicy = CookieSecurePolicy.Always;
        options.CorrelationCookie.SecurePolicy = CookieSecurePolicy.Always;
    },null, "MyAzureAdScheme", null)   

    .EnableTokenAcquisitionToCallDownstreamApi(initialScopes)
    .AddMicrosoftGraph(builder.Configuration.GetSection("DownstreamApi"))
    .AddInMemoryTokenCaches();

huangapple
  • 本文由 发表于 2023年2月27日 12:58:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/75576888.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定