Loginコントロールを使わずに、力業でASP.NETにLogin
注:追記あり
ASP.NET 2.0のMembershipオブジェクト、素敵な機能ですけど、標準の構成ではパスワードの質問が必須だったり、Loginコントロールを使わないとログイン状態が制御できなかったりして、案外小回りが効きません。
とりあえず、各種コントロールはあまり使わずに自分でユーザー登録などを実装しているのですが、CreateUsr()でユーザー登録直後にProfileメンバにアクセスできなくて困りました。というのを力業で解決。
とりあえず、↓のような感じで動きました。(無保証)
参考:
using System.Web.Security;
using System.Security.Principal;private void CreateUser(string username, string passwors, string email, string ketaiemail)
{
// ユーザーの作成
MembershipUser user = Membership.CreateUser(username, password, email);// 力業でASP.NETにLoginさせる
GenericIdentity identitiy = new GenericIdentity(user.UserName);
RolePrincipal principal = new RolePrincipal(identitiy);
Context.User = principal;// ↑がないとProvider.ProviderExceptionが発生
Profile.KetaiEMail = ketaiemail;
}
Using the ASP.NET membership provider in a Windows forms application
ASP.NET 2.0 Profile/Membership/Forms Authentication - Manual Login
追記:
Profileの値設定だけなら、わざわざログインしなくてもいい気がしてきました...。
で十分っぽいですね。ま、何かの役に立つかも知れないので残しておきます。
ProfileCommon prof = Profile.GetProfile(user.UserName);
prof.SetPropertyValue("KetaiEMail", ketaiemail);
prof.Save();
ちなみに、CreateUser()の後、
しておくと、アカウントを非アクティブ化してログイン出来ない状態にしておけます。その場で即登録ではなく、メールで登録確認などを行うのに便利です。
user.IsApproved = false;
Membership.UpdateUser(user);