vb.net - Is this the correct approach to customizing the ASP.Net MVC 4 template Login -
while mvc4 template provided microsoft useful, feel there few scenarios should covered out users trying log in.
- allow user log in email address instead of user name (they can still choose use user name). former easier remember.
- if don't have local account , try use one, check if have used external provider (such google) log in , let them know use instead.
- if have registered account locally have not yet confirmed email, let them know. current template warns username or password wrong.
maybe i'm over-thinking it, want provide user every opportunity log in , use site. here code wrote add these functions. wrote in vb, including c# tag since majority of mvc user's here seem favor , vb easy read. correct approach add these options? there glaring errors code, aside fact can refactor it? thank you.
<httppost()> _ <allowanonymous()> _ <validateantiforgerytoken()> _ public function login(byval model loginmodel, byval returnurl string) actionresult if modelstate.isvalid if isemail(model.username) 'the username email address dim username = getusernamebyemail(model.username) if username isnot nothing if websecurity.login(username, model.password, persistcookie:=model.rememberme) return redirecttolocal(returnurl) end if 'check if there local account dim localid = getuseridbyemail(model.username) if localid nothing 'no local account means username wrong modelstate.addmodelerror("", "the user name or password provided incorrect.") else if not oauthwebsecurity.haslocalaccount(localid) 'registered via external provider modelstate.addmodelerror("", "please login external provider have used.") else if not websecurity.isconfirmed(model.username) 'has local account, hasn't confirmed email modelstate.addmodelerror("", "you have not yet confirmed email.") else 'password wrong modelstate.addmodelerror("", "the user name or password provided incorrect.") end if end if end if else modelstate.addmodelerror("", "the email entered incorrect.") end if else 'must regular user name, log in normal if websecurity.login(model.username, model.password, persistcookie:=model.rememberme) return redirecttolocal(returnurl) end if 'check if there local account dim localid = getuseridbyusername(model.username) if localid nothing 'no local account means username wrong modelstate.addmodelerror("", "the user name or password provided incorrect.") else if not oauthwebsecurity.haslocalaccount(localid) 'registered via external provider modelstate.addmodelerror("", "please login external provider have used.") else if not websecurity.isconfirmed(model.username) 'has local account, hasn't confirmed email modelstate.addmodelerror("", "you have not yet confirmed email.") else 'password wrong modelstate.addmodelerror("", "the user name or password provided incorrect.") end if end if end if end if end if return view(model) end function 'check if input email address public function isemail(byval input string) boolean return regex.ismatch(input, "\a(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?)\z") end function public function getusernamebyemail(byval email string) string dim username string = nothing dim conn sqlconnection = new sqlconnection(system.configuration.configurationmanager.connectionstrings("defaultconnection").connectionstring) dim cmd sqlcommand = new sqlcommand("select username user_info email = @email", conn) cmd.parameters.add(new sqlparameter("@email", system.data.sqldbtype.nvarchar)) cmd.parameters("@email").value = email conn.open() dim reader sqldatareader = cmd.executereader() try while reader.read username = reader(0) end while reader.close() end try conn.close() return username end function public function getuseridbyemail(byval email string) integer? dim userid integer? dim conn sqlconnection = new sqlconnection(system.configuration.configurationmanager.connectionstrings("defaultconnection").connectionstring) dim cmd sqlcommand = new sqlcommand("select userid user_info email = @email", conn) cmd.parameters.add(new sqlparameter("@email", system.data.sqldbtype.nvarchar)) cmd.parameters("@email").value = email conn.open() dim reader sqldatareader = cmd.executereader() try while reader.read userid = reader(0) end while reader.close() end try conn.close() return userid end function public function getuseridbyusername(byval username string) integer? dim userid integer? dim conn sqlconnection = new sqlconnection(system.configuration.configurationmanager.connectionstrings("defaultconnection").connectionstring) dim cmd sqlcommand = new sqlcommand("select userid user_info username = @username", conn) cmd.parameters.add(new sqlparameter("@username", system.data.sqldbtype.nvarchar)) cmd.parameters("@username").value = username conn.open() dim reader sqldatareader = cmd.executereader() try while reader.read userid = reader(0) end while reader.close() end try conn.close() return userid end function
Comments
Post a Comment