1

我的 web.conf 文件如下:

    <connectionStrings>
    <add name="PaDBCon" connectionString="Data Source=(LocalDb)\v11.0;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|Pa.mdf" providerName="System.Data.SqlClient" />
  </connectionStrings>
...
<profile>
      <providers>
        <clear />
        <add name="ProfilesProvider"
             type="System.Web.Profile.SqlProfileProvider"
             connectionStringName="PaDBCon"
             applicationName="PaWeb" />
      </providers>
      <properties>
        <add name="FirstName" type="String"/>
        <add name="LastName" type="String"/>
        <add name="Site" type="String"/>
      </properties>
    </profile>

但是我收到一个错误“未找到配置文件默认提供程序”。如果我取出提供程序部分,它会创建一个 aspnetdb.mdf,它不会在 Visual Studio 中打开,但我希望提供程序与我的用户和角色一起存储在我的 Pa.mdf 数据库中。关于如何将配置文件保存在与用户帐户和角色相同的数据库中的任何想法?

编辑:使用时

<providers>
<clear />
<add name="ProfilesProvider"
             type="System.Web.Providers.DefaultProfileProvider"
             connectionStringName="PamperDBCon"
             applicationName="PamperWeb" />
      </providers>

我收到错误消息 - 无法加载类型“System.Web.Providers.DefaultProfileProvider”。

编辑:作为下面优秀答案的附录。此链接有助于添加更多解释。帮助解决了我剩下的问题。

4

1 回答 1

9

ASP.NET MVC 4 使用新的SimpleMembershipProvider. 以下是如何将自定义字段添加到自动生成的模板和用户配置文件。

  1. 使用 Internet 模板创建新的 ASp.NET MVC 4 应用程序
  2. 导航到~/Models/AccountModel.cs文件并将字段添加到UserProfile类中:

    [Table("UserProfile")]
    public class UserProfile
    {
        [Key]
        [DatabaseGeneratedAttribute(DatabaseGeneratedOption.Identity)]
        public int UserId { get; set; }
        public string UserName { get; set; }
    
        // New fields added to store profile information
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Site { get; set; }
    }
    
  3. 将新字段添加到RegisterModel视图模型:

    public class RegisterModel
    {
        [Required]
        [Display(Name = "User name")]
        public string UserName { 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; }
    
        // New fields added to store profile information
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Site { get; set; }
    }
    
  4. 修改~/Views/Account/Register.cshtml视图以允许用户在注册时输入这些附加字段:

    @model MvcApplication1.Models.RegisterModel
    @{
        ViewBag.Title = "Register";
    }
    
    <hgroup class="title">
        <h1>@ViewBag.Title.</h1>
        <h2>Create a new account.</h2>
    </hgroup>
    
    @using (Html.BeginForm()) {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary()
    
        <fieldset>
            <legend>Registration Form</legend>
            <ol>
                <li>
                    @Html.LabelFor(m => m.UserName)
                    @Html.TextBoxFor(m => m.UserName)
                </li>
                <li>
                    @Html.LabelFor(m => m.FirstName)
                    @Html.TextBoxFor(m => m.FirstName)
                </li>
                <li>
                    @Html.LabelFor(m => m.LastName)
                    @Html.TextBoxFor(m => m.LastName)
                </li>
                <li>
                    @Html.LabelFor(m => m.Site)
                    @Html.TextBoxFor(m => m.Site)
                </li>
                <li>
                    @Html.LabelFor(m => m.Password)
                    @Html.PasswordFor(m => m.Password)
                </li>
                <li>
                    @Html.LabelFor(m => m.ConfirmPassword)
                    @Html.PasswordFor(m => m.ConfirmPassword)
                </li>
            </ol>
            <input type="submit" value="Register" />
        </fieldset>
    }
    
    @section Scripts {
        @Scripts.Render("~/bundles/jqueryval")
    }
    
  5. 更新 AccountController 中的 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, Site = model.Site });
                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);
    }
    
  6. 点击Ctrl+F5启动应用程序并创建一个新用户

  7. 数据库中的UserProfile表将填充其他字段。

  8. 最后,当您需要访问某些用户(例如当前经过身份验证的用户)的这些属性时:

    using (var context = new UsersContext())
    {
        UserProfile profile = context.UserProfiles.First(x => x.UserName == User.Identity.Name);
        // TODO: do something with the user profile
    }
    
于 2012-09-24T15:46:38.793 回答