自定义简单成员资格API

时间:2013-12-29 15:53:47

标签: c# asp.net-mvc-4 simplemembership razor-2

我已经自定义了ASP.NET Simple Membership API,并从Account Controller到我的User类调用了Web安全性。这是控制器动作

[HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult Create(User user)
    {
        if (ModelState.IsValid)
        {
           try
            {
                WebSecurity.CreateUserAndAccount(user.UserName, user.Password, 
                    new{FirstName = user.FirstName, 
                        LastName= user.LastName, 
                        DateOfBirth= user.DateOfBirth, 
                        IsMale = user.IsMale, 
                        IsClient = user.IsClient,
                        Email = user.Email});

                WebSecurity.Login(user.UserName, user.Password);
                return RedirectToAction("Landing", "User");
            }
            catch (MembershipCreateUserException e)
            {
                ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
            }

           db.Users.Add(user);
           db.SaveChanges();

        }

        ViewBag.NationalityId = new SelectList(db.Nationalities, "NationalityId", "NationalityName", user.NationalityId);
        ViewBag.UserId = new SelectList(db.UserCardDetails, "UserCardDetailId", "CreditCardNumber", user.UserId);
        return View(user);
    }

这是我的用户类

public class User
    {
        public int UserId { get; set; }

        public int? NationalityId { get; set; }

        [StringLength(100)]
        public string UserName { get; set; }


        [Display(Name = "First Name")]
        [StringLength(100)]
        public string FirstName { get; set; }


        [Display(Name = "Last Name")]
        [StringLength(100)]
        public string LastName { get; set; }

        [EmailAddress]
        [StringLength(256)]
        [Display(Name = "Email ID")]
        public string Email { get; set; }

        [Display(Name = "Gender")]
        public bool? IsMale { get; set; }

        [Display(Name="Are you a designer or a client?")]
        public bool? IsClient { get; set; }


        [Display(Name = "Nationality")]
        [ForeignKey("NationalityId")]
        public virtual Nationality Nationality { get; set; }


        [Display(Name = "Date Of Birth")]
        public DateTime? DateOfBirth { 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; }



        public virtual ICollection<Project> Projects { get; set; }

        public virtual ICollection<Role> Roles { get; set; }

        public virtual UserCardDetail UserCardDetail { get; set; }


        public User()
        {
            this.Roles = new List<Role>();
            this.Projects = new List<Project>();
            this.IsMale = true;
            this.IsClient = true;
        }

    }

当我尝试通过表单发布数据时,我得到的异常表示密码不能为空。但是我正在填写密码。

1 个答案:

答案 0 :(得分:0)

想想我刚刚发现了一个修复程序,我添加了一个额外的Property Password = user.Password并且它有效。