Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ASP.NET MVC 3 inheriting Membership 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; }
- }
- }
- 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;
- }
- }
Add Comment
Please, Sign In to add comment