gpt4 book ai didi

c# - Xamarin Azure Active Directory 重定向 URI 错误

转载 作者:太空宇宙 更新时间:2023-11-03 19:39:41 25 4
gpt4 key购买 nike

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

应用程序能够转到 Microsoft SSO 页面,但在点击登录按钮后页面返回错误:AADSTS50011:请求中指定的回复网址与为应用程序配置的回复网址不匹配:[ 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();
}

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

}
}
}

在 Azure 上,该应用注册于:

名称:AppSGA

应用程序类型:原生

重定向 URi:AppSGA://auth

Azure 上的应用程序 list :

   {
"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 的默认 redirectUri 格式。

这是 documentation on setting up the redirectUri for public clients .

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

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

这里有更多info on setting up Xamarin AndroidiOS

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

关于c# - Xamarin Azure Active Directory 重定向 URI 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55461799/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com