我想实现身份登录到我的跨平台的应用程序。我在我的应用程序中创建了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);
});
}
}
}
您有什么具体的问题?你似乎知道如何调用你的API方法,并显示一个对话框?你有什么特别的不明白? – Jason
我不明白如何检查用户登录/注册以及显示对话框给用户返回(“您注册”,“密码不匹配”,“错误的电子邮件格式”等) – eccoripo
您的登录/注册函数应该返回一个你需要存储的布尔值(使用Settings,如下面的建议)你可以很容易地使用内置的DisplayAlert或使用已经使用的Acr Dialogs包显示对话框 – Jason