我正在使用 Xamarin.Forms 并尝试在 this tutorial 之后使用 Microsoft Graph API .
这在 iOS 模拟器中完美运行,但是当我在我的实际设备上尝试时,我无法登录。当我单击 WelcomePage.xaml
上的登录按钮时,它完全没有任何反应。 ...有什么我想念的吗?
这是我的登录
方法:
public async Task SignIn()
{
var scopes = OAuthSettings.Scopes.Split(' ');
// First, attempt silent sign in
// If the user's information is already in the app's cache,
// they won't have to sign in again.
string accessToken = string.Empty;
try
{
var accounts = await PCA.GetAccountsAsync();
if (accounts.Count() > 0)
{
var silentAuthResult = await PCA
.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
Debug.WriteLine("User already signed in.");
Debug.WriteLine($"Access token: {silentAuthResult.AccessToken}");
accessToken = silentAuthResult.AccessToken;
}
}
catch (MsalUiRequiredException)
{
// This exception is thrown when an interactive sign-in is required.
Debug.WriteLine("Silent token request failed, user needs to sign-in");
}
if (string.IsNullOrEmpty(accessToken))
{
// Prompt the user to sign-in
var interactiveRequest = PCA
.AcquireTokenInteractive(scopes);
if (AuthUIParent != null)
{
interactiveRequest = interactiveRequest
.WithParentActivityOrWindow(AuthUIParent);
}
var authResult = await interactiveRequest.ExecuteAsync();
Debug.WriteLine($"Access Token: {authResult.AccessToken}");
}
// Initialize Graph client
GraphClient = new GraphServiceClient(new DelegateAuthenticationProvider(
async(requestMessage) =>
{
var accounts = await PCA
.GetAccountsAsync();
var result = await PCA
.AcquireTokenSilent(scopes, accounts.FirstOrDefault())
.ExecuteAsync();
requestMessage.Headers.Authorization =
new AuthenticationHeaderValue("Bearer", result.AccessToken);
}));
await GetUserInfo();
IsSignedIn = true;
}
它在这条线上停止工作:
var authResult = await interactiveRequest.ExecuteAsync();
这是我的 Entitlements.plist
最佳答案
iOS 12 存在一个已知问题 documented here :
Microsoft has released a security advisory to provide information about an incompatibility between iOS12 and some types of authentication. The incompatibility breaks social, WSFed, and OIDC logins. This advisory also provides guidance on what developers can do to remove current security restrictions added by ASP.NET to their applications to become compatible with iOS12.
我怀疑行为差异的原因与您设备上的 iOS 版本和模拟器有关。
security advisory文档链接包括一些潜在的修复:
If you are using ASP.NET Core Identity you disable the protection by configuring cookies with the following code
services.ConfigureExternalCookie(options => { // Other options options.Cookie.SameSite = SameSiteMode.None; }); services.ConfigureApplicationCookie(options => { // Other options options.Cookie.SameSite = SameSiteMode.None; });
If you are using cookie authentication without ASP.NET Core identity you can turn off the protection with the following code
services.AddCookie(CookieAuthenticationDefaults.AuthenticationScheme, options => { // Other options options.Cookie.SameSite = Microsoft.AspNetCore.Http.SameSiteMode.None; })
关于c# - Xamarin.Forms 和 Microsoft Graph 无法在 iOS 设备上运行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59078756/