asp.net core - Validation Messages not showing on login page -
i have controller called account controller houses login function. problem having going straight index page instead of displaying model error invalid login.
in aps.net web forms used validation group , validationsummary same required here
[httppost] public async task<iactionresult> login(string email, string password, bool rememberme) { var user = await _usermanager.findbyemailasync(email); if (user == null) { modelstate.addmodelerror(string.empty, "invalid login"); return view(); } if (!user.emailconfirmed) { modelstate.addmodelerror(string.empty, "confirm email first"); return view(); } var passwordsigninresult = await _signinmanager.passwordsigninasync(user, password, ispersistent: rememberme, lockoutonfailure: false); if (!passwordsigninresult.succeeded) { await _usermanager.accessfailedasync(user); modelstate.addmodelerror(string.empty, "invalid login"); return view(); } return redirect("~/"); } login page
<form asp-controller="account" asp-action="login" method="post"> <div class="form-group has-feedback"> <input type="email" class="form-control" name="email" id="email" placeholder="email"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> <div class="form-group has-feedback"> <input type="password" class="form-control" name="password" id="password" placeholder="password"> <span class="glyphicon glyphicon-lock form-control-feedback"></span> </div> <div class="row"> <div class="col-xs-8"> <div class="checkbox icheck"> <label> <input type="checkbox" name="rememberme" value="true"> remember me <input type="hidden" name="rememberme" value="false" /> </label> </div> </div> <!-- /.col --> <div class="col-xs-4"> <button type="submit" class="btn btn-primary btn-block btn-flat">sign in</button> </div> <!-- /.col --> </div> </form> my startup.cs configure in case missing.
// method gets called runtime. use method configure http request pipeline. public void configure(iapplicationbuilder app, ihostingenvironment env, iloggerfactory loggerfactory) { loggerfactory.addconsole(configuration.getsection("logging")); loggerfactory.adddebug(); if (env.isdevelopment()) { app.usedeveloperexceptionpage(); } else { app.useexceptionhandler("/home/error"); } app.usestaticfiles(); app.useidentity(); app.usemvc(routes => { routes.maproute( name: "default", template: "{controller=home}/{action=index}/{id?}"); }); }
tried code , works fine , shows index page when no errors added modelstate.
validationsummary still present in asp.net core can use below.
<form asp-controller="account" asp-action="login" method="post"> <div asp-validation-summary="modelonly" class="text-danger"></div> <div class="form-group has-feedback"> <input type="email" class="form-control" name="email" id="email" placeholder="email"> <span class="glyphicon glyphicon-envelope form-control-feedback"></span> </div> ... </form> when used code, errors displayed on page without issues. 
but suggestion can improve code bit , instead of many return view(); in code use code in end of method before return redirect("~/")
if (!modelstate.isvalid) return view(); update: there full code of login method.
[httppost] public async task<iactionresult> login(string email, string password, bool rememberme) { identityuser user = await _usermanager.findbyemailasync(email); if (user == null) { modelstate.addmodelerror(string.empty, "invalid login"); return view();//this should kept because if user null, passwordsigninasync generate exception } if (!user.emailconfirmed) { modelstate.addmodelerror(string.empty, "confirm email first"); } var passwordsigninresult = await _signinmanager.passwordsigninasync(user, password, ispersistent: rememberme, lockoutonfailure: false); if (!passwordsigninresult.succeeded) { await _usermanager.accessfailedasync(user); modelstate.addmodelerror(string.empty, "invalid login"); } if (!modelstate.isvalid) return view(); return redirect("~/"); } update2: easiest way, in opinion, handle empty emails in case using required attribute in view email field below.
<input required type="email" class="form-control" name="email" id="email" placeholder="email"> this required attribute specifies user must fill in value before submitting form, without entering value email not able submit form, therefore not exception. can read more required attribute here , plus link form validation(see required attribute section).
Comments
Post a Comment