2

使用 MVC 4,Simplemembership

我正在构建一个供企业使用的 Web 应用程序,因此不希望开箱即用的 MVC 4 模板功能允许用户注册。相反,我想要一个只允许管理员访问然后创建用户的创建用户视图。我希望我可以更改注册控制器上的安全性,然后让当前登录的用户使用现有的注册视图创建用户......没有骰子。

这是控制器中的代码。我没有从模板中更改它:

// POST: /Account/Register

        [HttpPost]
        //[AllowAnonymous]
        [ValidateAntiForgeryToken]
        public ActionResult Register(RegisterModel model)
        {
            if (ModelState.IsValid)
            {
                // Attempt to register the user
                try
                {
                    WebSecurity.CreateUserAndAccount(
                        model.UserName,
                        model.Password,
                        new { FirstName = model.FirstName, LastName = model.LastName, Email = model.Email },
                        false
                    );
                    WebSecurity.Login(model.UserName, model.Password);
                    return RedirectToAction("Index", "Home");
                }
                catch (MembershipCreateUserException e)
                {
                    ModelState.AddModelError("", ErrorCodeToString(e.StatusCode));
                }
            }

            // If we got this far, something failed, redisplay form
            return View(model);
        }

当当前登录的用户尝试通过此创建新用户时,您会收到错误消息(@ 第 86 行):

The INSERT statement conflicted with the FOREIGN KEY constraint "FK_UserProfile_webpages_Membership". The conflict occurred in database "OTIS", table "dbo.webpages_Membership", column 'UserId'.
The statement has been terminated.

Line 84:                 try
Line 85:                 {
Line 86:                     WebSecurity.CreateUserAndAccount(
Line 87:                         model.UserName,
Line 88:                         model.Password,

Hwre 是 UserProfile 类

[Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }

        [Required]
        [Display(Name = "User name")]
        public string UserName { get; set; }

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

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

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

WebSecurity.CurrentUserID 填充了当前登录的用户,我认为这是导致问题的原因。使用 simplemembership 提供程序实现此目的的正确方法是什么?

编辑:我尝试将其移回允许匿名用户注册,因此当我调试它时 WebSecurity.CurrentUserID 为-1,但我仍然遇到相同的错误。如果类中的 UserId 字段用属性​​ [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)] 修饰,如何生成重复键?

4

3 回答 3

2

我遇到了同样的问题,但我使用提供程序中的 CreateUserAndAccount 方法而不是 WebSecurity 解决了它。

var membership = (SimpleMembershipProvider)Membership.Provider;
membership.CreateUserAndAccount(
    model.UserName,
    model.Password,
    new Dictionary<string, object>{
        { "FirstName", model.FirstName}, 
        { "LastName", model.LastName},
        { "Email", model.Email }
    }
);
于 2013-03-11T08:06:14.573 回答
1

Finally got back to this issue. The problem for me was that my database relationships were corrupted. The UserProfile table to Membership table foreign key constraints were set such that the UserId column was noted as the primary key in BOTH tables. As such, when it tried to add a row in UserProfile, it's newly generated ID did not exist in Membership, which caused the error. Simply deleting the relationship and recreating correctly such that UserProfile.UserId was the primary fixed it.

于 2013-04-25T19:29:21.267 回答
0

This was all I needed to do. I wouldn't think there is anything wrong with UserProfile or Membership tables.

 WebSecurity.CreateUserAndAccount(model.UserName, model.Password,
                                  new { Email=model.Email});
于 2015-06-16T02:23:45.743 回答