c# - Azure Active Directory 不会使用 ASP.NET Core 2.1 MVC 注销

标签 c# .net .net-core asp.net-core-mvc azure-active-directory

我有一个 ASP.NET Core 2.1 MVC 应用程序,我正在尝试使用 Azure AD 进行身份验证。该应用程序重定向到 Microsoft 登录页面,但当我注销然后返回到该应用程序的主页时,它会自动重新登录。

我试过调用 https://login.microsoftonline.com/common/oauth2/v2.0/logout并清除 cookie 但无济于事。

public void ConfigureServices(IServiceCollection services)
{
    services.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

    services.AddAuthentication(AzureADDefaults.AuthenticationScheme)
            .AddAzureAD(options => Configuration.Bind("AzureAd", options));

    services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
        {
            options.Authority = options.Authority + "/v2.0/";
            options.TokenValidationParameters.ValidateIssuer = false;
            options.Events.OnRedirectToIdentityProviderForSignOut = async context =>
            {
                var h = new HttpClient();
                var r = await h.GetAsync($"https://login.microsoftonline.com/common/oauth2/v2.0/logout?post_logout_redirect_uri=https%3A%2F%2Flocalhost%2%3A5001%2F");

                foreach (var cookie in context.Request.Cookies.Keys)
                {
                    context.Response.Cookies.Delete(cookie);
                }
            };

            options.Events.OnTokenResponseReceived = async conext =>
            {
                var t = 1;
            };
        });

    services.AddMvc(options =>
        {
            var policy = new AuthorizationPolicyBuilder()
                .RequireAuthenticatedUser()
                .Build();
            options.Filters.Add(new AuthorizeFilter(policy));
        })
        .SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
}

public async Task<IActionResult> Logout()
{
    var result =  SignOut("AzureAD", "AzureADOpenID", "AzureADCookie");
    return result;
}

最佳答案

请检查我将 Azure AD 身份验证添加到 ASP.NET Core 2.1 MVC 应用程序的方法。该工具将为您添加身份验证代码。您需要做的是将您的登录/退出按钮绑定(bind)到该方法。

1.单击连接服务->选择使用 Azure Active Directory 进行身份验证。

enter image description here

2.您需要提供一个登录按钮来触发登录页面。

enter image description here

3.为域输入您的租户名称并选择提供应用程序设置的方式。

enter image description here

4.点击finish按钮完成配置。

5.删除Startup.cs中的app.UseBrowserLink()

6.调用AccountController.cs中的SignOut()方法来注销用户。它运行良好。

[HttpGet]
        public IActionResult SignOut()
        {
            var callbackUrl = Url.Action(nameof(SignedOut), "Account", values: null, protocol: Request.Scheme);
            return SignOut(
                new AuthenticationProperties { RedirectUri = callbackUrl },
                CookieAuthenticationDefaults.AuthenticationScheme,
                OpenIdConnectDefaults.AuthenticationScheme);
        }

关于c# - Azure Active Directory 不会使用 ASP.NET Core 2.1 MVC 注销,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55537543/

相关文章:

c# - 从自己的后台线程更新绑定(bind)控件

.net-core - .net核心版本生成本地化文件夹

c# - 如何提取 FAT 磁盘镜像?

.net - 定期部署 .NET/WPF 应用程序进行内部测试

c# - 刷新两次picturebox显示错误图像

c# - 在数据流网络中使用 BufferBlock<T> 的好处

c# - dotnetcore 控制台上的 Microsoft.Extensions.Logging 和 Serilog。在 RollingFile 接收器中包含范围信息

logging - 网核 : manually create a Logger without using LoggerFactory

c# - NHibernate - 抛出 Sytem.OutOfMemoryException

c# - Javascript 和回发问题