配置文件提供程序不在MVC4 ASP.net中工作

时间:2012-09-21 19:11:37

标签: asp.net-mvc-4 asp.net-profiles

我的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'。

编辑: 作为下面优秀答案的附录。这个link有助于添加更多解释。帮助澄清了我剩下的问题。

1 个答案:

答案 0 :(得分:9)

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

  1. 使用Internet模板
  2. 创建新的ASp.NET MVC 4应用程序
  3. 导航到~/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; }
    }
    
  4. 将新字段添加到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; }
    }
    
  5. 修改~/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")
    }
    
  6. 更新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);
    }
    
  7. Ctrl + F5 启动应用程序并创建新用户

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

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

    using (var context = new UsersContext())
    {
        UserProfile profile = context.UserProfiles.First(x => x.UserName == User.Identity.Name);
        // TODO: do something with the user profile
    }