2017-10-18 114 views
0

我想实现身份登录到我的跨平台的应用程序。我在我的应用程序中创建了Azure SQL数据库和配置的API服务。一切工作正常,我可以注册/登录用户。但是,我想检查用户是否登录并显示对话框(您成功注册/登录,或密码不匹配/错误等)。如果是成功,我想导航到应用程序的主页。你能告诉我如何做到这一点?感谢您的支持。Xamarin跨平台 - 登录/注册

这里是迄今所做的:

apiservices.cs

public class ApiServices { 
    public async Task RegisterUserAsync(string email, string password, string confirmPassword) { 
     var client = new HttpClient(); 
     var success = false; 
     var model = new RegisterBindingModel { 
      Email = email, Password = password, ConfirmPassword = confirmPassword 
     }; 
     try { 
      var json = JsonConvert.SerializeObject(model); 
      HttpContent content = new StringContent(json); 
      content.Headers.ContentType = new MediaTypeHeaderValue("application/json"); 
      if (email != null || password != null) { 
       success = true; 
       Acr.UserDialogs.UserDialogs.Instance.ShowSuccess(string.Format("You are now signed-in as {0}.", email)); 
       var response = await client.PostAsync(Constants.BaseApiAddress + "api/Account/Register", content); 
       Debug.WriteLine(response); 
       Debug.WriteLine(await response.Content.ReadAsStringAsync()); 
       Debug.WriteLine(response.StatusCode); 
      } 
     } catch (Exception ex) { 
      Acr.UserDialogs.UserDialogs.Instance.ShowError(string.Format("Authentication Failed: {0}", ex.Message)); 
     } 
    } 
} 

registerViewModel.cs

public class RegisterViewModel { 
    public string Email { 
     get; 
     set; 
    } 
    public string Password { 
     get; 
     set; 
    } 
    public string ConfirmPassword { 
     get; 
     set; 
    } 
    public ICommand RegisterCommand { 
     get { 
      return new Command(async() => { 
       ApiServices apiServices = new ApiServices(); 
       await apiServices.RegisterUserAsync(Email, Password, ConfirmPassword); 
      }); 
     } 
    } 
} 

loginViewModel.cs

public class LoginViewModel { 
     public string Email { 
      get; 
      set; 
     } 
     public string Password { 
      get; 
      set; 
     } 
     public ICommand LoginCommand { 
      get { 
       return new Command(async() => { 
        ApiServices apiServices = new ApiServices(); 
        await apiServices.LoginUserAsync(Email, Password); 
       }); 
      } 
     } 
    } 
+0

您有什么具体的问题?你似乎知道如何调用你的API方法,并显示一个对话框?你有什么特别的不明白? – Jason

+0

我不明白如何检查用户登录/注册以及显示对话框给用户返回(“您注册”,“密码不匹配”,“错误的电子邮件格式”等) – eccoripo

+0

您的登录/注册函数应该返回一个你需要存储的布尔值(使用Settings,如下面的建议)你可以很容易地使用内置的DisplayAlert或使用已经使用的Acr Dialogs包显示对话框 – Jason

回答

0

安装Xam.Plugins.Settings

这将添加一个名为设置

在这个类的帮助类,你应该加上:

private const string IsLoginKey = "login_key"; 
private static readonly bool IsLoginDefault = false; 

//Then adding this property 
public static bool IsLogin 
{ 
    get 
    { 
     return AppSettings.GetValueOrDefault(IsLoginKey, IsLoginDefault); 
    } 
    set 
    { 
     AppSettings.AddOrUpdateValue(IsLoginKey, value); 
    } 
} 

然后在您App.xaml.cs文件是这样的:

using YourProjectNameSpace.Helper 

..... 

if (Settings.IsLoggedIn) 
{ 
    MainPage = new NavigationPage(new MainPage()); 
} 
else 
{ 
    MainPage.DisplayAlert("Your Title","You Message","OK"); 
    //Or navigate to Login page, like this: 
    //MainPage = new NavigationPage(new LoginPage()); 
} 
+0

感谢你的回应,它给了我有一些想法,但这不是我想要的,而且你的代码逻辑不完整,它不会在这两种情况下循环。 – eccoripo