Hi team! im messing around creting a basic login function using Firebase and ASP.Net,
I have my login ActionResult inside the HomeController
but whenever i access the Login Page, the @Html.ValidationMessageFor message is being displayed when there's no input on the @Html.EditorFor,
i've already tried using CSS to hide it, but then it wont pop up again, i need this validation to show up after the Login button is clicked, hope someone can help me out, please check the code
HomeController
------------------------------------------------------------
public ActionResult Index()
{
return View();
}
public ActionResult Contact()
{
ViewBag.Message = "Your contact page.";
return View();
}
public async Task
{
try
{
if (ModelState.IsValid)
{
var auth = new FirebaseAuthProvider(new FirebaseConfig(apiKEY));
var a = await auth.SignInWithEmailAndPasswordAsync(model.Email, model.Password);
string token = a.FirebaseToken;
var user = a.User;
if (token != "")
{
SignInUser(user.Email, token, false);
return RedirectToLocal(returnURL);
}
else
{
ModelState.AddModelError(string.Empty, "Error email o contraseña incorrecta.");
}
}
}
catch (FirebaseAuthException ex)
{
Response.Write("");
//throw new Exception(ex.Message);
}
return this.View(model);
}
private void SignInUser(string email, string token, bool isPersistent)
{
// Initialization.
var claims = new List
try
{
// Setting
claims.Add(new Claim(ClaimTypes.Email, email));
claims.Add(new Claim(ClaimTypes.Authentication, token));
var claimIdenties = new ClaimsIdentity(claims, DefaultAuthenticationTypes.ApplicationCookie);
var ctx = Request.GetOwinContext();
var authenticationManager = ctx.Authentication;
// Sign In.
authenticationManager.SignIn(new Microsoft.Owin.Security.AuthenticationProperties() { IsPersistent = isPersistent }, claimIdenties);
}
catch (Exception ex)
{
// Info
throw ex;
}
}
private ActionResult RedirectToLocal(string returnUrl)
{
try
{
// Verification.
if (Url.IsLocalUrl(returnUrl))
{
// Info.
return Redirect(returnUrl);
}
}
catch (Exception ex)
{
// Info
throw ex;
}
// Info.
return this.RedirectToAction("Index", "Home");
}
----------------------------------------------------
View
----------------------------------------------------
@{
ViewBag.Title = "Login";
}
Login
@using (Html.BeginForm("Login", "Home", FormMethod.Post))
{
@Html.AntiForgeryToken()
User
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
@Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
@Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
}
@Html.ActionLink("Back to List", "Index")
Sachin SinghPosted May 1, 2022, 12:20 PM