のデフォルトプロバイダとしてあなたのメンバシッププロバイダを登録することができ、私はおそらくそれについて次のように行くでしょう:
まず、あなたがEntity FrameworkまたはいくつかのdaでSimpleMembershipを使用していると仮定します(ADO、LINQ to SQLなど)の2つのコンポーネントがあります:WebSecurity.*
メソッド呼び出し、およびプロファイル変更を行うためのデータベース接続。個人的には、データが純粋であることを保証するためにCONSTRAINT
をデータベースに追加しますが、このロジックも処理するメンバシップサービスを実装することもできます。
まず、お使いのコントローラ(以下のようなもの)で参照することができるインタフェースにグループこれら:
public interface IMembershipService
{
Int32 CurrentUserId { get; }
String CurrentUserName { get; }
Boolean IsAuthenticated { get; }
Boolean CreateUserAndAccount(String username, String password, String emailaddress = null);
Boolean CreateUserAndAccount(String username, string password, out String confirmationToken, String emailaddress = null);
Boolean Login(String username, String password, Boolean persistCookie = false);
void Logout();
}
次にあなたがSimpleMembershipのハイブリッドおよびデータベース接続などのサービスを実装することができます。一般的なままにするために、私はIRepository<T>
パターンを使用していますが、これは直接DbContext
、ObjectContext
などになる可能性があります。
public class MembershipService : IMembershipService
{
protected readonly SimpleMembershipProvider membershiProvider;
protected readonly SimpleRoleProvider roleProvider;
protected readonly IRepository<UserProfile> profileRepository;
public MembershipService(IRepository<UserProfile> profileRepository)
{
this.membershipProvider = Membership.Provider as SimpleMembershipProvider;
this.roleProvider = Role.Provider as SimpleRoleProvider;
this.profileRepository = userRepository;
}
#region IMembershipService Implementation
public Int32 CurrentUserId
{
get { return WebSecurity.CurrentUserId; }
}
public String CurrentUserName
{
get { return WebSecurity.CurrentUserName; }
}
public Boolean IsAuthenticated
{
get { return WebSecurity.IsAuthenticated; }
}
public Boolean CreateUserAndAccount(String username, String password, String emailaddress = null)
{
// validate the email address is unique
if (!this.profileRepository.Any(x => x.EmailAddress == emailaddress))
{
WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
// handle the error how you see fit
// (maybe even exception?)
return false;
}
}
public Boolean CreateUserAndAccount(String username, String password, out String confirmationToken, String emailaddress = null, out)
{
// validate the email address is unique
if (this.profileRepository.First(x => x.EmailAddress == emailaddress) == null)
{
confirmationToken = WebSecurity.CreateUserAndAccount(username, password, new
{
EmailAddress = emailaddress
}, createConfirmationToken);
return true;
}
else
{
// handle the error how you see fit
// (maybe even exception?)
confirmationToken = String.Empty;
return false;
}
}
public Boolean Login(String username, String password, Boolean persistCookie = false)
{
return WebSecurity.Login(username, password, persistCookie);
}
public void Logout()
{
WebSecurity.Logout();
}
#endregion
}
コントローラでこのインターフェイスを参照し、ロジックを1か所に配置することができます。あなたはDIコンテナを使用している場合、明らかにそれを登録しますが、ここでの実装例です:あなたがEntityFrameworkを使用している場合
public class AccountController: Controller
{
private readonly IMembershipService membershipService;
public AccountController(IMembershipService membershipService)
{
this.membershipService = membershipService;
}
/* ... */
[HttpPost, ValidateAntiForgeryToken]
public ActionResult Register(LoginViewModel model, String returnUrl)
{
if (ModelState.IsValid)
{
if (this.membershipService.CreateUserandAccount(model.Username, model.Password, model.EmailAddress))
{
this.membershipService.Login(model.Username, model.Password);
if (!String.IsNullOrEmpty(returnUrl) && Url.IsLocalUrl(returnUrl))
{
return Redirect(returnUrl);
}
return RedirectToRoute("Default");
}
else
{
ModelState.AddModelError("", "Unable to register.");
}
}
return View(model);
}
/* ... */
}
、あなたもIValidatableObject
を使用することができます。複製に抵抗するために、ここで一意のエントリをチェックし、別のSO質問/答えだ:それは新しいデータベースの場合
Entity Framework IValidatableObject
、別のオプションは、ユーザー名として電子メールを使用することです。ユーザー名はデフォルトで一意です。 – Igarioshka