Xamarin Azure Active Diretory Redirect URI Error



我正在开发一个需要通过Azure AD对用户进行身份验证的Xamarin应用程序,我正在使用Microsoft.Identity.Client nugget。

该应用程序能够转到 microsoft SSO 页面,但在点击登录按钮后,页面返回错误:AADSTS50011:请求中指定的回复 URL 与为应用程序配置的回复 URL 不匹配:[应用程序 ID]

应用类:

public static string ClientID = [ApplicationID];
public static string[] Scopes = { "User.Read" };
public static string Username = string.Empty;
public static UIParent UiParent { get; set; }
public App()
{
InitializeComponent();
PCA = new PublicClientApplication(ClientID);
PCA.RedirectUri = $"msal{App.ClientID}://auth";
//PCA.RedirectUri = $"AppSGA://auth";

MainPage = new Paginas.Login();
}

在构造函数的第四行中,如果我使用 PCA。重定向 Uri = "AppSGA://auth";而不是PCA。RedirectUri = $"msal{App.ClientID}://auth"; 它不会返回错误,但 SSO 页面将永远加载而没有任何响应。

登录页面的登录方法:

private async void ADLogin()
{
AuthenticationResult authResult = null;
IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync();
try
{
IAccount firstAccount = accounts.FirstOrDefault();
authResult = await App.PCA.AcquireTokenSilentAsync(App.Scopes, firstAccount);
await UserDataAsync(authResult.AccessToken).ConfigureAwait(false);
}
catch (MsalUiRequiredException ex)
{
try
{
authResult = await App.PCA.AcquireTokenAsync(App.Scopes, App.UiParent);
await UserDataAsync(authResult.AccessToken);
}
catch (Exception ex2)
{
}
}
}

在 Azure 上,应用注册到:

名称: 阿普加

应用程序类型:本机

重定向 URi:AppSGA://auth

Azure 上的应用清单:

{
"appId": AppID,
"appRoles": [],
"availableToOtherTenants": true,
"displayName": "AppSGA",
"errorUrl": null,
"groupMembershipClaims": null,
"optionalClaims": null,
"acceptMappedClaims": null,
"homepage": null,
"informationalUrls": {
"privacy": null,
"termsOfService": null
},
"identifierUris": [],
"keyCredentials": [],
"knownClientApplications": [],
"logoutUrl": null,
"oauth2AllowImplicitFlow": true,
"oauth2AllowUrlPathMatching": true,
"oauth2Permissions": [],
"oauth2RequirePostResponse": false,
"objectId": [ObjectId],
"parentalControlSettings": {
"countriesBlockedForMinors": [],
"legalAgeGroupRule": "Allow"
},
"passwordCredentials": [],
"publicClient": true,
"replyUrls": [
"AppSGA://auth"
],
"requiredResourceAccess": [
{
"resourceAppId": resourceAppId,
"resourceAccess": [
{
"id": id,
"type": "Scope"
}
]
},
{
"resourceAppId": resourceAppId
"resourceAccess": [
{
"id": id,
"type": "Scope"
}
]
}
],
"samlMetadataUrl": null
}

有谁知道这个问题的原因是什么?

PCA.RedirectUri = $"msal{App.ClientID}://auth";是使用 MSAL 时 Xamarin Android 和 iOS 的默认重定向 Uri 格式。

下面是有关为公共客户端设置 redirectUri 的文档。

如果您使用的是 Android,则需要在 AndroidManifest.xml 中包含以下内容:

<data android:scheme="msal{client_id}" android:host="auth" />

以下是有关设置 Xamarin Android 和 iOS 的更多信息

此外,应考虑更新到 MSAL v3.x。下面是博客文章的链接,其中概述了如何迁移到新的构建器模式以创建公共客户端和处理获取令牌调用。

这通常是因为在门户中配置的回复 URL 与应用指定的回复 URL 不同。这必须是完全匹配的。

这是解决此问题的教程。它适用于应用服务 https://blogs.msdn.microsoft.com/jpsanders/2018/01/30/azure-app-service-error-aadsts50011-the-reply-address-http-azurewebsites-netsignin-oidc-does-not-match-the-reply-addresses-configured-for-the-application/,但相同的步骤适用。

有时传播此更改确实需要几分钟 - 我经常被抓住,因为我在 AAD 中更改了此设置,但认为它尚未修复,因为它没有立即开始工作。

最新更新