Moso Blan

Moso Blan

  • NA
  • 2
  • 1.1k

ValidationMesssageFor always showing up

Apr 29 2022 6:42 AM

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<ActionResult> Login(Models.User model, string returnURL)
        {
            
            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("<script>alert('Por favor ingrese contraseña correcta')</script>");
                //throw new Exception(ex.Message);
            }
            
            return this.View(model);
        }
 private void SignInUser(string email, string token, bool isPersistent)
        {
            // Initialization.
            var claims = new List<Claim>();

            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";
    }
    <head>
        <link href="@Url.Content("~/Content/LoginStyleSheet.css")" rel="stylesheet" type="text/css"/>
    </head>
    
    <body>
            <h2>Login</h2>
    
            @using (Html.BeginForm("Login", "Home", FormMethod.Post))
            {
                @Html.AntiForgeryToken()
    
                <div class="form-horizontal">
                    <h4>User</h4>
                    <hr />
                    @Html.ValidationSummary(true, "", new { @class = "text-danger" })
                    <div class="form-group">
                        @Html.LabelFor(model => model.Email, htmlAttributes: new { @class = "control-label col-md-2" })
    
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Email, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Email, "", new { @class = "text-danger" })
                            
                        </div>
                        <div hidden>
    
                        </div>
                    </div>
    
                    <div class="form-group">
                        @Html.LabelFor(model => model.Password, htmlAttributes: new { @class = "control-label col-md-2" })
                        <div class="col-md-10">
                            @Html.EditorFor(model => model.Password, new { htmlAttributes = new { @class = "form-control" } })
                            @Html.ValidationMessageFor(model => model.Password, "", new { @class = "text-danger" })
                        </div>
    
                        
                    </div>
    
    
                    <div class="form-group">
                        <div class="col-md-offset-2 col-md-10">
                            <input type="submit" value="Login" class="btn btn-default" />
                        </div>
                    </div>
                </div>
    
    
            }
    
            <div>
                @Html.ActionLink("Back to List", "Index")
            </div>
            <script src="~/Scripts/jquery-1.10.2.min.js"></script>
            <script src="~/Scripts/jquery.validate.min.js"></script>
            <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script>
        </body>

 

 

 

 

 

 


Answers (1)