c# - Confirm email doesn't work -
i'm working asp.net mvc5 project , want users confirm email before login webpage. did manage when user register user email. did manage make when user clicks link in email confirmed in database.
so, problem. want them confirm email before can login. here code trying achieve this.
// post: /account/login [httppost] [allowanonymous] [validateantiforgerytoken] public async task<actionresult> login(loginviewmodel model, string returnurl) { if (!modelstate.isvalid) { var user = await usermanager.findasync(model.username, model.password); if (user != null) { if (user.emailconfirmed == true) { await signinasync(user, model.rememberme); return redirecttolocal(returnurl); } else { modelstate.addmodelerror("", "confirm email address."); } } else { modelstate.addmodelerror("", "invalid username or password."); } } // doesn't count login failures towards account lockout // enable password failures trigger account lockout, change shouldlockout: true var result = await signinmanager.passwordsigninasync(model.username, 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); } }
but user can login anyway without confirming email.
and here how send email.
if (modelstate.isvalid) { var user = new applicationuser() { username = model.username }; user.email = model.email; user.emailconfirmed = false; var result = await usermanager.createasync(user, model.password); if (result.succeeded) { mailmessage m = new mailmessage( new mailaddress("noreply@stuff.net", "web registration"), new mailaddress(user.email)); m.subject = "email confirmation"; m.body = string.format("dear {0}<br/>thank registration, please click on below link complete registration: <a href=\"{1}\" title=\"user email confirm\">{1}</a>", user.username, url.action("confirmemail", "account", new { token = user.id, email = user.email }, request.url.scheme)); m.isbodyhtml = true; smtpclient smtp = new smtpclient("mail.stuff.net"); smtp.credentials = new networkcredential("noreply@stuff.net", "passwordstuff"); smtp.enablessl = false; smtp.port = 8889; smtp.send(m); return redirecttoaction("confirmemail", "account", new { email = user.email }); } else { adderrors(result); } }
regards. carlsson.
code
var user = await usermanager.findasync(model.username, model.password); if (user != null) { if (user.emailconfirmed == true) { await signinasync(user, model.rememberme); return redirecttolocal(returnurl); } else { modelstate.addmodelerror("", "confirm email address."); } } else { modelstate.addmodelerror("", "invalid username or password."); }
has execute if modelstate.isvalid! alternative performed, when form data incorrect
try this:
if (!modelstate.isvalid) { return view(); } var user = await usermanager.findasync() ... ...
Comments
Post a Comment