ASP.NET MVC使用User Profiler

1.預設在ASP.NET MVC不會有System.Web.Providers,所以需在Nuget主控台輸入
install-package "System.Web.Providers"

2.設定組態
<profile defaultProvider="DefaultProfileProvider">

      <providers>

        <add name="DefaultProfileProvider" type="System.Web.Providers.DefaultProfileProvider, System.Web.Providers, Version=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" connectionStringName="DefaultConnection" applicationName="/" />

      </providers>

     </profile>

3.定義資訊



<properties>

   <add name="ForeNames" type="System.String" />

   <add name="LastName" type="System.String" />

   <add name="Gender" type="System.String" />

   <group name="Contact">

     <add name="MobileNo" type="System.String" />

     <add name="EmailAddress" type="System.String" />

   </group>

 </properties>

4.自訂Model



public class ProfileModel

{

[Display(Name = "First Name")]

        public string ForeNames { get; set; }

        [Display(Name = "Last Name")]

        public string LastName { get; set; }

        [Display(Name = "Gender")]

        public string Gender { get; set; }

        [Display(Name = "Mobile No")]

        public string MobileNo { get; set; }

        [Display(Name = "Email Address")]

        public string EmailAddress { get; set; }

}

5.新增讀取資訊Action

public ActionResult EditProfile()

{           

      ProfileBase _userProfile =ProfileBase.Create(HttpContext.User.Identity.Name);

      ProfileModel _profile = new ProfileModel();

      if (_userProfile.LastUpdatedDate > DateTime.MinValue)

      {

          _profile.ForeNames =Convert.ToString(_userProfile.GetPropertyValue("ForeNames"));

          _profile.LastName =Convert.ToString(_userProfile.GetPropertyValue("LastName"));

          _profile.Gender =Convert.ToString(_userProfile.GetPropertyValue("Gender"));

          _profile.MobileNo =Convert.ToString(_userProfile.GetProfileGroup("Contact").GetPropertyValue("MobileNo"));

          _profile.EmailAddress =Convert.ToString(_userProfile.GetProfileGroup("Contact").GetPropertyValue("EmailAddress"));

       }

       return View(_profile);

}

6.更新Action



[HttpPost]

public ActionResult EditProfile(ProfileModel model)

{

    if (ModelState.IsValid)

    {

       // Attempt to register the user profile

       System.Web.Profile.ProfileBase profile = System.Web.Profile.ProfileBase.Create(User.Identity.Name, true);

       if (profile != null)

       {

           profile.SetPropertyValue("Gender", model.Gender);

           profile.SetPropertyValue("ForeNames", model.ForeNames);

           profile.SetPropertyValue("LastName", model.LastName);

           profile.GetProfileGroup("Contact").SetPropertyValue("MobileNo", model.MobileNo);

           profile.GetProfileGroup("Contact").SetPropertyValue("EmailAddress", model.EmailAddress);

           profile.Save();

       }

       else

       {

           ModelState.AddModelError("", "Error writing to Profile");

       }

    }     

    return View(model);

}

以上再加入View後,在第一次執行時,就會在資料庫自動幫你建立資料表
image


參考文章

http://dotnetnsqlcorner.blogspot.tw/2012/04/membership-in-aspnet-mvc-4.html


Source Code
https://github.com/kimx/UserProfilerLabs

這個網誌中的熱門文章

[TFS] 分支與合併

[.NET Core] 將專案發行至IIS