Xamarin Azure Active Diretory 重定向 URI 错误

rod*_*nes 1 c# azure-active-directory xamarin.forms

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

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

应用类:

    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();
    }
Run Code Online (Sandbox Code Playgroud)

在构造函数的第四行中,如果我使用 PCA.RedirectUri = "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)
            {

            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

在 Azure 上,该应用程序已注册:

名称:AppSGA

应用程序类型:原生

重定向 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
}
Run Code Online (Sandbox Code Playgroud)

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

小智 6

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

这是有关为公共客户端设置 redirectUri文档

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

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

这是有关设置 Xamarin AndroidiOS的更多信息

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