c# - How do I change the ASP.NET MVC default login page to login by username instead of email address? -
i'll go on investigation , guys can figure out idea should next.
the relevant m, v , c login
public class loginviewmodel { [required] [display(name = "email")] [emailaddress] public string email { get; set; } [required] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [display(name = "remember me?")] public bool rememberme { get; set; } }
and
@using (html.beginform("login", "account", new { returnurl = viewbag.returnurl }, formmethod.post, new { @class = "form-horizontal", role = "form" })) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(m => m.email, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.textboxfor(m => m.email, new { @class = "form-control" }) @html.validationmessagefor(m => m.email, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(m => m.password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.passwordfor(m => m.password, new { @class = "form-control" }) @html.validationmessagefor(m => m.password, "", new { @class = "text-danger" }) </div> </div> <div class="form-group" style="margin-bottom: 40px;"> <div class="col-md-10"> <div class="checkbox"> @html.checkboxfor(m => m.rememberme) @html.labelfor(m => m.rememberme) </div> </div> </div> <div class="form-group"> <div class="col-md-10"> <input type="submit" value="log in" class="btn btn-default" style="padding-left: 40px; padding-right: 40px;" /> </div> </div> }
and
[httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> login(loginviewmodel model, string returnurl) { if (!modelstate.isvalid) { return view(model); } // doesn't count login failures towards account lockout // enable password failures trigger account lockout, change shouldlockout: true var result = await signinmanager.passwordsigninasync(model.email, model.password, model.rememberme, shouldlockout: false); switch (result) { case signinstatus.success: return redirecttolocal(returnurl); case signinstatus.lockedout: return view("lockout"); case signinstatus.requiresverification: return redirecttoaction("sendcode", new { returnurl = returnurl, rememberme = model.rememberme }); case signinstatus.failure: default: modelstate.addmodelerror("", "invalid login attempt."); return view(model); } }
what i'm trying have login username rather email address. while can't find in mvc template source code pertaining username, looked in aspnetusers
table template , see there indeed column called username
. trick try , change above source code want without causing side effects elsewhere in mvc template.
i changed m
public class loginviewmodel { [required] [display(name = "username")] public string username { get; set; } [required] [datatype(datatype.password)] [display(name = "password")] public string password { get; set; } [display(name = "remember me?")] public bool rememberme { get; set; } }
and v
@using (html.beginform("login", "account", new { returnurl = viewbag.returnurl }, formmethod.post, new { @class = "form-horizontal", role = "form" })) { @html.antiforgerytoken() @html.validationsummary(true, "", new { @class = "text-danger" }) <div class="form-group"> @html.labelfor(m => m.username, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.textboxfor(m => m.username, new { @class = "form-control" }) @html.validationmessagefor(m => m.username, "", new { @class = "text-danger" }) </div> </div> <div class="form-group"> @html.labelfor(m => m.password, new { @class = "col-md-2 control-label" }) <div class="col-md-10"> @html.passwordfor(m => m.password, new { @class = "form-control" }) @html.validationmessagefor(m => m.password, "", new { @class = "text-danger" }) </div> </div> <div class="form-group" style="margin-bottom: 40px;"> <div class="col-md-10"> <div class="checkbox"> @html.checkboxfor(m => m.rememberme) @html.labelfor(m => m.rememberme) </div> </div> </div> <div class="form-group"> <div class="col-md-10"> <input type="submit" value="log in" class="btn btn-default" style="padding-left: 40px; padding-right: 40px;" /> </div> </div> }
the changing of c stuff i'm stuck on. know changing
var result = await signinmanager.passwordsigninasync(model.email, model.password, model.rememberme, shouldlockout: false);
to
var email = pingdatabaseandgetemailfromusername(model.username); var result = await signinmanager.passwordsigninasync(email, model.password, model.rememberme, shouldlockout: false);
but, there has better way, right?
also, it's possible screwed other stuff modifying loginviewmodel
because other bits of code in mvc template might use , expect have email
field.
so should doing here? want simplest change.
if have change passwordsigninasync
, implementation? can't find it.
the default template implementation uses email address username, why it's sending model.email
value passwordsigninasync
function. assuming created user accounts separate email
, username
values, should simple changing sign in method to:
var result = await signinmanager.passwordsigninasync(model.username, model.password, model.rememberme, shouldlockout: false);
again, assumes user accounts created distinct username
, email
values.
Comments
Post a Comment