I'm using MVC 4 and I'm trying to added a new field to the UserProfile named AddedById (for house keeping).
Here is what I have:
WebSecurity.CreateUserAndAccount(model.UserName, model.Password, new
{
FirstName = model.FirstName,
LastName = model.LastName,
AddedDate = DateTime.UtcNow
AddedByID = // to sure how to get the id because the account has not been created yet.
});
Should I just update the AddById after the account is created?
Because a support team could create the account for someone.
If that the case, make sure that the AddedByID
is Nullable
, then have the Support Team
be authenticated first before they create a User
.
Then, make your Register Action
look like this;
//
// POST: /Account/Register
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// Attempt to register the user
using (EfDb db = new EfDb())
{
try
{
var userProfile = db.UserProfiles.Local.SingleOrDefault(u => u.UserName == User.Identity.Name)
?? db.UserProfiles.SingleOrDefault(u => u.UserName == User.Identity.Name);
WebSecurity.CreateUserAndAccount(
model.UserName,
model.Password,
new
{
FirstName = model.FirstName,
LastName = model.LastName,
AddedDate = DateTime.UtcNow,
AddedByID = userProfile.UserId
}
);
catch (MembershipCreateUserException e)
{
ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
}
}
}
// If we got this far, something failed, redisplay form
return View(model);
}