Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/wpf/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# WPF应用程序-AzureAD受众验证失败_C#_Wpf_Azure Active Directory - Fatal编程技术网

C# WPF应用程序-AzureAD受众验证失败

C# WPF应用程序-AzureAD受众验证失败,c#,wpf,azure-active-directory,C#,Wpf,Azure Active Directory,所以我被卡住了 我在Azure上运行了一个简单的api。它已根据公司政策配置AzureAD。 我可以通过浏览器访问api。还尝试了postman访问,该功能在以下设置下也可以正常工作: 类型:OAuth2.0 Grant Type: Implicit Callback URL: https://login.microsoftonline.com/common/oauth2/nativeclient Auth URL: https://login.microsoftonline.com/{{ten

所以我被卡住了

我在Azure上运行了一个简单的api。它已根据公司政策配置AzureAD。 我可以通过浏览器访问api。还尝试了postman访问,该功能在以下设置下也可以正常工作:

类型:OAuth2.0

Grant Type: Implicit
Callback URL: https://login.microsoftonline.com/common/oauth2/nativeclient
Auth URL: https://login.microsoftonline.com/{{tenant_id}}/oauth2/authorize?resource={{client_id}}
Client ID: {{client_id}}
Scope: api://{{client_id}}/gh_client_access
现在我正尝试使用相同的设置通过WPF应用程序进行访问。我用的是MSAL。 在我的应用程序中,我配置了以下内容:

static App()
{
    _clientApp = PublicClientApplicationBuilder.Create(ClientId)
        .WithRedirectUri("https://login.microsoftonline.com/common/oauth2/nativeclient")
        .WithB2CAuthority($"https://login.microsoftonline.com/{Tenant}/oauth2/authorize?resource={ClientId}")
        .WithTenantId(Tenant)
        .WithClientId(ClientId)
        .Build();
}
在窗口中,我得到一个令牌:

authResult = await app.AcquireTokenInteractive(scopes)
                    .WithAccount(accounts.FirstOrDefault())
                    .WithPrompt(Prompt.SelectAccount)
                    .ExecuteAsync();
我收到一个令牌,但在发出请求时,我收到了错误:

{"code":401,"message":"IDX10214: Audience validation failed. Audiences: '[PII is hidden]'. Did not match: validationParameters.ValidAudience: '[PII is hidden]' or validationParameters.ValidAudiences: '[PII is hidden]'."}

我遗漏了什么?

在这里发布这个答案,以防其他人也有同样的问题

第一个错误,B2C授权,而我需要常规Azure广告:

static App()
{
    _clientApp = PublicClientApplicationBuilder.Create(ClientId)
        .WithDefaultRedirectUri()
        .WithAuthority($"https://login.microsoftonline.com/{Tenant}")
        .Build();
}
现在,另一个问题在范围内。这定义了受众,格式错误。 我有
api://{client\u id}/{scope\u name}
它必须是
{client\u id}/{scope\u name}

如果你有同样的情况,邮递员正在工作,看看并解码你的工作令牌和你的错误令牌进行比较


感谢@AllenWu的帮助

您正在使用B2C身份验证?格式应为
.WithB2CAuthority(“https://fabrikamb2c.b2clogin.com/tfp/{tenant}/{PolicySignInSignUp}”)
。请试一试。嗨,谢谢你的评论,很抱歉我工作很忙,我今天会试试看这是否能解决问题,然后回来报告。我可以知道你正在使用哪个idp吗?Azure AD或Azure AD B2C?如果您使用AAD,则应使用
with authority()
方法,而不是
with b2cauthority()
。类似于
.WithAuthority($)https://login.microsoftonline.com/{Tenant}/oauth2/authorize”)
我实际上认为您是对的,我应该使用WithAuthority(),(实际上我已经尝试了很多不同的方法),但我仍然会遇到访问群体错误。正如我提到的,在《邮递员》中,一切都很顺利