Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- $("#btnLogin").click(function () {
- var emailModal = $("#txtEmail").val();
- var senhaModal = $("#txtSenha").val();
- var remembermeModal = false;
- $.ajax({
- url: "/Agendamento/LoginModal",
- type: "POST",
- data: {
- "email": emailModal,
- "senha": senhaModal,
- "rememberme": remembermeModal
- },
- success: function (data) {
- $("#LoginTeste").html(data);
- }
- })
- public async Task<IActionResult> LoginModal(string email, string senha, bool rememberme)
- {
- var model = new AgendamentoViemModel();
- model.Login.Email = email;
- model.Login.Password = senha;
- model.Login.RememberMe = rememberme;
- if (model.Login.Email.IndexOf('@') > -1)
- {
- //Validate email format
- string emailRegex = @"^([a-zA-Z0-9_-.]+)@(([[0-9]{1,3}" +
- @".[0-9]{1,3}.[0-9]{1,3}.)|(([a-zA-Z0-9-]+" +
- @".)+))([a-zA-Z]{2,4}|[0-9]{1,3})(]?)$";
- Regex re = new Regex(emailRegex);
- if (!re.IsMatch(model.Login.Email))
- {
- ModelState.AddModelError("Email", "Email is not valid");
- }
- }
- else
- {
- //validate Username format
- string emailRegex = @"^[a-zA-Z0-9]*$";
- Regex re = new Regex(emailRegex);
- if (!re.IsMatch(model.Login.Email))
- {
- ModelState.AddModelError("Email", "Username is not valid");
- }
- }
- if (ModelState.IsValid)
- {
- var userName = model.Login.Email;
- if (userName.IndexOf('@') > -1)
- {
- var user = await _userManager.FindByEmailAsync(model.Login.Email);
- if (user == null)
- {
- ModelState.AddModelError(string.Empty, "Invalid login attempt.");
- return View(model);
- }
- else
- {
- userName = user.UserName;
- }
- }
- var result = await _signInManager.PasswordSignInAsync(userName, model.Login.Password, model.Login.RememberMe, lockoutOnFailure: false);
- if (result.Succeeded)
- {
- _logger.LogInformation("User logged in.");
- return PartialView("_LoginPartial", model);
- }
- else
- {
- return new StatusCodeResult(400);
- }
- }
- return View(model);
- }
- <div id="LoginTeste">
- @await Html.PartialAsync("_LoginPartial")
- <div>
- </div>
- </div>
Add Comment
Please, Sign In to add comment