Reputation: 5564
I'm trying to write my own account registration logic in an Mvc 4 application. I started with a basic template instead of the internet template. At the moment I have a Tenant
model with a number of properties. Using code first I've created a tenant table in my database (there will be more models and tables added over time - I'm just trying to get one working first).
I've created a RegisterTenantModel - similar to the one in the internet template except I'm using email instead of username.
public class TenantRegisterModel
{
[Required]
[Display(Name="Email")]
public string Email { get; set; }
[Required]
[StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
[DataType(DataType.Password)]
[Display(Name = "Password")]
public string Password { get; set; }
[DataType(DataType.Password)]
[Display(Name = "Confirm password")]
[Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
public string ConfirmPassword { get; set; }
}
In my RegisterTenant method below I want to:
TenantRegisterModel
.On calling Membership.CreateUser
, how do I know that model.Email
and model.Password
will be added to the Tenant
table? Likewise, if I was doing the same scenario as described above for a Landlord
, how would I know that the details entered are being added to the Landlord
table? Is there a way with Membership.CreateUser
I can specify what table to add the user's details too?
RegisterTenant:
public ActionResult RegisterTenant(TenantRegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register a tenant account
MembershipCreateStatus createStatus;
Membership.CreateUser(model.Email, model.Password);
// ...
}
}
I of course have a context class, which at the moment has public DbSet<LetLord.Models.Tenant> Tenants { get; set; }
, so how do I know what table a user's details are being added if I had multiple DbSets?
Upvotes: 2
Views: 164
Reputation: 5564
How do I know that model.Email and model.Password will be added to the Tenant table?
I added additional properties to the UserProfile model. Then in the CreateUserAndAccount
method I dynamically passed the properties in as follows:
WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
new { Email = model.Email,
FirstName = model.FirstName,
LastName = model.LastName,
AccountType = model.AccountType,
DaytimeContactNumber = "",
EveningContactNumber = "" },
false);
Likewise, if I was doing the same scenario as described above for a Landlord, how would I know that the details entered are being added to the Landlord table? Is there a way with Membership.CreateUser I can specify what table to add the user's details too?
I created roles for landlords and tenants, and in Register
, just after CreateUserAndAccount
I checked the value of enum AccountType
:
if (model.AccountType == AccountType.Tenant)
{
try
{
Roles.AddUserToRole(model.UserName, "Tenant");
}
catch (Exception e)
{
ModelState.AddModelError("Unable to add user to role", e);
}
}
Upvotes: 0
Reputation: 157
MVC 4 Does not use the same membership as MVC 3. You want to look up simplemembership. This is actually a very good solution to the common issue of storing additional data for a user.
this is a good place to start reading about it. Not sure how familiar you are with the MVC3 Membership Provider and Database Schema but this has changed a lot in MVC4. It took me a while to find everything.
Basically with the new setup you can create your own user table with any info you want and simply map 1 column of your database for the id (which is now an int and not a (guid) like the mvc3 membership provider and schema) and the "username" which can be an email or any unique identifier like that
WebSecurity.InitializeDatabaseConnection("DefaultConnection", "UserProfile", "UserId", "UserName", autoCreateTables: false);
You link up your connection string to the database and it will create the membership schema with that database. Then you can add the model to your context and use Entity framework for functions like dbcontext.UserProfile.FirstName and such.
here is some more starting point material
http://patrickdesjardins.com/blog/asp-net-membership-with-mvc4
good luck
Upvotes: 2
Reputation: 4974
You should probably implement Membership
by yourself and register it as MembershipProvider
in your web.config. This way you have full control what happens with the data and you still have all the nice features.
There is a sample project in MVC3 that implements a Code-first Membership- and Roleprovider which would give you an idea about how it's done.
Alternatively, you could use the ProfileProvider to save some activation key (and your other custom user fields) and add the user to some role like "NotActivated"
.
Update: to my knowledge, the standard Membership-Provider uses the DefaultConnection in the web.config. If you use Code-First in the same database, it will break because there's a structure that is not made from code-first already. You could use database-first alternatively, I think.
Upvotes: 1