WebSecurity.CreateUserAndAccount propertyValues

柔情痞子 提交于 2019-12-21 04:17:16

问题


I'm writing an mvc 4 c# .net 4.5 website

I want to create a new company object and register a new user that is linked to that company.

My account model is:

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string EmailAddress { get; set; }
        public string PhoneNumber { get; set; }
        public bool MarketingEmailOptin { get; set; }
        public bool isDisabled { get; set; }
        public virtual Company CompanyICanEdit { get; set; } 
    }

If i call the following it adds the user fine but has null for the CompanyICanEdit field:

WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
                                 addCompanyViewModel.User.Password,
                                 propertyValues: new
                                 {
                                   FirstName = addCompanyViewModel.User.FirstName,
                                   LastName = addCompanyViewModel.User.LastName,
                                   EmailAddress = addCompanyViewModel.User.EmailAddress,
                                   PhoneNumber = addCompanyViewModel.User.PhoneNumber,
                                   MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
                                   isDisabled = false
                                 });

which i would expect as i am not assigning it anything.

i have tried adding (mycompany is a company object):

    WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
                                 addCompanyViewModel.User.Password,
                                 propertyValues: new
                                 {
                                   FirstName = addCompanyViewModel.User.FirstName,
                                   LastName = addCompanyViewModel.User.LastName,
                                   EmailAddress = addCompanyViewModel.User.EmailAddress,
                                   PhoneNumber = addCompanyViewModel.User.PhoneNumber,
                                   MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
                                   isDisabled = false,
                                   CompanyICanEdit = mycompany
                                 });

But i get an error saying it can't match the type.

How do i go about registering the user so that the CompanyICanEdit contains the CompanyId value of mycompany?

Any help will be appreciated. thanks


回答1:


Never worked out how to do it in 1 go, got round it by the following in the end if anyone has the same problem.

//
// POST: /BusinessManager/ManageCompanies/Add
[HttpPost]
public ActionResult Add(AddCompanyViewModel addCompanyViewModel)
{
    if (ModelState.IsValid)
    {
        // Create company and attempt to register the user
        try
        {
            WebSecurity.CreateUserAndAccount(addCompanyViewModel.User.UserName,
                                                addCompanyViewModel.User.Password,
                                                propertyValues: new
                                                {
                                                    FirstName = addCompanyViewModel.User.FirstName,
                                                    LastName = addCompanyViewModel.User.LastName,
                                                    EmailAddress = addCompanyViewModel.User.EmailAddress,
                                                    PhoneNumber = addCompanyViewModel.User.PhoneNumber,
                                                    MarketingEmailOptin = addCompanyViewModel.User.MarketingEmailOptin,
                                                    isDisabled = false
                                                });

            db.Companies.Add(addCompanyViewModel.Company);

            var newuser = db.UserProfiles.FirstOrDefault(u => u.UserName == addCompanyViewModel.User.UserName);
            if (newuser != null)
            {
                newuser.CompanyICanEdit = addCompanyViewModel.Company;
                db.Entry(newuser).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            else
            {
                ModelState.AddModelError("", "New user wasn't added");
            }                     
        }
        catch (MembershipCreateUserException e)
        {
            ModelState.AddModelError("", Mywebsite.Controllers.AccountController.ErrorCodeToString(e.StatusCode));
        }

    }

    return View(addCompanyViewModel);
}


来源:https://stackoverflow.com/questions/14912208/websecurity-createuserandaccount-propertyvalues

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!