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が作成されますが、プロバイダーをユーザーとロールとともにPa.mdfdbに保存する必要があります。プロファイルを取得してユーザーアカウントおよびロールと同じDBに保存する方法に関するアイデアはありますか?

編集:使用する場合

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

エラーが発生します-タイプ'System.Web.Providers.DefaultProfileProvider'を読み込めませんでした。

編集:以下の優れた答えへの補遺として。このリンクは、さらに説明を追加するのに役立ちました。私の残りの質問を解決するのに役立ちました。

4

1 に答える 1

9

ASP.NETMVC4は新しいを使用しSimpleMembershipProviderます。自動生成されたテンプレートとユーザープロファイルにカスタムフィールドを追加する方法は次のとおりです。

  1. インターネットテンプレートを使用して、新しいASp.NETMVC4アプリケーションを作成します
  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 に答える