在.NET8 MAUI中如何为GraphClient传递WithIosKeychainSecurityGroup?
解决iOS上GraphServiceClient的Keychain安全组配置问题
要在.NET MAUI iOS项目中为GraphServiceClient配置iOS Keychain安全组,你需要在TokenCredentialOptions中指定对应的安全组,具体操作如下:
修改TokenCredentialOptions配置
在初始化TokenCredentialOptions时,添加IosKeychainSecurityGroup属性,值设置为你在Entitlements.plist中配置的安全组字符串:private static readonly TokenCredentialOptions options = new TokenCredentialOptions { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud, // 替换为你在Entitlements.plist中配置的安全组 IosKeychainSecurityGroup = "XXXXX.com.microsoft.adalcache" };确认Entitlements.plist配置有效性
确保你的Entitlements.plist已开启Keychain共享,并包含指定的安全组,配置示例如下:<key>keychain-access-groups</key> <array> <string>XXXXX.com.microsoft.adalcache</string> </array>保持凭证与Graph客户端初始化逻辑
原有的UsernamePasswordCredential和GraphServiceClient初始化逻辑无需额外修改,此时凭证会自动使用指定的Keychain安全组存储令牌,解决iOS平台的权限报错问题。
修改后的完整初始化代码如下:
private static readonly string[] scopes = new[] { "https://graph.microsoft.com/.default" }; private static readonly string tenantId = "tenant"; private static readonly string clientId = "client"; private static readonly TokenCredentialOptions options = new TokenCredentialOptions { AuthorityHost = AzureAuthorityHosts.AzurePublicCloud, IosKeychainSecurityGroup = "XXXXX.com.microsoft.adalcache" }; private static readonly UsernamePasswordCredential credential = new UsernamePasswordCredential("username", "password", tenantId, clientId, options); private static readonly GraphServiceClient graph = new GraphServiceClient(credential, scopes);
注意:安全组格式必须与Apple开发者账号中配置的一致,同时要确保MAUI项目在iOS平台下正确引用了Entitlements.plist(可在项目属性的iOS签名选项中指定)。
内容的提问来源于stack exchange,提问作者eengstroem
相关产品推荐
相关产品推荐

