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:

  1. change userprofile model add email property, same registermodel , change login model username email
  2. update initializesimplemembershipattribute use email instead of username natural key users
  3. update accountcontroller login , register actions capture username , email on registration, , use email on login
  4. update views

so, actual changes make are:

  • models/accountmodels.cs changes
    • class userprofile: add property public string email { get; set; }
    • class loginmodel: change property name username email
    • class registermodel: add property public string email { get; set; } , set attributes required, set display(name etc. (max length if want enforce well
  • accountcontroller changes
    • login method:
      • change model.username model.email in line if (modelstate.isvalid && websecurity.login(model.username ...
    • 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);
  • filters/initializesimplemembershipattribute.cs changes
    • change "username" "email" in websecurity.initializedatabaseconnection("defaultconnection", "userprofile", "userid", "username", autocreatetables: true);
  • views/account/login.cshtml
    • change 3 usages of m.username m.email
  • views/account/register.cshtml
    • copy , paste username <li> section , change duplicates m.username m.email

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

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -