public override bool FinishedLaunching(UIApplication application, NSDictionary launchOptions) { // Override point for customization after application launch. // If not required for your application you can safely delete this method var appleIdProvider = new ASAuthorizationAppleIdProvider(); appleIdProvider.GetCredentialState(KeychainItem.CurrentUserIdentifier, (credentialState, error) => { switch (credentialState) { case ASAuthorizationAppleIdProviderCredentialState.Authorized: // The Apple ID credential is valid. break; case ASAuthorizationAppleIdProviderCredentialState.Revoked: // The Apple ID credential is revoked. break; case ASAuthorizationAppleIdProviderCredentialState.NotFound: // No credential was found, so show the sign-in UI. InvokeOnMainThread(() => { var storyboard = UIStoryboard.FromName("Main", null); if (!(storyboard.InstantiateViewController(nameof(LoginViewController)) is LoginViewController viewController)) { return; } viewController.ModalPresentationStyle = UIModalPresentationStyle.FormSheet; viewController.ModalInPresentation = true; Window?.RootViewController?.PresentViewController(viewController, true, null); });
public async Task <AppleAccount> SignInAsync() { var appleIdProvider = new ASAuthorizationAppleIdProvider(); var request = appleIdProvider.CreateRequest(); request.RequestedScopes = new[] { ASAuthorizationScope.Email, ASAuthorizationScope.FullName }; var authorizationController = new ASAuthorizationController(new[] { request }); authorizationController.Delegate = this; authorizationController.PresentationContextProvider = this; authorizationController.PerformRequests(); tcsCredential = new TaskCompletionSource <ASAuthorizationAppleIdCredential>(); var creds = await tcsCredential.Task; if (creds == null) { return(null); } var appleAccount = new AppleAccount(); appleAccount.Token = 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(); return(appleAccount); }
public async Task <(string IdToken, string RawNonce)> GetCredentialAsync() { if (!Version.TryParse(UIDevice.CurrentDevice.SystemVersion, out var version) || version.Major < 13) { return(null, null); } var provider = new ASAuthorizationAppleIdProvider(); var nonce = GenerateNonce(32); var request = provider.CreateRequest(); request.RequestedScopes = new[] { ASAuthorizationScope.FullName, ASAuthorizationScope.Email }; request.Nonce = GetHashedNonce(nonce); var controller = new ASAuthorizationController(new[] { request }); var authorizationControllerDelegate = new AuthorizationControllerDelegate(UIApplication.SharedApplication.KeyWindow); controller.Delegate = authorizationControllerDelegate; controller.PresentationContextProvider = authorizationControllerDelegate; controller.PerformRequests(); var credential = await authorizationControllerDelegate.GetCredentialAsync().ConfigureAwait(false); var idToken = new NSString(credential.IdentityToken, NSStringEncoding.UTF8).ToString(); return(idToken, nonce); }
static async Task <WebAuthenticatorResult> PlatformAuthenticateAsync(Options options) { if (DeviceInfo.Version.Major < 13) { throw new FeatureNotSupportedException(); } var provider = new ASAuthorizationAppleIdProvider(); var req = provider.CreateRequest(); authManager = new AuthManager(Platform.GetCurrentWindow()); var scopes = new List <ASAuthorizationScope>(); if (options.IncludeFullNameScope) { scopes.Add(ASAuthorizationScope.FullName); } if (options.IncludeEmailScope) { scopes.Add(ASAuthorizationScope.Email); } req.RequestedScopes = scopes.ToArray(); var controller = new ASAuthorizationController(new[] { req }); controller.Delegate = authManager; controller.PresentationContextProvider = authManager; controller.PerformRequests(); var creds = await authManager.GetCredentialsAsync(); if (creds == null) { return(null); } var idToken = new NSString(creds.IdentityToken, NSStringEncoding.UTF8).ToString(); var authCode = new NSString(creds.AuthorizationCode, NSStringEncoding.UTF8).ToString(); var name = NSPersonNameComponentsFormatter.GetLocalizedString(creds.FullName, NSPersonNameComponentsFormatterStyle.Default, 0); var appleAccount = new WebAuthenticatorResult(); appleAccount.Properties.Add("id_token", idToken); appleAccount.Properties.Add("authorization_code", authCode); appleAccount.Properties.Add("state", creds.State); appleAccount.Properties.Add("email", creds.Email); appleAccount.Properties.Add("user_id", creds.User); appleAccount.Properties.Add("name", name); appleAccount.Properties.Add("realuserstatus", creds.RealUserStatus.ToString()); return(appleAccount); }
protected BaseSignInWithAppleService(Func <UIWindow> windowProvider) { _appleIdProvider = new ASAuthorizationAppleIdProvider(); _authControllerDelegate = new CustomDelegate(); _authControllerDelegate.CompletedWithAppleId += DidCompleteAuthWithAppleId; _authControllerDelegate.CompletedWithPassword += DidCompleteAuthWithPassword; _authControllerDelegate.CompletedWithError += DidCompleteAuthWithError; _presentationProvider = new CustomPresentationContextProvider(windowProvider); }
private void HandleAuthorizationAppleIDButtonPress(object sender, EventArgs e) { var appleIdProvider = new ASAuthorizationAppleIdProvider(); var request = appleIdProvider.CreateRequest(); request.RequestedScopes = new [] { ASAuthorizationScope.Email, ASAuthorizationScope.FullName }; var authorizationController = new ASAuthorizationController(new [] { request }); authorizationController.Delegate = this; authorizationController.PresentationContextProvider = this; authorizationController.PerformRequests(); }
private void SignInWithAppl() { var provider = new ASAuthorizationAppleIdProvider(); var request = provider.CreateRequest(); request.RequestedScopes = new[] { ASAuthorizationScope.Email, ASAuthorizationScope.FullName }; var authorizationController = new ASAuthorizationController(new[] { request }) { Delegate = this, PresentationContextProvider = this }; authorizationController.PerformRequests(); }
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); }
public async Task <AppleSignInCredentialState> GetCredentialStateAsync(string userId) { var appleIdProvider = new ASAuthorizationAppleIdProvider(); var credentialState = await appleIdProvider.GetCredentialStateAsync(userId); switch (credentialState) { case ASAuthorizationAppleIdProviderCredentialState.Authorized: // The Apple ID credential is valid. return(AppleSignInCredentialState.Authorized); case ASAuthorizationAppleIdProviderCredentialState.Revoked: // The Apple ID credential is revoked. return(AppleSignInCredentialState.Revoked); case ASAuthorizationAppleIdProviderCredentialState.NotFound: // No credential was found, so show the sign-in UI. return(AppleSignInCredentialState.NotFound); default: return(AppleSignInCredentialState.Unknown); } }
public void SignIn(AuthType type) { authType = type; switch (authType) { case AuthType.Google: GoogleSignIn.SharedInstance.SignInUser(); SetVerificationStatus(VerificationStatus.Initialized); break; case AuthType.Facebook: if (loginManager != null) { loginManager.LogIn(readPermissions.ToArray(), GetTopViewController(), HandleLogin); SetVerificationStatus(VerificationStatus.Initialized); } break; case AuthType.Apple: if (IsVersion13) { currentNonce = RandomNonceString(); var appleIdProvider = new ASAuthorizationAppleIdProvider(); var request = appleIdProvider.CreateRequest(); request.RequestedScopes = new[] { ASAuthorizationScope.Email, ASAuthorizationScope.FullName }; var authorizationController = new ASAuthorizationController(new[] { request }) { Delegate = this, PresentationContextProvider = this }; authorizationController.PerformRequests(); } break; } }