我正在尝试将身份登录实施到我的跨平台应用中。我在我的应用程序中创建了Azure SQL数据库并配置了API服务。一切正常,我可以注册/登录用户。但是,我想检查用户是否登录并显示对话框(您已成功注册/登录,或密码不匹配/错误等)。如果成功,我想将用户导航到应用程序的主页。你能告诉我如何实现这个目标吗?感谢您的支持。
到目前为止已完成的工作:
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 :(得分:0)
它将添加一个名为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());
}