ASP.NET MVC 3继承成员身份userId



我希望通过在单独的模型/表中存储额外的成员详细信息来扩展MVC 3应用程序中的aspnet_membership。我不打算使用ASP.NET ProfileProvider。

我想使用成员的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; }
    }
}

这就是我在项目中所做的,我有另一个类,它是Member,里面有电子邮件。我有一个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;
    }
}

我希望它能有所帮助!

相关内容

最新更新