Simplemembership - add an email field and use as a login

I am trying to do two things here:

1. Adding an email field to the UserProfile table (default). It works like a charm. Users can register both a username and an email address.

2. Change the username to use email instead of username, it also works like a charm.

Here is the problem. The above only works when I separate them, as soon as I use both of them the registration process ends with the following message :

Cannot insert the value NULL into column 'UserName', table 'opdb.dbo.UserProfile'; column does 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 follow the manual found here . In the comments section, I see that others have the same problem, however, the solution is not to set the UserName field to valid values, as indicated in the answers.

The strange thing is that everything works until I use both together. Any ideas? It drives me crazy!

Thank you in advance

Change Could there be a column order in the database? What should the field used for identification be the first column in the table? So, if I want email as an identifier, which should be the first column in the table?

+4
source share
2 answers

This is not a column order. I think you did not set your WebSecurity.InitializeDatabaseConnection correctly. It seems that when you add an email field, it is treated as an email field (rather than a natural key for users), and when you change the username to use email, do you actually insert the email into the UserName column?

Anyway, just get the job done. Assuming you already have an email column in the database and you are using a standard web application template, do the following:

  • Change the UserProfile model to add the Email property, do the same with RegisterModel and change the Login model from UserName to Email
  • Update the InitializeSimpleMembershipAttribute to use Email instead of UserName as the natural key for users.
  • Update your AccountController Login and Register actions so that you register your username and email during registration and use your email at login
  • Refresh Views

So the actual changes:

  • Models /AccountModels.cs
    • class UserProfile : add property public string Email { get; set; } public string Email { get; set; }
    • class LoginModel : change the property name from UserName to Email
    • class RegisterModel : add the property public string Email { get; set; } public string Email { get; set; } public string Email { get; set; } and set its attributes to Required , set Display(Name , etc. (maximum length, if you want to apply this as well
  • AccountController changes
    • Login method:
      • Change model.UserName to model.Email in the line if (ModelState.IsValid && WebSecurity.Login(model.UserName ...
    • Register method:
      • Change WebSecurity.CreateUserAndAccount(model.UserName, model.Password); on WebSecurity.CreateUserAndAccount(model.Email, model.Password, new { UserName = model.UserName });
      • EDIT - Missed change: Change WebSecurity.Login(model.UserName, model.Password); on WebSecurity.Login(model.Email, model.Password);
  • Filters /InitializeSimpleMembershipAttribute.cs changes
    • Change "UserName" to "Email" in WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: true);
  • Views / Accounts / Login.cshtml
    • Change three use m.UserName to m.Email
  • Views / accounts / Register.cshtml
    • Copy and paste the UserName <li> section and change the duplicates of m.UserName to m.Email

In the MVC 4 vanilla web application project, these changes will work fine (in a true vanilla project, you may have to apply migrations if you want to use them and you don't edit the database to add an E-mail column).

+5
source

I'm not sure the Andy Browns suggestion will work, because WebSecurity links in the WebMatrix.WebData namespace will not work properly. They specifically consider the UserName field to perform their actions. I could be wrong, but this is my perception. If you want to create a separate field, create another username (Name) and run your own code to update it so that it reflects on the user.

I decided to create a login view that renamed UserName to User Email

 <li> @Html.Label("Email Address:") @Html.TextBoxFor(m => m.UserName) </li> 

and then in the Login and Register models it is simply required that the username be entered as an email format using:

 [Required] [DataType(DataType.EmailAddress)] [EmailAddress] [Display(Name = "UserEmail")] public string UserName { get; set; } 

Maybe too simplistic, but it works for me.

+2
source

Source: https://habr.com/ru/post/1480565/


All Articles