我正在尝试让我的Xamarin应用程序运行Azure AD B2C。为了检查我们是否已经登录,我使用了如下代码。
public async Task<bool> IsLoggedIn()
{
IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync();
if (accounts != null)
{
return accounts.Any();
}
return false;
}
}
开始时我的代码如下
protected async override void OnStart()
{
var authenticationService = Container.Resolve<IAuthenticationService>();
bool isLoggedIn;
isLoggedIn = await authenticationService.IsLoggedIn();
if (isLoggedIn)
{
var cachingService = Container.Resolve<ICachingService>();
AppUser = await authenticationService.Login(cachingService.CurrentMode);
await NavigationService.NavigateAsync("http://www.xxxx.com/root/navigation/main");
}
else
{
await NavigationService.NavigateAsync("http://www.xxxx.com/navigation/login");
}
}
}
如果我只是从isloggedin返回false,它会正确地显示登录页面。
但呼唤
IEnumerable<IAccount> accounts = await App.PCA.GetAccountsAsync();
似乎有问题。
我有例外(有时)
System.NullReferenceException:对象引用未设置为对象的实例
但在“QuickWatch”中查看应用程序对象时,我发现pac对象被正确填充。
更新
app.cs构造函数如下
public static PublicClientApplication PCA = null;
public App(IPlatformInitializer initializer = null) : base(initializer)
{
PCA = new PublicClientApplication(GlobalSetting.Instance.ClientID, GlobalSetting.Instance.AuthoritySignUpSignIn);
PCA.RedirectUri = $"msal{GlobalSetting.Instance.ClientID}://auth";
}
我的登录方法如下
public async Task<User> Login(string mode)
{
IEnumerable<IAccount> accounts = await App.PCA?.GetAccountsAsync();
AuthenticationResult ar;
try
{ //get token from cache.
ar = await App.PCA?.AcquireTokenSilentAsync(GlobalSetting.Instance.Scopes, GetAccountByPolicy(accounts, GlobalSetting.Instance.PolicySignUpSignIn), GlobalSetting.Instance.AuthoritySignUpSignIn, false);
}
catch (MsalUiRequiredException ex)
{
// get token from interaction.
ar = await App.PCA?.AcquireTokenAsync(GlobalSetting.Instance.Scopes, GetAccountByPolicy(accounts, GlobalSetting.Instance.PolicySignUpSignIn), App.UiParent);
}
//fetch token and make actual user object.
return new User
{
Id = _cachingService.LoggedInUserId,
Name = "Jessica Doe",
ProfilePicUrl = "https://content-static.upwork.com/uploads/2014/10/01xxx27/profilephoto1.jpg",
BusinessProfile = _userService.GetBusinessProfile(_cachingService.LoggedInUserId),
ProfileVariables = _userService.GetUserProfileVariables(_cachingService.LoggedInUserId),
Settings = _userService.GetSettings(_cachingService.LoggedInUserId)
};
}
当我不调用AcquireTokensilentAsync,只发送一个假的用户对象时,它会工作;当我调用AcquireTokensilentAsync时,它不会工作。
AR对象被填充,它导航到主页面,点击它的视图模型构造函数,但显示一个空白页。
我也尝试过不同版本的MSAL。现在是最新版本。
更新2
深入到这个例外
microsoft.identity.client.msalclientException:应用程序没有在authorities.plist中启用钥匙链访问组。结果,保存到iOS钥匙链失败。在entitlements.plist中未启用密钥链访问组“3g3lmcd5r.com.microsoft.adalcache”。见
https://aka.ms/msal-net-enable-keychain-groups
有关启用钥匙链访问组和权利的详细信息。位于Microsoft.Identity.Core.IOStokencacheAssor.Save(System.String帐户,System.String服务,System.String通用,System.Int32类型,System.String值)[0x00052]In:0,位于Microsoft.Identity.Core.IOStokencacheAssor.SaveAccessToken(Microsoft.Identity.Core.Cache.MSALAccessTokenCacheItem项)[0x00028]In:0,位于Microsoft.Identity。core.metremetry.metremetryTokenCacheAccessor.saveAccessToken(microsoft.identity.core.cache.msalaaccessTokenCacheItem item)[0x0000]位于:0,位于Microsoft.identity.core.metremetry.metremetryTokenCacheAccessor.saveAccessToken(microsoft.identity.core.cache.msalaaccessTokenCacheItem item,microsoft.identity.core.requestContext requestContext)[0x002A]中:0,位于microsoft.identity.client.tokencache.saveaccessandrefreshtoken(microsoft.identity.core.instance.ivalidatedauthoritiescache validateauthoritiescache,microsoft.identity.core.instance.iaadInstanceDiscovery aadinstanceDiscovery,microsoft.identity.client.internal.requests.authenticationrequestParameters,microsoft.identity.core.oauth2.msaltokenresponse response)[0x00143]位于:0,位于Microsoft.Identity.Client.Internal.RequestBase.CacheTokenResponse和CreateAuthenticationResult(Microsoft.Identity.Core.OAuth2.msaltokenresponse msaltokenresponse)[0x001B4]位于:0,位于Microsoft.Identity.Client.Internal.Requests.InteractionRequest+D_u9.MoveNext()[0x00168]位于:0---从引发异常的前一位置开始的堆栈跟踪结束---在Microsoft.Identity.Client.Internal.Requests.RequestBase+D_uuu28.MoveNext()[0x00160]In:0---从引发异常的前一位置开始的堆栈跟踪结束---在Microsoft.Identity.Client.PublicClientApplication+D_24.MoveNext()[0x00EF]In:0---ST结束ACK跟踪来自引发异常的前一个位置---在Microsoft.Identity.Client.PublicClientApplication+D_uu17.MoveNext()[0x000ac]在:0---堆栈跟踪结束于引发异常的前一个位置---在cdthat.services.azureadb2authenticationservice+D_uu3.MoveNext()[0x000fc]在c:\cdthat best\cdthat\services\azureadb中2authenticationservice.cs:45---从引发异常的上一个位置开始的堆栈跟踪结束---在cdthat.viewmodels.loginpageviewmodel+d_uu8.movenext()[0x0003c]中,C:\cdthat best\cdthat\viewmodels\loginpageviewmodel.cs:56---从引发异常的上一个位置开始的堆栈跟踪结束---在system.runtime.compilerservices.a中。syncmethodbuildercore+<>c.b_uu6_0(system.object state)[0x0000]在/library/frameworks/xamarin.ios.framework/versions/12.2.1.11/src/xamarin.ios/mcs/class/referencesource/mscorlib/system/runtime/compilerservices/asynchmethodbuilder.cs:1023在foundation.nsAsynchronizationContextDispatcher.apply()[0x0000]在/library/frameworks/xamari中N.IOS。框架/版本/122.1.11/SRC/XAMARIN。IOS/Fung/NasActudio.C.:AT(包装器管理到本地)UIKIT。UIApple。UIApplicationMain(int,String [],INTPTR,INTPTR)在UIKit.UIApplication.Main(System .String)[AgsSystem,INTPTR主体,System,INTPTR委托] [0x090005]中/库/框架/ XAMARIN.IOS.Frrase/Value/2.2.1。11/src/xamarin.ios/uikit/uiapplication.cs:79 at uikit.uiapplication.main(system.string[]args,system.string principalclassname,system.string delegateclassname)[0x0002c]in/library/frameworks/xamarin.ios.framework/versions/12.2.1.11/src/xamarin.ios/uikit/uiapplication.cs:63 at cdthat.ios.application.main(system.string[]args)[0x00014]inC:\cdthatbest\cdthat.ios\main.cs:16错误代码:缺少权限