简体   繁体   English

.NET 6 web 应用程序使用 Microsoft 标识(又名:Azure Active Directory)登录错误

[英]SIgn-in error in .NET 6 web application using Microsoft Identity (aka: Azure Active Directory)

I'm trying to use Microsoft Identity (formerly: Azure AD) authentication in an ASP.NET web application running on .NET 6我正在尝试在 ASP.NET web 应用程序中使用 Microsoft Identity(以前:Azure AD)身份验证

I've used this code to configure authentication in my startup class ConfigureServices method:我在启动 class ConfigureServices方法中使用此代码配置身份验证:

    services.AddAuthentication(OpenIdConnectDefaults.AuthenticationScheme)
        .AddMicrosoftIdentityWebApp(options => {
            options.Instance = appSettings.UserSettings.AzIdInstance;
            options.Domain = appSettings.UserSettings.AzIdDomain;
            options.TenantId = appSettings.UserSettings.AzIdTenantId;
            options.ClientId = appSettings.UserSettings.AzIdClientId;
            options.CallbackPath = appSettings.UserSettings.AzIdCallbackPath;
            options.SignedOutCallbackPath = appSettings.UserSettings.AzIdSignOutCallbackPath;
        });
    services.AddAuthorization();

Then in the Configure method, I've added:然后在Configure方法中,我添加了:

app.UseAuthentication();
app.UseAuthorization();

When I try to access a controller action protected by the [Authorize] attribute, it correctly redirects me to the microsoft login page, however after I log in when the app then tries to redirect to my callback path ( /signin-oidc ) the connection gets reset and I get this browser error:当我尝试访问受[Authorize]属性保护的 controller 操作时,它会正确地将我重定向到 Microsoft 登录页面,但是在我登录后,当应用程序尝试重定向到我的回调路径 ( /signin-oidc ) 时,连接被重置,我得到这个浏览器错误:

在此处输入图像描述

What am I doing wrong here?我在这里做错了什么? Is there a good example online on how to properly configure this?网上有没有关于如何正确配置这个的好例子?

Apparently, this error was happening because I had set the cookie's SameSite attribute to None with this code (I need it to be None for some cross-domain calls that are done to the server):显然,发生此错误是因为我已使用此代码将 cookie 的SameSite属性设置为None (对于对服务器执行的一些跨域调用,我需要它为None ):

services.Configure<CookieAuthenticationOptions>(CookieAuthenticationDefaults.AuthenticationScheme, options =>
{
    //In-memory cookie store, so sessions are forcibly killed when the webapp restarts
    //This ensures that the user's Claims are up to date - remember to restart the service when Azure AD security group memberships are changed!
    options.SessionStore = services.BuildServiceProvider().GetService<MemoryCacheTicketStore>();

    //We want to disable the same-site policy, otherwise cross-domain API calls from JS won't work
    options.Cookie.SameSite = SameSiteMode.None;
});

apparently this by itself causes the error.显然这本身会导致错误。 To fix it, I had to also add the following code:要修复它,我还必须添加以下代码:

services.Configure<CookiePolicyOptions>(options =>
            {
                options.CheckConsentNeeded = context => true;
                options.HandleSameSiteCookieCompatibility(s => false);
                options.MinimumSameSitePolicy = SameSiteMode.None;
            });

this seems to have fixed the problem.这似乎解决了这个问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 由于Microsoft.AspNet.Identity.Application错误而无法登录 - Unable to sign-in because of Microsoft.AspNet.Identity.Application error 无法使用 Authentication=Active Directory Managed Identity 从 do.net web 应用程序连接到 Azure SQL MI - Unable to connect to Azure SQL MI from dotnet web application using Authentication=Active Directory Managed Identity Azure Active Directory使用Microsoft帐户登录 - Azure Active Directory sign in with Microsoft Account Asp.net Identity使用密码和Azure Active Directory身份验证 - Asp.net Identity using password and Azure Active Directory authentication 在 Azure Active Directory B2C 中使用 OpenID Connect 进行 Web 登录会提供 id_token 而不是 access_token - Web sign-in with OpenID Connect in Azure Active Directory B2C gives id_token instead of access_token Azure Active Directory -.Net Core 3 Web 应用程序 - 禁用 2 因素身份验证 - Azure Active Directory - .Net Core 3 Web Application - Disable 2 Factor Authentication 与Active Directory集成的.NET应用程序的单点登录 - Single sign on for .NET application integrated with Active Directory 当在 Blazor 应用程序中使用 Azure Active Directory 时如何在 asp net core 3.1 中自定义注销页面 - How to customize sign out page in asp net core 3.1 when Azure Active Directory used in a Blazor application 如何从 asp.net web api 中的身份声明获取 JWT 身份验证的登录用户 - How to get JWT authenticated sign-in user from identity claims in asp.net web api 如何为我的 React/.NET Core 3.0 SPA web 应用程序添加 Microsoft Identity/Azure AD 登录功能 - How to add Microsoft Identity/Azure AD login feature for my React/.NET Core 3.0 SPA web application
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM