ASP.Net Core MVC 与 Microsoft 的身份验证

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

我正在.Net 8中使用ASP.Net Core MVC应用程序。我已经配置了Microsoft登录,但是当我登录时,它会使用之前登录的帐户自动登录。相反,我想启用在登录前选择帐户的选项。如何做到这一点?

请在下面找到我的配置。

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = MicrosoftAccountDefaults.AuthenticationScheme;
}).AddCookie().AddMicrosoftAccount(MicrosoftAccountDefaults.AuthenticationScheme, options =>
{
    options.ClientId = builder.Configuration.GetSection("MicrosoftKeys:ClientId").Value;
    options.ClientSecret = builder.Configuration.GetSection("MicrosoftKeys:ClientSecret").Value;
});

我想这样展示 enter image description here

asp.net-core-mvc .net-8.0
1个回答
0
投票

您需要设置事件来强制用户在登录前选择帐户。

builder.Services.AddAuthentication(options =>
{
    options.DefaultScheme = CookieAuthenticationDefaults.AuthenticationScheme;
    options.DefaultChallengeScheme = MicrosoftAccountDefaults.AuthenticationScheme;
})
.AddCookie()
.AddMicrosoftAccount(microsoftOptions =>
{
    microsoftOptions.ClientId = builder.Configuration["MicrosoftKeys:ClientId"];
    microsoftOptions.ClientSecret = builder.Configuration["MicrosoftKeys:ClientSecret"];

    microsoftOptions.Events = new OAuthEvents
    {
        OnRedirectToAuthorizationEndpoint = context =>
        {
            // Force the user to select an account on the Microsoft login page
            context.Response.Redirect(context.RedirectUri + "&prompt=select_account");
            return Task.CompletedTask;
        }
    };
});

© www.soinside.com 2019 - 2024. All rights reserved.