public async Task<LoginResult> LoginAsync(AuthNProviderType providerType)
        {
            // force a logout on the mobile services client and  clear user profile 
            _authNProvider.Logout();

            // TODO: should add some error checking of the provider we get sent from ViewModel/View
            // TODO: but for now will just use as is

            var authNResult = await _authNProvider.AuthenticateAsync(providerType, _authNProviderSettings);

            if (authNResult != null)
            {
                var loginResult = new LoginResult
                {
                    ProviderName = authNResult.ProviderName,
                    IdentityString = authNResult.IdentityString,
                    MobileServicesUserToken = authNResult.MobileServicesUserToken
                };

                return loginResult;
            }
            else
            {
                // TODO: Something bad happend
                return null;
            }

        }
Esempio n. 2
0
        public async Task<LoginResult> LoginAsync(AuthNProviderType authenticationProviderType)
        {
            // force a logout on the mobile services client and clear user profile 
            _authNProvider.Logout();

            // TODO: should add some error checking of the provider we get sent from ViewModel/View
            // TODO: but for now will just use as is

            var authNResult = await _authNProvider.AuthenticateAsync(authenticationProviderType,
                                                                     _authNProviderSettings);

            if (authNResult != null)
            {

                Debug.WriteLine("LoginService:LoginAsync: AuthN Result not null, send Mvx Message with Id and Token.");
                var loginMessage = new LoginMessage(this, authNResult.ProviderType,
                                                    authNResult.IdentityString,
                                                    authNResult.MobileServicesUserToken);

                _mvxMessenger.Publish(loginMessage);

                var loginResult = new LoginResult
                {
                    ProviderType = authNResult.ProviderType,
                    IdentityString = authNResult.IdentityString,
                    MobileServicesUserToken = authNResult.MobileServicesUserToken
                };

                Debug.WriteLine("LoginService:LoginAsync: ProviderType is: {0}", authNResult.ProviderType);
                Debug.WriteLine("LoginService:LoginAsync: IdentityString is: {0}", authNResult.IdentityString);
                Debug.WriteLine("LoginService:LoginAsync: MobileServicesUserToken is: {0}", authNResult.MobileServicesUserToken);

                return loginResult;
            }
            else
            {
                // TODO: Something bad happend
                Debug.WriteLine("LoginService:LoginAsync: AuthN Result WAS NULL so returning null LoginResult.");

                return null;
            }

        }
        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;
        }