C# 使用Identity Server 4和Azure AD进行静默续订

C# 使用Identity Server 4和Azure AD进行静默续订,c#,asp.net-core,azure-active-directory,identityserver4,C#,Asp.net Core,Azure Active Directory,Identityserver4,我有一个identity server 4 web应用程序,它使用asp.net核心标识和azure ad对用户进行身份验证。我遇到的问题是,静默续费不适用于azure广告用户。如果您使用身份用户的用户名和密码登录,则可以正常工作 如果在客户端应用程序触发静默续订时使用Azure AD用户登录,我会在identity server日志记录中看到以下错误: 显示错误:prompt=未请求任何,但用户未经过身份验证 对于外部身份验证的处理,我使用的是外部控制器,可在此处的快速入门中找到: 我还从中

我有一个identity server 4 web应用程序,它使用asp.net核心标识和azure ad对用户进行身份验证。我遇到的问题是,静默续费不适用于azure广告用户。如果您使用身份用户的用户名和密码登录,则可以正常工作

如果在客户端应用程序触发静默续订时使用Azure AD用户登录,我会在identity server日志记录中看到以下错误:

显示错误:prompt=未请求任何,但用户未经过身份验证

对于外部身份验证的处理,我使用的是外部控制器,可在此处的快速入门中找到:

我还从中提取了一些示例代码和Microsoft.Identity.Web项目

在这一点上,我尝试了十几种不同的方法。下面是我的startup类的当前状态

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

            services.Configure<OpenIdConnectOptions>(AzureADDefaults.OpenIdScheme, options =>
            {
                options.SignInScheme = IdentityServerConstants.ExternalCookieAuthenticationScheme;
                options.ResponseType = OpenIdConnectResponseType.CodeIdToken;
                options.Authority = options.Authority + "/v2.0/"; // Microsoft identity platform

                options.TokenValidationParameters.ValidateIssuer = true;
                options.TokenValidationParameters.NameClaimType = "preferred_username";

                options.Scope.Add("email");
                options.Scope.Add("offline_access");
                options.Scope.Add("profile");
                options.Scope.Add("openid");

                options.Events.OnRedirectToIdentityProvider = context =>
                {
                    var login = context.Properties.GetParameter<string>(OpenIdConnectParameterNames.LoginHint);
                    if (!string.IsNullOrWhiteSpace(login))
                    {
                        context.ProtocolMessage.LoginHint = login;
                        context.ProtocolMessage.DomainHint = context.Properties.GetParameter<string>(
                            OpenIdConnectParameterNames.DomainHint);

                        // delete the login_hint and domainHint from the Properties when we are done otherwise
                        // it will take up extra space in the cookie.
                        context.Properties.Parameters.Remove(OpenIdConnectParameterNames.LoginHint);
                        context.Properties.Parameters.Remove(OpenIdConnectParameterNames.DomainHint);
                    }

                    // Additional claims
                    if (context.Properties.Items.ContainsKey("claims"))
                    {
                        context.ProtocolMessage.SetParameter("claims",
                            context.Properties.Items["claims"]);
                    }

                    return Task.FromResult(0);
                };

                var handler = options.Events.OnAuthorizationCodeReceived;
                options.Events.OnAuthorizationCodeReceived = async context =>
                {
                    var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisition>();
                    await tokenAcquisition.AddAccountToCacheFromAuthorizationCodeAsync(context, options.Scope).ConfigureAwait(false);
                    await handler(context).ConfigureAwait(false);
                };

                // Handling the sign-out: removing the account from MSAL.NET cache
                options.Events.OnRedirectToIdentityProviderForSignOut = async context =>
                {
                    // Remove the account from MSAL.NET token cache
                    var tokenAcquisition = context.HttpContext.RequestServices.GetRequiredService<ITokenAcquisition>();
                    await tokenAcquisition.RemoveAccountAsync(context).ConfigureAwait(false);
                };
            })
            .AddInMemoryTokenCaches();
services.AddAuthentication(IISDefaults.AuthenticationScheme)
.AddAzureAD(options=>Configuration.Bind(“AzureAd”,options));
配置(AzureADDefaults.OpenIdScheme,选项=>
{
options.signnscheme=IdentityServerConstants.ExternalCookieAuthenticationScheme;
options.ResponseType=OpenIdConnectResponseType.CodeIdToken;
options.Authority=options.Authority+“/v2.0/”;//Microsoft identity platform
options.TokenValidationParameters.ValidateIsuer=true;
options.TokenValidationParameters.NameClaimType=“首选\用户名”;
选项。范围。添加(“电子邮件”);
options.Scope.Add(“脱机访问”);
选项。范围。添加(“配置文件”);
options.Scope.Add(“openid”);
options.Events.OnRedirectToIdentityProvider=上下文=>
{
var login=context.Properties.GetParameter(OpenIdConnectParameterNames.LoginHint);
如果(!string.IsNullOrWhiteSpace(登录))
{
context.ProtocolMessage.LoginHint=login;
context.ProtocolMessage.DomainHint=context.Properties.GetParameter(
OpenIdConnectParameterNames.DomainHint);
//否则,请从属性中删除登录提示和域提示
//它将在cookie中占用额外的空间。
context.Properties.Parameters.Remove(OpenIdConnectParameterNames.LoginHint);
context.Properties.Parameters.Remove(OpenIdConnectParameterNames.DomainHint);
}
//附加索赔
if(context.Properties.Items.ContainsKey(“索赔”))
{
context.ProtocolMessage.SetParameter(“声明”,
context.Properties.Items[“索赔]);
}
返回Task.FromResult(0);
};
var handler=options.Events.OnAuthorizationCodeReceived;
options.Events.OnAuthorizationCodeReceived=异步上下文=>
{
var tokenAcquisition=context.HttpContext.RequestServices.GetRequiredService();
await tokenAcquisition.AddAccountToCacheFromAuthorizationCodeAsync(上下文,options.Scope)。ConfigureAwait(false);
等待处理程序(上下文)。配置等待(false);
};
//处理注销:从MSAL.NET缓存中删除帐户
options.Events.OnRedirectToIdentityProviderForSignOut=异步上下文=>
{
//从MSAL.NET令牌缓存中删除帐户
var tokenAcquisition=context.HttpContext.RequestServices.GetRequiredService();
Wait tokenAcquisition.RemoveAccountAsync(上下文)。ConfigureWait(false);
};
})
.AddInMemoryTokenCaches();

我真的不知道接下来该怎么办。如果您有任何建议、建议或想法,我们将不胜感激。

您能否重现错误,但这次请尝试访问需要验证的Microsoft网站,看看您是否仍然拥有他们管理的验证cookie。您能否重现错误,但这一次,请尝试访问需要身份验证的Microsoft网站,看看您是否仍然拥有他们管理的身份验证cookie。