2012-01-09 97 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 Code First中那样工作;您需要创建一个EDMX EF模型并手动包含这些aspnet表。但是,你也可以建立aspnet表的模型,并让代码首先将它们当作模型的一部分......我从来没有用EF代码先试过,并且不知道这是否可取(我怀疑它不是)。我一直推出自己的会员资格(OpenId等)。 – kamranicus 2012-01-09 18:20:33

回答

0

这就是我在我的项目中做的事情,我有一个其他课程,这是成员,里面我有电子邮件。我有一个AuthenticationService,我用我的用户在这里唱这个AuthenticationService ...

在web.config中我有两个不同的连接字符串,一个用于应用程序BD,另一个用于成员BD。

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; 

    } 
} 

我希望它可以帮助!