如何在使用Xamarin Essentials Web身份验证器获取authToken后获取苹果用户姓名和电子邮件。

问题描述 投票:0回答:1

我想在Xamarin Froms Essentials Web Authenticator之后从苹果公司获取电子邮件、用户名和姓氏等信息。我只得到WebAuthenticatorResult,其中只包含令牌、刷新令牌和令牌ID。我如何使用这些令牌获取用户信息?我使用的是asp net core 3.1的Restful Web Api。

你能帮助我吗?苹果的文档不清楚,是如此混乱。请帮助... 谢谢你的帮助。

web xamarin.forms apple-sign-in authenticator xamarin.essentials
1个回答
0
投票

我不知道你是否可以从Xamarin Froms Essentials Web Authenticator中获得这些用户信息。WebAuthenticatorResult.Properties 属性.

如果你不能,你可以自己实现Apple SignIn,如上所述。此处你可以通过使用Xamarin.forms中的DependencyService获取这些信息。

public interface IAppleSignInService
{
    bool Callback(string url);

    Task<AppleAccount> SignInAsync();
}

而在iOS项目中

public async Task<AppleAccount> SignInAsync()
{
    // Fallback to web for older iOS versions
    if (!Is13)
        return await webSignInService.SignInAsync();

    AppleAccount appleAccount = default;

#if __IOS__13
    var provider = new ASAuthorizationAppleIdProvider();
    var req = provider.CreateRequest();

    authManager = new AuthManager(UIApplication.SharedApplication.KeyWindow);

    req.RequestedScopes = new[] { ASAuthorizationScope.FullName, ASAuthorizationScope.Email };
    var controller = new ASAuthorizationController(new[] { req });

    controller.Delegate = authManager;
    controller.PresentationContextProvider = authManager;

    controller.PerformRequests();

    var creds = await authManager.Credentials;

    if (creds == null)
        return null;

    appleAccount = new AppleAccount();
    appleAccount.IdToken = JwtToken.Decode(new NSString(creds.IdentityToken, NSStringEncoding.UTF8).ToString());
    appleAccount.Email = creds.Email;
    appleAccount.UserId = creds.User;
    appleAccount.Name = NSPersonNameComponentsFormatter.GetLocalizedString(creds.FullName, NSPersonNameComponentsFormatterStyle.Default, NSPersonNameComponentsFormatterOptions.Phonetic);
    appleAccount.RealUserStatus = creds.RealUserStatus.ToString();
#endif

    return appleAccount;
}
© www.soinside.com 2019 - 2024. All rights reserved.