asp.net - Simplemembership - adding email field and use as login-name -
i trying 2 things here:
1. adding email field (default)userprofile table. works charm. users can register both username , email.
2. change login use email instead of username, works charm.
here problem. above works when seperate them, use both of them the registration process fails following error message:
cannot insert value null column 'username', table 'opdb.dbo.userprofile'; column not allow nulls. insert fails. websecurity.createuserandaccount(model.username, model.password, new { email = model.email }); websecurity.login(model.email, model.password);
i more or less following guide found here, in comments section see others having same problem, however, solution not set username field allow nulls stated in replies.
the strange thing works long don't use both together. ideas? drives me crazy!
thanks in advance
edit: order of columns in database? field used identification needs first column in table? if want email ident need first column in table?
it not column order. think haven't set websecurity.initializedatabaseconnection
properly. sounds when add email field, treated email field (and not natural key users), , when change login use email, inserting email username column?
either way, simple make work. assuming have email column in database , using standard internet application template, following:
- change
userprofile
model addemail
property, sameregistermodel
, changelogin
modelusername
email
- update
initializesimplemembershipattribute
use email instead ofusername
natural key users - update
accountcontroller
login
,register
actions capture username , email on registration, , use email on login - update views
so, actual changes make are:
- models/accountmodels.cs changes
class userprofile
: add propertypublic string email { get; set; }
class loginmodel
: change property nameusername
email
class registermodel
: add propertypublic string email { get; set; }
, set attributesrequired
, setdisplay(name
etc. (max length if want enforce well
accountcontroller
changeslogin
method:- change
model.username
model.email
in lineif (modelstate.isvalid && websecurity.login(model.username ...
- change
register
method:- change
websecurity.createuserandaccount(model.username, model.password);
websecurity.createuserandaccount(model.email, model.password, new { username = model.username });
- edit - missed change: change
websecurity.login(model.username, model.password);
websecurity.login(model.email, model.password);
- change
- filters/initializesimplemembershipattribute.cs changes
- change
"username"
"email"
inwebsecurity.initializedatabaseconnection("defaultconnection", "userprofile", "userid", "username", autocreatetables: true);
- change
- views/account/login.cshtml
- change 3 usages of
m.username
m.email
- change 3 usages of
- views/account/register.cshtml
- copy , paste username
<li>
section , change duplicatesm.username
m.email
- copy , paste username
in vanilla internet application mvc 4 project, these changes work (in true vanilla project may have apply migrations if want use these , aren't editing database add email column).
Comments
Post a Comment