public RazorEmailResult SendAccountNameReminder(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Here's your missing Username";
     return Email("SendAccountNameReminder", model);
 }
 public RazorEmailResult SendAccountVerified(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Account Verification";
     return Email("SendAccountVerified", model);
 }
 public RazorEmailResult SendAccountDelete(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Delete Account";
     return Email("SendAccountDelete", model);
 }
 public RazorEmailResult SendChangeEmailRequestNotice(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Email Request Notice";
     return Email("SendChangeEmailRequestNotice", model);
 }
 public RazorEmailResult SendEmailChangedNotice(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Email Changed Notification";
     return Email("SendEmailChangedNotice", model);
 }
 private static string BuildUrl(User user, string action, string controller, object routeValues = null)
 {
     if (UrlHelper.RequestContext.HttpContext.Request.Url != null)
     {
         string scheme = UrlHelper.RequestContext.HttpContext.Request.Url.Scheme;
         return UrlHelper.Action(action, controller, routeValues, scheme);
     }
     return null;
 }
        public virtual void SignIn(User user, bool isPersistant = false)
        {
            Tracing.Information(String.Format("[ClaimsBasedAuthenticationService.Signin] called: {0}", user.Username));

            if (String.IsNullOrWhiteSpace(user.Username)) throw new ArgumentException("username");

            // gather claims
            var claims = new List<Claim>();
            foreach (UserClaim uc in user.Claims)
                claims.Add(new Claim(uc.Type, uc.Value));

            if (!String.IsNullOrWhiteSpace(user.Email))
            {
                claims.Insert(0, new Claim(ClaimTypes.Email, user.Email));
            }
            claims.Insert(0, new Claim(ClaimTypes.AuthenticationMethod, AuthenticationMethods.Password));
            claims.Insert(0, new Claim(ClaimTypes.AuthenticationInstant, DateTime.UtcNow.ToString("s")));
            claims.Insert(0, new Claim(ClaimTypes.Name, user.Username));
            claims.Insert(0, new Claim(ClaimTypes.NameIdentifier, user.Username));

            // create principal/identity
            var id = new ClaimsIdentity(claims, "Forms");
            var cp = new ClaimsPrincipal(id);

            // claims transform
            cp = FederatedAuthentication.FederationConfiguration.IdentityConfiguration.ClaimsAuthenticationManager.Authenticate(String.Empty, cp);

            // issue cookie
            var sam = FederatedAuthentication.SessionAuthenticationModule;
            if (sam == null)
                throw new Exception("SessionAuthenticationModule is not configured and it needs to be.");

            var token = new SessionSecurityToken(cp, isPersistant ?  FormsAuthentication.Timeout : TimeSpan.FromMinutes(SessionHelpers.GetSessionTimeoutInMinutes))
                {
                    IsPersistent = isPersistant
                };
            sam.WriteSessionTokenToCookie(token);

            Tracing.Verbose(String.Format("[ClaimsBasedAuthenticationService.Signin] cookie issued: {0}", claims.GetValue(ClaimTypes.NameIdentifier)));
        }
        public void Seed(DataContext context)
        {
            var user = new User()
                {
                    Id = 1,
                    Email = "*****@*****.**",
                    Username = "******",
                    FirstName = "Rod",
                    LastName = "Johnson",
                    LastLogin = DateTime.UtcNow,
                    Gender = Gender.Male,
                    Address = "Admin address",
                    PhoneNumber = "555-555-5555",
                    IsLoginAllowed = true,
                    IsAccountClosed = false,
                    IsAccountVerified = true,
                    Created = DateTime.UtcNow,
                    Tenant = "default",
                    // password is "admin"
                    HashedPassword = "******",
                    PasswordChanged = DateTime.UtcNow,
                    FailedLoginCount = 0,
                    Updated = DateTime.UtcNow
                };

            user.Claims.Add(new UserClaim()
                {
                    Type = ClaimTypes.Role,
                    Value = "Admin"
                });

            user.Claims.Add(new UserClaim()
            {
                Type = ClaimTypes.Role,
                Value = "Super Admin"
            });

            context.Users.AddOrUpdate(user);
            context.SaveChanges();
        }
 public void SendAccountNameReminder(User user)
 {
     _controller.SendAccountNameReminder(user).Deliver();
 }
 public void SendAccountVerified(User user)
 {
     _controller.SendAccountVerified(user).Deliver();
 }
 public void SendChangeEmailRequestNotice(User user, string newEmail)
 {
     _controller.SendChangeEmailRequestNotice(user).Deliver();
 }
 public UserLoggedIn(User user)
     : base(user, "User Logged In")
 {
 }
 public UserLoggedOut(User user)
     : base(user, "User Logged Out")
 {
 }
 public UserCreated(User user, string loginUrl)
     : base(user, "User Created")
 {
     LoginUrl = loginUrl;
 }
 public void SendPasswordChangeNotice(User user)
 {
     _controller.SendPasswordChangeNotice(user).Deliver();
 }
 public UserLockedOut(User user)
 {
     User = user;
 }
 public RazorEmailResult SendPasswordChangeNotice(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Your Password has been updated";
     return Email("SendPasswordChangeNotice", model);
 }
 public MembershipEvent(MembershipEventCode code, User user)
     : base(code.GetDescription(), user, WebEventCodes.WebExtendedBase, (int)code)
 {
     _user = user;
     _culture = CultureInfo.CurrentCulture.Name;
 }
 public void SendResetPassword(User user)
 {
     _controller.SendPasswordReset(user).Deliver();
 }
 public void SendAccountDelete(User user)
 {
     _controller.SendAccountDelete(user).Deliver();
 }
        protected internal virtual void DeleteAccount(User account)
        {
            if (_settings.AllowAccountDeletion || !account.IsAccountVerified)
            {
                Tracing.Verbose(String.Format("[UserAccountService.DeleteAccount] removing account record: {0}, {1}", account.Tenant, account.Username));
                this.userRepository.Delete(account);
            }
            else
            {
                Tracing.Verbose(String.Format("[UserAccountService.DeleteAccount] marking account closed: {0}, {1}", account.Tenant, account.Username));
                account.CloseAccount();
            }

            using (var tx = new TransactionScope())
            {
                this.userRepository.SaveOrUpdate(account);

                if (this.notificationService != null)
                {
                    this.notificationService.SendAccountDelete(account);
                }

                tx.Complete();
            }
        }
        public virtual IValidationContainer<User> CreateAccount(string tenant, string username, string password, string email, string firstName, string lastName, string phone, string address)
        {
            Tracing.Information(String.Format("[UserAccountService.CreateAccount] called: {0}, {1}, {2}", tenant, username, email));

            if (_settings.EmailIsUsername)
            {
                username = email;
            }

            if (!_settings.MultiTenant)
            {
                tenant = _settings.DefaultTenant;
            }

            if (String.IsNullOrWhiteSpace(tenant)) throw new ArgumentException("tenant");
            if (String.IsNullOrWhiteSpace(username)) throw new ArgumentException("username");
            if (String.IsNullOrWhiteSpace(password)) throw new ArgumentException("password");
            if (String.IsNullOrWhiteSpace(email)) throw new ArgumentException("email");
            if (String.IsNullOrWhiteSpace(email)) throw new ArgumentException("firstName");
            if (String.IsNullOrWhiteSpace(email)) throw new ArgumentException("lastName");
            if (String.IsNullOrWhiteSpace(email)) throw new ArgumentException("phone");
            if (String.IsNullOrWhiteSpace(email)) throw new ArgumentException("address");

            ValidatePassword(tenant, username, password);

            var validator = new EmailAddressAttribute();
            if (!validator.IsValid(email))
            {
                Tracing.Verbose(String.Format("[UserAccountService.CreateAccount] Email validation failed: {0}, {1}, {2}", tenant, username, email));

                throw new ValidationException("Email is invalid.");
            }

            if (UsernameExists(tenant, username))
            {
                Tracing.Verbose(String.Format("[UserAccountService.CreateAccount] Username already exists: {0}, {1}", tenant, username));

                var msg = _settings.EmailIsUsername ? "Email" : "Username";
                throw new ValidationException(msg + " already in use.");
            }

            if (EmailExists(tenant, email))
            {
                Tracing.Verbose(String.Format("[UserAccountService.CreateAccount] Email already exists: {0}, {1}, {2}", tenant, username, email));

                throw new ValidationException("Email already in use.");
            }

            var account = new User(tenant, username, password, email)
                {
                    FirstName = firstName,
                    LastName = lastName,
                    Address = address,
                    PhoneNumber = phone
                };

            var validation = account.GetValidationContainer();
            if (!validation.IsValid)
                return validation;

            this.userRepository.SaveOrUpdate(account);

            if (this.notificationService != null)
            {
                if (_settings.RequireAccountVerification)
                {
                    this.notificationService.SendAccountCreate(account);
                }
                else
                {
                    this.notificationService.SendAccountVerified(account);
                }
            }

            _unitOfWork.Commit();
            return validation;
        }
 public void SendEmailChangedNotice(User user, string oldEmail)
 {
     _controller.SendEmailChangedNotice(user).Deliver();
 }
 public RazorEmailResult SendPasswordReset(User model)
 {
     To.Add(model.Email);
     From = "*****@*****.**";
     Subject = "Password Reset";
     return Email("SendPasswordReset", model);
 }
        public IValidationContainer<User> SaveOrUpdate(User user)
        {
            var container = user.GetValidationContainer();
            if (!container.IsValid)
                return container;

            userRepository.SaveOrUpdate(user);
            _unitOfWork.Commit();

            return container;
        }
        protected internal virtual IValidationContainer<User> Authenticate(User account, string password, int failedLoginCount, TimeSpan lockoutDuration)
        {
            var container = account.GetValidationContainer();
            if (!container.IsValid)
                return container;

            var result = account.Authenticate(password, failedLoginCount, lockoutDuration);

            if (!result)
                container.ValidationErrors.Add("", new List<string>() { "Unable to authenticate user" });

            this.userRepository.SaveOrUpdate(account);
            _unitOfWork.Commit();

            Tracing.Verbose(String.Format("[UserAccountService.Authenticate] authentication outcome: {0}, {1}, {2}", account.Tenant, account.Username, result ? "Successful Login" : "Failed Login"));

            return container;
        }
 protected UserActivity(User user, string friendlyName)
 {
     FriendlyName = friendlyName;
     User = user;
 }