如何在Xamarin中实现自动登录?

时间:2019-06-02 19:57:30

标签: xamarin xamarin.forms prism xamarin.auth

我正在Xamarin中实现具有登录功能的应用程序。我正在使用Xamarin SecureStorage将我的用户名和密码存储在钥匙串中。我正在使用Prism在页面之间进行搜索。

我有一个AuthenticationService,可以处理所有通过SecureStorage保存和存储钥匙串数据。在我的App.Xaml中,下面有一个PubSubEvent,用于处理用户是否通过身份验证。

   var result = await NavigationService.NavigateAsync("/MainPage?title=Welcome");

   if (!result.Success)
   {
        Console.WriteLine("Error {0}", result.Exception);
        logger.Report(result.Exception, new Dictionary<string, string> { { "navigationPath", "/MainPage" } });
                //MainPage = result.CreateErrorPage();
        System.Diagnostics.Debugger.Break();
   }

在我的LoginPageViewModel中,根据我的登录按钮,我具有以下绑定。

   private async Task OnLoginUserCommandExecutedAsync()
    {
        try
        {
            MyPrivateSession response = new MyPrivateSession();

            //Username/Password text is valid
            response = await _authenticationService.LoginAsync(UserLogin);

            if (!response.IsValidUser)
            {
                await PageDialogService.DisplayAlertAsync("", "Login Unsuccessful - Invalid User Id or Password", "Ok");
            }
        }

        catch (AuthenticationException authEx)
        {
            await PageDialogService.DisplayAlertAsync("Error", authEx.Message, "Ok");
        }

        catch (Exception e)
        {
            Debugger.Break();
            Logger.Log(e.Message, Category.Exception, Priority.Medium);
            await PageDialogService.DisplayAlertAsync("Fatal Error", e.Message, "Ok");
        }
    }

也在我的LoginPageViewModel中,我在onNavigatedTo中包含以下代码以从SecureStorage获取保存的登录信息

   public override async void OnNavigatedTo(INavigationParameters parameters)
        {
            // Determine if there is a preferred account to use in cases where the user is logged out and sent directly to the login
            // in this case the AssumedLogin isn't defined.
            if (_authenticationService.AssumedLogin is null)
            {
                await _authenticationService.AssumeUserPriorToLogin();
                if (!(_authenticationService.AssumedLogin is null))
                {

                    UserLogin.Username = _authenticationService.AssumedLogin.Username;
                    UserLogin.Password = _authenticationService.AssumedLogin.Properties["password"];

                    //Segues to VIN Utilities
                    await OnLoginUserCommandExecutedAsync();

                }
            }
        }

在我的AuthenticationService中,从SecureStorage访问用户名和密码后,在逻辑末尾有这个提示

var user = GetCurrentUser();
                    _container.UseInstance(user);

                    //Publish User Authenticated Event
                    _eventAggregator.GetEvent<UserAuthenticatedEvent>().Publish(user);

我的问题是,当用户使用登录按钮手动登录时,一切正常。但是,当我在LoginPageViewModel的{​​{1}}上添加逻辑以在用户具有保存的用户名和密码时自动对用户进行身份验证。它把我踢出OnNavigatedTo,回到登录页面。

我的一个疑问是,当我通过导航页面自动登录时,应用程序尚未完成加载。在MainPage中的“已加载我的应用程序”之前,应用程序将首先从OnNavigatedTo登录MainPage。 (请参见下文)如何确保在开始搜索主页之前先完成iOS初始化程序?

AppDelegate

0 个答案:

没有答案