该应用程序无法访问 Xamarin.Forms 中的 iOS 钥匙串,但可以在 Android 中使用

Van*_*ood 5 xamarin.ios azure-active-directory xamarin.forms azure-ad-b2c azure-ad-msal

您好,我正在学习使用 MSAL,但遇到了有线问题。我完全遵循了教程,并且正在使用示例代码。但我在App.xaml.cs中构建客户端时仍然遇到此异常:

microsoft.identity.client.msalclientException:应用程序无法访问应用程序发布者的 ios 钥匙串(团队 ID 为空)。这是为了在同一发布者的应用程序之间启用单点登录所必需的。这是ios配置问题。有关启用钥匙串访问的更多详细信息,请参阅 https://aka.ms/msal-net-enable-keychain-access 。

AuthenticationClient = PublicClientApplicationBuilder.Create(Constants.ClientId)
                .WithIosKeychainSecurityGroup(Constants.IosKeychainSecurityGroups)
                .WithB2CAuthority(Constants.AuthoritySignin)
                .WithRedirectUri($"msal{Constants.ClientId}://auth")
                .Build();
Run Code Online (Sandbox Code Playgroud)

相关设置如下:

信息.plist:

<key>CFBundleURLTypes</key>
    <array>
        <dict>
            <key>CFBundleURLName</key>
            <string>ADB2C Auth</string>
            <key>CFBundleURLSchemes</key>
            <array>
                <string>msal....</string>
            </array>
            <key>CFBundleTypeRole</key>
            <string>None</string>
        </dict>
        <dict>
            <key>CFBundleURLName</key>
            <string>URL Type 1</string>
        </dict>
    </array>
Run Code Online (Sandbox Code Playgroud)

Contants.cs 中的 ioskeychain

 // set to a unique value for your app, such as your bundle identifier. Used on iOS to share keychain access.
        static readonly string iosKeychainSecurityGroup = "com.xamarin.adb2cauthorization";

Run Code Online (Sandbox Code Playgroud)

权利.plist:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>keychain-access-groups</key>
    <array>
        <string>$(AppIdentifierPrefix)com.xamarin.adb2cauthorization</string>
    </array>
</dict>
</plist>

Run Code Online (Sandbox Code Playgroud)

应用程序委托:

public override bool OpenUrl(UIApplication app, NSUrl url, NSDictionary options)
        {
            AuthenticationContinuationHelper.SetAuthenticationContinuationEventArgs(url);
            return base.OpenUrl(app, url, options);
        }
Run Code Online (Sandbox Code Playgroud)

我尝试将 Microsoft.Identity.Client 升级到 v4.17.1,但仍然无法正常工作。由于共享代码适用于 Android 部分,我真的不确定我可能会丢失什么。任何帮助,将不胜感激。

Luc*_*ler 5

转到 ios 项目属性 => iOS Bundle Signing 并选择您的 Entitlements.plist。

Entitlements.plist 文件应如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>aps-environment</key>
    <string>development</string>
    <key>keychain-access-groups</key>
    <array>
        <string>$(AppIdentifierPrefix)com.microsoft.adalcache</string>
    </array>
</dict>
</plist>
Run Code Online (Sandbox Code Playgroud)

还; 重要的是将此文件设置为 maui oder xamarin 中的自定义权利(ios 设置)

  • 还; 重要的是将此文件设置为 maui oder xamarin 中的自定义权利(ios 设置) (2认同)