ASP.NET MVC - My MVC Applicationを解析してみる (4)Register - データアクセス層
Register.aspxより呼び出されるRegisterアクションを解析し、データアクセスの仕組みを
中心にみていきます。
◆AccountController.cs
97行目のValidateRegistration()で入力チェックを行い、
91: [AcceptVerbs(HttpVerbs.Post)]
92: public actionresult register(string username, string email,
string password, string confirmPassword)
93: {
94:
95: ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
96:
97: if (ValidateRegistration(userName, email, password, confirmPassword))
98: {
99: // Attempt to register the user
100: MembershipCreateStatus createStatus =
MembershipService.CreateUser(userName, password, email);
101:
102: if (createStatus == MembershipCreateStatus.Success)
103: {
104: FormsAuth.SignIn(userName, false /* createPersistentCookie */);
105: return RedirectToAction("Index", "Home");
106: }
107: else
108: {
109: ModelState.AddModelError("_FORM", ErrorCodeToString(createStatus));
110: }
111: }
112:
113: // If we got this far, something failed, redisplay form
114: return View();
115:}
エラーが無い場合は100行目でMembershipService.CreateUser()を呼び出してユーザー登録を行います。
このMembershipServiceは System.Web.Security名前空間に属するクラスで、ASP.NET 内の認証用の機能です(※1)。
MembershipServiceがユーザー情報を格納する先は、デフォルトでは既定のプロバイダが自動的に指定されます(※2)。
なお、web.config内にあるmembership 要素の defaultProvider 属性を使用して、MembershipServiceが使用するプロバイダを指定することも可能です(※2)。
今回のまとめ
1.My MVC Applicationでは、ユーザー認証にMembershipServiceという認証用のクラスを使用している。
2.ユーザー情報の格納先を独自に定義することも可能である。
3.ユーザー情報へのアクセスはプロバイダを使用し、直接SQL文を実行しているわけではない。
実際の案件ではSQLServer以外のデータベースに接続しなければならない場合も想定でき、
そうするとプロバイダではなく従来のADO.NETの仕組みを使用する必要性がでてくくると思われます。
ASP.NET MVCでどのようにADO.NET、もしくはそれ同等の接続方法を使用できるかは、今後の要調査項目です。。。
※1 メンバシップの概要 参照。
※2 ASP.NET アプリケーションの設定によるメンバシップの使用 参照。