Esempio n. 1
0
        // Use constructor DI from IoC container again to get the
        // platform-specific IAuthNProvider and IAuthNProviderSettings injected in.
        // the plugin registers which IAuthNProvider to inject based on platform executing on
        public LoginService(IAuthNProvider authenticationProvider,
                            IAuthNProviderSettings authenticationProviderSettings,
                            IMvxMessenger mvxMessenger)
        {
            Debug.WriteLine("LoginService:Constructor: A LoginService from the AzureMobileAuthN Plugin was created.");

            _authNProvider = authenticationProvider;
            _authNProviderSettings = authenticationProviderSettings;
            _mvxMessenger = mvxMessenger;
        }
        public async Task<AuthenticationResult> AuthenticateAsync(AuthNProviderType providerType,
                                                                  IAuthNProviderSettings providerSettings)
        {
            // try without sending AMS Application key for now
            _mobileSvcsClient = new 
                MobileServiceClient(providerSettings.UrlToAuthenticationProvider);

            // default to MS account if whatever passed in is a no go
            // TODO: See Azure Mobile GitHub to see how they check for invalid Enum values
            var azmobProvider = MobileServiceAuthenticationProvider.MicrosoftAccount;

            // see which Azure Mobile Services Authentication Provider they want to use
            switch (providerType)
            {
                case AuthNProviderType.Google:
                    azmobProvider = MobileServiceAuthenticationProvider.Google;
                    break;
                case AuthNProviderType.Facebook:
                    azmobProvider = MobileServiceAuthenticationProvider.Facebook;
                    break;
                case AuthNProviderType.Twitter:
                    azmobProvider = MobileServiceAuthenticationProvider.Twitter;
                    break;
                case AuthNProviderType.Microsoft:
                    azmobProvider = MobileServiceAuthenticationProvider.MicrosoftAccount;
                    break;
                default:
                    break;
            }

            try
            {
                await _mobileSvcsClient.LoginAsync(azmobProvider);

                // TODO: How do I want to use User Profile Stuff here?
                // TODO: I will doouble set stff to see how it feels to use each

                var userProfile = await LoadUserProfile();

                var authResult = new AuthenticationResult
                {
                    ProviderName = _mobileSvcsClient.ToString(),
                    IdentityString = _mobileSvcsClient.CurrentUser.UserId,
                    MobileServicesUserToken = _mobileSvcsClient.CurrentUser.MobileServiceAuthenticationToken
                };

                return authResult;
            }
            catch (InvalidOperationException iop)
            {
                // TODO: if I try to login to Twitter and click back button on WP8
                // TODO: then System.InvalidOperationException is thrown but is NOT caught in Login VM!??  Why?
                // TODO: Must the try catch be inside here in the platform-specific authprovider?
                // TODO: Seems like that is the case, so leave it?

                // TODO: Do something useful
                //user cancelled so try again
                //MessageBox.Show("You must login to access the application.", "Please try again", MessageBoxButton.OK);
                Debug.WriteLine(
                    "AuthenticationProvider:InvalidOpException: You must login to access the application.");
            }
            catch (Exception ex)
            {
                // TODO: Do something useful
                //MessageBox.Show("We were not able to log you in, make sure you are connected to the internet and try again.", "Login failed", MessageBoxButton.OK);
                Debug.WriteLine(
                    "AuthenticationProvider:Exception: not able to log you in, make sure you are connected to the internet.");
            }
            finally
            {
                Debug.WriteLine("AuthenticationProvider:AuthenticateAsync: finally in try catch...got here!");
            }

            // TODO: how do I want to handle?
            // if I got here then something bad happened
            // TODO: Something besides null?

            return null;
        }
        public async Task<AuthNResult> AuthenticateAsync(AuthNProviderType authenticationProviderType,
                                                         IAuthNProviderSettings authenticationProviderSettings)
        {
            // try without sending Azure Mobile Service Application key for now
            //_mobileSvcsClient = new
            //    MobileServiceClient(authenticationProviderSettings.UrlToAuthenticationProvider);


            try
            {
                CurrentPlatform.Init();
                // Create the Mobile Service Client instance, using the provided
                // Mobile Service URL and key
                _mobileSvcsClient = new MobileServiceClient(authenticationProviderSettings.UrlToAuthenticationProvider);

            }
            catch (Java.Net.MalformedURLException)
            {
                //CreateAndShowDialog(new Exception("There was an error creating the Mobile Service. Verify the URL"), "Error");
            }
            catch (Exception e)
            {
                //CreateAndShowDialog(e, "Error");
            }



            // default to MS account if whatever passed in is a no go
            // TODO: See Azure Mobile GitHub to see how they check for invalid Enum values
            var azmobProvider = MobileServiceAuthenticationProvider.MicrosoftAccount;

            // see which Azure Mobile Services Authentication Provider they want to use
            switch (authenticationProviderType)
            {
                case AuthNProviderType.Google:
                    azmobProvider = MobileServiceAuthenticationProvider.Google;
                    break;
                case AuthNProviderType.Facebook:
                    azmobProvider = MobileServiceAuthenticationProvider.Facebook;
                    break;
                case AuthNProviderType.Twitter:
                    azmobProvider = MobileServiceAuthenticationProvider.Twitter;
                    break;
                case AuthNProviderType.Microsoft:
                    azmobProvider = MobileServiceAuthenticationProvider.MicrosoftAccount;
                    break;
                default:
                    break;
            }

            try
            {
                // use activity hack from Michixxx to be able to call MobileServiceClient.
                // this should be the rigth way to resolve current Activity...
                var activity = Mvx.Resolve<IMvxAndroidCurrentTopActivity>().Activity;

                await _mobileSvcsClient.LoginAsync(activity, azmobProvider);

                // TODO: How do I want to use User Profile Stuff here?
                // TODO: I will double set stuff to see how it feels to use each

                var userProfile = await LoadUserProfile();

                var authNResult = new AuthNResult
                {
                    ProviderType = authenticationProviderType.ToString(),
                    IdentityString = _mobileSvcsClient.CurrentUser.UserId,
                    MobileServicesUserToken = _mobileSvcsClient.CurrentUser.MobileServiceAuthenticationToken
                };

                return authNResult;
            }
            catch (InvalidOperationException iop)
            {
                // TODO: if I try to login to Twitter and click back button on WP8
                // TODO: then System.InvalidOperationException is thrown but is NOT caught in Login ViewModel!??  Why?
                // TODO: Must the try/catch be inside here in the platform-specific authprovider?
                // TODO: Seems like that is the case, so leave it?

                // TODO: Do something useful
                //user cancelled so try again
                //MessageBox.Show("You must login to access the application.", "Please try again", MessageBoxButton.OK);

                Console.WriteLine(
                    "AuthenticationProvider:InvalidOpException: You must login to access the application.");
                //Debug.WriteLine(
                //    "AuthenticationProvider:InvalidOpException: You must login to access the application.");
            }
            catch (Exception ex)
            {
                // TODO: Do something useful
                //MessageBox.Show("We were not able to log you in, make sure you are connected to the internet and try again.", "Login failed", MessageBoxButton.OK);
                Console.WriteLine(
                    "AuthenticationProvider:Exception: not able to log you in, make sure you are connected to the internet.");
            }
            finally
            {
                Console.WriteLine("AuthenticationProvider:AuthenticateAsync: finally - in try/catch...got here!");
            }

            // TODO: how do I want to handle?
            // if I got here, then something bad happened
            // TODO: Something besides null?

            return null;
        }
 // Use constructor DI from IoC container again to get the
 // platform-specific IAuthenticationProvider and IAuthNProviderSettings injected in.
 // the plugin registers which IAuthenticationProvider to inject based on platform executing on
 public LoginService(IAuthenticationProvider authenticationProvider, 
                     IAuthNProviderSettings authNProviderSettings)
 {
     _authNProvider = authenticationProvider;
     _authNProviderSettings = authNProviderSettings;
 }