原來的前後台版本是用WebMatrix/WebSecurity,
而如今前台要改版成為行動版,而VS2012的mobile的範本的membership機制是用「Universal Provider」,這下子麻煩大了
所幸,前人已經踢過鐵板了,參考這篇連結吧
使用ASP.NET SimpleMembership 提供者
以及
To call this method, the “Membership.Provider” property must be an instance of “ExtendedMembershipProvider”
另外,以下摘錄實作程序(真的是我實過也試過可以run)
步驟一,修改web.config,加入原本auth的db connection
=====================
<connectionStrings>
<add name="memberConnection" ........ providerName="System.Data.SqlClient" />
</connectionStrings>
=====================
步驟二.在web.config中,把原本的
==========================
<profile defaultProvider="DefaultProfileProvider">
...
...
</roleManager>
==============================
改成以下的設定:
==============================================
<profile defaultProvider="SimpleProfileProvider">
<providers>
<add name="SimpleProfileProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" connectionStringName="DefaultConnection" applicationName="/" />
</providers>
</profile>
<membership defaultProvider="SimpleMembershipProvider">
<providers>
<add name="SimpleMembershipProvider" type="WebMatrix.WebData.SimpleMembershipProvider, WebMatrix.WebData" />
</providers>
</membership>
<roleManager defaultProvider="SimpleRoleProvider">
<providers>
<add name="SimpleRoleProvider" type="WebMatrix.WebData.SimpleRoleProvider, WebMatrix.WebData" />
</providers>
</roleManager>
==============================================
步驟四,到Gloabal.aspx.cs的 Application_Start函數,加上webSecurity的起始設定
(當然這是基於你已經把role/user_profile...等其他資料表都開好的假設)
==========================
protected void Application_Start()
{
//memberConnection是我們在web.config中設定的connection string.
WebSecurity.InitializeDatabaseConnection("memberConnection", "StoreManager", "UserId", "UserName", autoCreateTables: true);
AreaRegistration.RegisterAllAreas();
WebApiConfig.Register(GlobalConfiguration.Configuration);
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
==========================
步驟五,修改model中,RegisterModel等其他model的欄位,有的非必要的就把他的[Required]拿掉
步驟六.修改Account Controller--基本上都不必改,唯一要改的是「新建用戶帳號」那段
因為實作是委託給WebSecurity,所以小改一下才會成功:
=======原本========================
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
// 嘗試註冊使用者
MembershipCreateStatus createStatus;
Membership.CreateUser(model.UserName, model.Password, model.Email, passwordQuestion: null, passwordAnswer: null, isApproved: true, providerUserKey: null, status: out createStatus);
if (createStatus == MembershipCreateStatus.Success)
{
FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
return RedirectToAction("Index", "Home");
}
else
{
ModelState.AddModelError("", ErrorCodeToString(createStatus));
}
}
// 如果執行到這裡,發生某項失敗,則重新顯示表單
return View(model);
}
==============改為==================================
public ActionResult Register(RegisterModel model)
{
if (ModelState.IsValid)
{
try {
WebMatrix.WebData.WebSecurity.CreateUserAndAccount(model.UserName, model.Password);
//自己再加入user & Role mapping table--「webpages_UsersInRoles」的inser動作吧...
FormsAuthentication.SetAuthCookie(model.UserName, createPersistentCookie: false);
return RedirectToAction("Index", "Home");
}catch(Exception exp)
{
ModelState.AddModelError("", exp.Message);
}
}
// 如果執行到這裡,發生某項失敗,則重新顯示表單
return View(model);
}
======================================================
其他登入/改變密碼都沒問題.這樣算是大功告成了!!
以上,誌於 2013.10.19
沒有留言:
張貼留言