2012-01-09 21 views
1

MVC 3アプリケーションでaspnet_membershipを拡張するために、追加のメンバーの詳細を別のモデル/テーブルに格納します。私はASP.NET ProfileProviderの使用を検討していません。ASP.NET MVC 3のメンバーシップを継承userId

追加モデル/テーブルのメンバーのuserIdをプライマリ/外部キーとして使用したいと思います。どうすればこれを達成できますか?サンプルコードは正しい行に沿っていますか?

ありがとうございました。

public class Profile 
{ 
    [Key] 
    public Guid ProfileId { get; set; } 
    public string LastName { get; set; } 
    public string FirstName { get; set; } 

    public virtual MembershipUser User 
    { 
     get { return Membership.GetUser(ProfileId); } 
    } 

    public string FullName 
    { 
     get { return LastName + ", " + FirstName; } 
    } 
} 
+0

私はこれがEFコードファーストで欲しいのとまったく同じではないと思います。 EDMX EFモデルを作成し、aspnetテーブルを手動で組み込む必要があります。しかし、aspnetテーブルをモデル化して、コードで最初にそれらをモデルの一部として扱うこともできます。まず、EFコードでそれを試したことはありません。それが推奨されるかどうかわかりません。私はいつも自分のメンバーシップを転がしています(OpenIdなど)。 – kamranicus

答えて

0

これは私のプロジェクトでやっていることです。私はメンバーであり、私は電子メールを持っている他のクラスがあります。私はこのAuthenticationServiceのコードを私のユーザーで歌うために使用するAuthenticationServiceを持っています。

web.configには、アプリケーションBD用とメンバーシップ用BDの2種類の接続文字列があります。

public class AuthenticationService : IAuthenticationService 
{ 
    private readonly IConfigHelper _configHelper; 
    private readonly ISession _session; 

    public AuthenticationService(IConfigHelper configHelper, ISession session) 
    { 
     _configHelper = configHelper; 
     _session = session; 
    } 

    public bool IsValidLogin(string email, string password) 
    { 
     CheckLocked(email); 
     return Membership.ValidateUser(email, password); 
    } 

    public void SignIn(string email, bool createPersistentCookie) 
    { 
     if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); 
     FormsAuthentication.SetAuthCookie(email, createPersistentCookie); 
    } 

    public void SignOut() 
    { 
     FormsAuthentication.SignOut(); 
    } 

    public User GetLoggedUser() 
    { 
     var email = GetLoggedInUserName(); 

     if (IsMember()) 
      return _session.Single<Member>(x => x.Email == email); 

     return _session.Single<DelegateMember>(x => x.Email == email); 
    } 

    public string GetLoggedInUserName() 
    { 
     return Membership.GetUser() != null ? Membership.GetUser().UserName : string.Empty; 
    } 

    public MembershipCreateStatus RegisterUser(string email, string password, string role) 
    { 
     MembershipCreateStatus status; 
     //On doit laisser Guid.NewGuid().ToString() sinon ça ne passe pas 
     Membership.CreateUser(email, password, email, Guid.NewGuid().ToString(), Guid.NewGuid().ToString(), true, out status); 

     if (status == MembershipCreateStatus.Success) 
     { 
      Roles.AddUserToRole(email, role); 
     } 
     return status; 
    } 

    public MembershipUserCollection GetAllUsers() 
    { 
     return Membership.GetAllUsers(); 
    } 

    public string GeneratePassword() 
    { 
     var alphaCaps = "QWERTYUIOPASDFGHJKLZXCVBNM"; 
     var alphaLow = "qwertyuiopasdfghjklzxcvbnm"; 
     var numerics = "1234567890"; 
     var special = "@#$"; 
     var allChars = alphaCaps + alphaLow + numerics + special; 
     var r = new Random(); 
     var generatedPassword = ""; 
     for (int i = 0; i < MinPasswordLength - 1; i++) 
     { 
      double rand = r.NextDouble(); 
      if (i == 0) 
      { 
       //First character is an upper case alphabet 
       generatedPassword += alphaCaps.ToCharArray()[(int)Math.Floor(rand * alphaCaps.Length)]; 
       //Next one is numeric 
       rand = r.NextDouble(); 
       generatedPassword += numerics.ToCharArray()[(int) Math.Floor(rand*numerics.Length)]; 
      } 
      else 
      { 
       generatedPassword += allChars.ToCharArray()[(int)Math.Floor(rand * allChars.Length)]; 
      } 
     } 
     return generatedPassword; 
    } 

    public int MinPasswordLength 
    { 
     get 
     { 
      return Membership.Provider.MinRequiredPasswordLength; 
     } 
    } 

    public string AdminRole 
    { 
     get { return "admin"; } 
    } 

    public string MemberRole 
    { 
     get { return "member"; } 
    } 

    public string DelegateRole 
    { 
     get { return "delegate"; } 
    } 

    public string AgentRole 
    { 
     get { return "agent"; } 
    } 

    public bool Delete(string email) 
    { 
     return Membership.DeleteUser(email); 
    } 

    public bool IsAdmin() 
    { 
     return Roles.IsUserInRole(AdminRole); 
    } 

    public bool IsMember() 
    { 
     return Roles.IsUserInRole(MemberRole); 
    } 

    public bool IsDelegate() 
    { 
     return Roles.IsUserInRole(DelegateRole); 
    } 

    public bool IsAgent() 
    { 
     return Roles.IsUserInRole(AgentRole); 
    } 

    public bool ChangePassword(string email, string oldPassword, string newPassword) 
    { 
     if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); 
     if (String.IsNullOrEmpty(oldPassword)) throw new ArgumentException("Value cannot be null or empty.", "oldPassword"); 
     if (String.IsNullOrEmpty(newPassword)) throw new ArgumentException("Value cannot be null or empty.", "newPassword"); 

     // The underlying ChangePassword() will throw an exception rather 
     // than return false in certain failure scenarios. 
     try 
     { 
      var currentUser = Membership.Provider.GetUser(email, true); 
      return currentUser.ChangePassword(oldPassword, newPassword); 
     } 
     catch (ArgumentException) 
     { 
      return false; 
     } 
     catch (MembershipPasswordException) 
     { 
      return false; 
     } 
    } 

    public string ResetPassword(string email) 
    { 
     if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); 
     Unlock(email); 
     var currentUser = Membership.Provider.GetUser(email, false); 
     return currentUser.ResetPassword(); 
    } 

    public bool CheckLocked(string email) 
    { 
     if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); 
     var currentUser = Membership.Provider.GetUser(email, false); 
     if (currentUser == null) return false; 
     if (!currentUser.IsLockedOut) return false; 
     if (currentUser.LastLockoutDate.AddMinutes(30) < DateTime.Now) 
     { 
      currentUser.UnlockUser(); 
      return false; 
     } 
     return true; 
    } 

    public bool Unlock(string email) 
    { 
     if (String.IsNullOrEmpty(email)) throw new ArgumentException("Value cannot be null or empty.", "email"); 
     var currentUser = Membership.Provider.GetUser(email, false); 
     if (currentUser == null) return false; 
     currentUser.UnlockUser(); 
     return true; 

    } 
} 

私は助けてくれることを願っています!

関連する問題