Asp.net core Azure AD登录有限循环

Asp.net core Azure AD登录有限循环,asp.net-core,azure-active-directory,Asp.net Core,Azure Active Directory,我的代码进入无限循环,点击azure登录页面(由Microsoft托管),然后重定向回我的应用程序,然后返回ms主机登录页面等 在我的代码中,OnAuthorizationCodeReceived事件中有一个断点 public void ConfigureAzureAd(IServiceCollection services) { //set authentication to use Azure AD services.AddAuthentica

我的代码进入无限循环,点击azure登录页面(由Microsoft托管),然后重定向回我的应用程序,然后返回ms主机登录页面等

在我的代码中,
OnAuthorizationCodeReceived
事件中有一个断点

    public void ConfigureAzureAd(IServiceCollection services)
    {
        //set authentication to use Azure AD
        services.AddAuthentication(auth =>
        {                
            auth.DefaultAuthenticateScheme = CookieAuthenticationDefaults.AuthenticationScheme;
            auth.DefaultChallengeScheme = OpenIdConnectDefaults.AuthenticationScheme;
            auth.DefaultSignInScheme = CookieAuthenticationDefaults.AuthenticationScheme;
        })
        .AddCookie()
        .AddOpenIdConnect(opts =>
        {

            Configuration.GetSection("OpenIdConnect").Bind(opts);

            opts.Events = new OpenIdConnectEvents
            {
                OnAuthorizationCodeReceived = async ctx =>
                {
                    HttpRequest request = ctx.HttpContext.Request;
                    //We need to also specify the redirect URL used
                    string currentUri = UriHelper.BuildAbsolute(request.Scheme, request.Host, request.PathBase, request.Path);
                    //Credentials for app itself
                    var credential = new ClientCredential(ctx.Options.ClientId, ctx.Options.ClientSecret);

                    //Construct token cache
                    ITokenCacheFactory cacheFactory = ctx.HttpContext.RequestServices.GetRequiredService<ITokenCacheFactory>();
                    TokenCache cache = cacheFactory.CreateForUser(ctx.Principal);

                    var authContext = new AuthenticationContext(ctx.Options.Authority, cache);

                    //Get token for Microsoft Graph API using the authorization code
                    string resource = "https://graph.microsoft.com";
                    AuthenticationResult result = await authContext.AcquireTokenByAuthorizationCodeAsync(
                        ctx.ProtocolMessage.Code, new Uri(currentUri), credential, resource);

                    //Tell the OIDC middleware we got the tokens, it doesn't need to do anything
                    ctx.HandleCodeRedemption(result.AccessToken, result.IdToken);
                    //ctx.HandleCodeRedemption();
                }
            };
        });
    }

您需要在代码和Azure AD中的应用程序注册中设置回复URL。您应该将回复URL设置为您希望用户重定向到的任何位置(通常与您发布的主页URL类似)

作为参考,您可以在Github示例中看到示例。
回答我自己的问题

我认为我的问题与功能文件夹有关。我正在实现自定义路由以启用功能文件夹,但Azure AD代码将其自己的自定义路由设置为“/登录oidc”。我是通过使用Visual Studio使用Azure Active Directory向导创建一个新项目得出这一结论的,并获得了测试项目的登录,但当我将旧代码移植到新的测试项目时,我得到了完全相同的错误,但在新的“Visual Studio向导”中几乎没有配置,它与我的Azure广告交互,注册了应用程序,并添加了所有必需的配置,因此我知道在添加功能文件夹之前它会这样做,并且在功能文件夹之后产生完全相同的错误行为,因此得出结论,这与功能文件夹自定义路由有关


如果有人感兴趣,我找到的帮助实现功能文件夹的代码的Url:

您的登录过程是如何启动的?如果您没有在质询结果中指定重定向URL,则您将获得此行为。关于设置Azure AD的身份验证,Microsoft文档中还有一个很好的解释:
"OpenIdConnect": {
    "ClientId": "<guid in here>", // Application ID
    "ClientSecret": "<secrect from portal.azure.com>",
    "Authority":     "https://login.microsoftonline.com/emailwithout@symbol.onmicrosoft.com/",
    "PostLogoutRedirectUri": "http://www.<projectname_in_here>.local",
    "CallbackPath": "/signin-oidc",
    "ResponseType": "code id_token"
}